Skip to content

Commit 365477a

Browse files
committed
Add network dependency
1 parent 535727c commit 365477a

4 files changed

Lines changed: 68 additions & 37 deletions

File tree

examples/simple/main.tf

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ resource "openstack_compute_keypair_v2" "keypair" {
44
name = "my-keypair"
55
}
66
module "compute" {
7-
source = "../../"
8-
instance_name = "BLUE"
9-
instance_count = 2
10-
image_name = "cirros"
11-
flavor_name = "m1.tiny"
12-
keypair = "${openstack_compute_keypair_v2.keypair.name}"
13-
network_name = "my-network"
14-
security_group_names = ["default"]
7+
source = "../../"
8+
instance_name = "BLUE"
9+
instance_count = 2
10+
image_name = "cirros"
11+
flavor_name = "m1.tiny"
12+
keypair = "${openstack_compute_keypair_v2.keypair.name}"
13+
network_ids = ["${openstack_compute_keypair_v2.keypair.id}"]
14+
subnet_ids = ["${module.mgmt_network.subnet_ids}"]
15+
security_group_name = "${var.project_name}-sg"
16+
security_group_rules = "${var.bastion_security_group_rules}"
1517
}

main.tf

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,58 @@ data "openstack_images_image_v2" "this" {
77
most_recent = true
88
}
99

10-
data "openstack_networking_network_v2" "this" {
11-
name = "${var.network_name}"
10+
resource "openstack_networking_secgroup_v2" "this" {
11+
count = "${var.security_group_name != "" ? 1 : 0}"
12+
13+
name = "${var.security_group_name}"
14+
description = "${format("%s %s", var.security_group_name, "security group")}"
1215
}
1316

14-
data "openstack_networking_secgroup_v2" "this" {
15-
count = "${length(var.security_group_names)}"
17+
resource "openstack_networking_secgroup_rule_v2" "this" {
18+
count = "${length(var.security_group_rules)}"
1619

17-
name = "${var.security_group_names[count.index]}"
20+
port_range_min = "${lookup(var.security_group_rules[count.index], "port_range_min")}"
21+
port_range_max = "${lookup(var.security_group_rules[count.index], "port_range_max")}"
22+
protocol = "${lookup(var.security_group_rules[count.index], "protocol")}"
23+
direction = "${lookup(var.security_group_rules[count.index], "direction")}"
24+
ethertype = "${lookup(var.security_group_rules[count.index], "ethertype")}"
25+
remote_ip_prefix = "${lookup(var.security_group_rules[count.index], "remote_ip_prefix")}"
26+
security_group_id = "${element(openstack_networking_secgroup_v2.this.*.id, count.index)}"
1827
}
1928

20-
resource "openstack_compute_instance_v2" "this" {
21-
count = "${var.instance_count}"
29+
# This trigger wait for subnet defined outside of this module to be created
30+
resource "null_resource" "network_subnet_found" {
31+
count = "${length(var.subnet_ids)}"
2232

23-
name = "${var.instance_name}-${count.index}"
24-
image_name = "${data.openstack_images_image_v2.this.name}"
25-
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
26-
key_pair = "${var.keypair}"
27-
28-
network {
29-
port = "${openstack_networking_port_v2.this.*.id[count.index]}"
33+
triggers = {
34+
subnet = "${var.subnet_ids[count.index][0]}"
3035
}
3136
}
3237

33-
resource "openstack_networking_port_v2" "this" {
38+
resource "openstack_compute_instance_v2" "this" {
3439
count = "${var.instance_count}"
3540

36-
name = "${var.network_name}-port-${count.index}"
37-
network_id = "${data.openstack_networking_network_v2.this.id}"
38-
admin_state_up = "true"
39-
security_group_ids = ["${data.openstack_networking_secgroup_v2.this.*.id}"]
41+
depends_on = ["null_resource.network_subnet_found"]
42+
43+
name = "${var.instance_count > 1 ? format("%s-%s", var.instance_name, count.index) : var.instance_name}"
44+
image_name = "${data.openstack_images_image_v2.this.name}"
45+
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
46+
key_pair = "${var.keypair}"
47+
security_groups = "${openstack_networking_secgroup_v2.this.*.name}"
48+
49+
dynamic "network" {
50+
for_each = var.network_ids
51+
52+
content {
53+
uuid = network.value
54+
}
55+
}
4056
}
4157

4258
# resource "openstack_compute_interface_attach_v2" "this" {
4359
# count = "${var.instance_count}"
4460

4561

4662
# instance_id = "${openstack_compute_instance_v2.this.*.id[count.index]}"
47-
# port_id = "${openstack_networking_port_v2.this.*.id[count.index]}"
63+
# network_id = "${var.network_id}"
4864
# }
49-

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ output "ids" {
66
output "names" {
77
description = "List of instances name"
88
value = "${openstack_compute_instance_v2.this.*.name}"
9+
}
10+
11+
output "network_fixed_ip_v4" {
12+
value = "${openstack_compute_instance_v2.this.*.network.0.fixed_ip_v4}"
913
}

variables.tf

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@ variable "keypair" {
2424
description = "The name of the keypair to use"
2525
}
2626

27-
variable "network_name" {
28-
type = "string"
29-
default = ""
30-
description = "The name of the network to attach instance to"
27+
variable "network_ids" {
28+
type = list
29+
default = []
30+
description = "IDs of the networks to attach instance to"
31+
}
32+
33+
variable "subnet_ids" {
34+
type = list
35+
default = []
36+
description = "IDs of the networks subnet to attach instance to"
37+
}
38+
39+
variable "security_group_name" {
40+
type= "string"
3141
}
3242

33-
variable "security_group_names" {
34-
type = "list"
35-
default = ["default"]
36-
description = "The name of the network to attach instance to"
43+
variable "security_group_rules" {
44+
type = list(map(any))
45+
default = []
46+
description = "The definition os security groups to associate to instance. Only one is allowed"
3747
}

0 commit comments

Comments
 (0)