Skip to content

Commit

Permalink
Add network dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
orachide committed Jun 16, 2019
1 parent 535727c commit 365477a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
18 changes: 10 additions & 8 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ resource "openstack_compute_keypair_v2" "keypair" {
name = "my-keypair"
}
module "compute" {
source = "../../"
instance_name = "BLUE"
instance_count = 2
image_name = "cirros"
flavor_name = "m1.tiny"
keypair = "${openstack_compute_keypair_v2.keypair.name}"
network_name = "my-network"
security_group_names = ["default"]
source = "../../"
instance_name = "BLUE"
instance_count = 2
image_name = "cirros"
flavor_name = "m1.tiny"
keypair = "${openstack_compute_keypair_v2.keypair.name}"
network_ids = ["${openstack_compute_keypair_v2.keypair.id}"]
subnet_ids = ["${module.mgmt_network.subnet_ids}"]
security_group_name = "${var.project_name}-sg"
security_group_rules = "${var.bastion_security_group_rules}"
}
57 changes: 36 additions & 21 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,58 @@ data "openstack_images_image_v2" "this" {
most_recent = true
}

data "openstack_networking_network_v2" "this" {
name = "${var.network_name}"
resource "openstack_networking_secgroup_v2" "this" {
count = "${var.security_group_name != "" ? 1 : 0}"

name = "${var.security_group_name}"
description = "${format("%s %s", var.security_group_name, "security group")}"
}

data "openstack_networking_secgroup_v2" "this" {
count = "${length(var.security_group_names)}"
resource "openstack_networking_secgroup_rule_v2" "this" {
count = "${length(var.security_group_rules)}"

name = "${var.security_group_names[count.index]}"
port_range_min = "${lookup(var.security_group_rules[count.index], "port_range_min")}"
port_range_max = "${lookup(var.security_group_rules[count.index], "port_range_max")}"
protocol = "${lookup(var.security_group_rules[count.index], "protocol")}"
direction = "${lookup(var.security_group_rules[count.index], "direction")}"
ethertype = "${lookup(var.security_group_rules[count.index], "ethertype")}"
remote_ip_prefix = "${lookup(var.security_group_rules[count.index], "remote_ip_prefix")}"
security_group_id = "${element(openstack_networking_secgroup_v2.this.*.id, count.index)}"
}

resource "openstack_compute_instance_v2" "this" {
count = "${var.instance_count}"
# This trigger wait for subnet defined outside of this module to be created
resource "null_resource" "network_subnet_found" {
count = "${length(var.subnet_ids)}"

name = "${var.instance_name}-${count.index}"
image_name = "${data.openstack_images_image_v2.this.name}"
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
key_pair = "${var.keypair}"

network {
port = "${openstack_networking_port_v2.this.*.id[count.index]}"
triggers = {
subnet = "${var.subnet_ids[count.index][0]}"
}
}

resource "openstack_networking_port_v2" "this" {
resource "openstack_compute_instance_v2" "this" {
count = "${var.instance_count}"

name = "${var.network_name}-port-${count.index}"
network_id = "${data.openstack_networking_network_v2.this.id}"
admin_state_up = "true"
security_group_ids = ["${data.openstack_networking_secgroup_v2.this.*.id}"]
depends_on = ["null_resource.network_subnet_found"]

name = "${var.instance_count > 1 ? format("%s-%s", var.instance_name, count.index) : var.instance_name}"
image_name = "${data.openstack_images_image_v2.this.name}"
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
key_pair = "${var.keypair}"
security_groups = "${openstack_networking_secgroup_v2.this.*.name}"

dynamic "network" {
for_each = var.network_ids

content {
uuid = network.value
}
}
}

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


# instance_id = "${openstack_compute_instance_v2.this.*.id[count.index]}"
# port_id = "${openstack_networking_port_v2.this.*.id[count.index]}"
# network_id = "${var.network_id}"
# }

4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ output "ids" {
output "names" {
description = "List of instances name"
value = "${openstack_compute_instance_v2.this.*.name}"
}

output "network_fixed_ip_v4" {
value = "${openstack_compute_instance_v2.this.*.network.0.fixed_ip_v4}"
}
26 changes: 18 additions & 8 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ variable "keypair" {
description = "The name of the keypair to use"
}

variable "network_name" {
type = "string"
default = ""
description = "The name of the network to attach instance to"
variable "network_ids" {
type = list
default = []
description = "IDs of the networks to attach instance to"
}

variable "subnet_ids" {
type = list
default = []
description = "IDs of the networks subnet to attach instance to"
}

variable "security_group_name" {
type= "string"
}

variable "security_group_names" {
type = "list"
default = ["default"]
description = "The name of the network to attach instance to"
variable "security_group_rules" {
type = list(map(any))
default = []
description = "The definition os security groups to associate to instance. Only one is allowed"
}

0 comments on commit 365477a

Please sign in to comment.