Skip to content

Commit

Permalink
use for_each function instead of count in the third example
Browse files Browse the repository at this point in the history
  • Loading branch information
diodonfrost committed Dec 25, 2019
1 parent 46923b2 commit 199e37d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 41 deletions.
18 changes: 10 additions & 8 deletions 03-instance-with-multiple-network/00-variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ variable "network_http" {
}
}

# Set how many http instance you will deploy
variable "desired_capacity_http" {
type = number
default = 2
variable "http_instance_names" {
type = set(string)
default = ["http-instance-1",
"http-instance-2",
"http-instance-3"]
}

#### VM DB parameters ####
Expand All @@ -57,9 +58,10 @@ variable "network_db" {
}
}

# Set how many db instance you will deploy
variable "desired_capacity_db" {
type = number
default = 3
variable "db_instance_names" {
type = set(string)
default = ["db-instance-1",
"db-instance-2",
"db-instance-3"]
}

24 changes: 10 additions & 14 deletions 03-instance-with-multiple-network/060-instance_http.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Create instance
#
resource "openstack_compute_instance_v2" "http" {
count = var.desired_capacity_http
name = "http-${count.index}"
for_each = var.http_instance_names
name = each.key
image_name = var.image
flavor_name = var.flavor_http
key_pair = openstack_compute_keypair_v2.user_key.name
user_data = file("scripts/first-boot.sh")
network {
port = element(openstack_networking_port_v2.http.*.id, count.index)
port = openstack_networking_port_v2.http[each.key].id
}
}

# Create network port
resource "openstack_networking_port_v2" "http" {
count = var.desired_capacity_http
name = "port-http-${count.index}"
for_each = var.http_instance_names
name = "port-http-${each.key}"
network_id = openstack_networking_network_v2.generic.id
admin_state_up = true
security_group_ids = [
Expand All @@ -31,17 +31,13 @@ resource "openstack_networking_port_v2" "http" {

# Create floating ip
resource "openstack_networking_floatingip_v2" "http" {
count = var.desired_capacity_http
pool = var.external_network
for_each = var.http_instance_names
pool = var.external_network
}

# Attach floating ip to instance
resource "openstack_compute_floatingip_associate_v2" "http" {
count = var.desired_capacity_http
floating_ip = element(
openstack_networking_floatingip_v2.http.*.address,
count.index,
)
instance_id = element(openstack_compute_instance_v2.http.*.id, count.index)
for_each = var.http_instance_names
floating_ip = openstack_networking_floatingip_v2.http[each.key].address
instance_id = openstack_compute_instance_v2.http[each.key].id
}

21 changes: 10 additions & 11 deletions 03-instance-with-multiple-network/061-instance_db.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Create instance
#
resource "openstack_compute_instance_v2" "db" {
count = var.desired_capacity_db
name = "db-${count.index}"
for_each = var.db_instance_names
name = each.key
image_name = var.image
flavor_name = var.flavor_db
key_pair = openstack_compute_keypair_v2.user_key.name
user_data = file("scripts/first-boot.sh")
network {
port = element(openstack_networking_port_v2.db.*.id, count.index)
port = openstack_networking_port_v2.db[each.key].id
}
}

# Create network port
resource "openstack_networking_port_v2" "db" {
count = var.desired_capacity_db
name = "port-db-${count.index}"
for_each = var.db_instance_names
name = "port-db-${each.key}"
network_id = openstack_networking_network_v2.generic.id
admin_state_up = true
security_group_ids = [
Expand All @@ -31,14 +31,13 @@ resource "openstack_networking_port_v2" "db" {

# Create floating ip
resource "openstack_networking_floatingip_v2" "db" {
count = var.desired_capacity_db
pool = var.external_network
for_each = var.db_instance_names
pool = var.external_network
}

# Attach floating ip to instance
resource "openstack_compute_floatingip_associate_v2" "db" {
count = var.desired_capacity_db
floating_ip = element(openstack_networking_floatingip_v2.db.*.address, count.index)
instance_id = element(openstack_compute_instance_v2.db.*.id, count.index)
for_each = var.db_instance_names
floating_ip = openstack_networking_floatingip_v2.db[each.key].address
instance_id = openstack_compute_instance_v2.db[each.key].id
}

8 changes: 0 additions & 8 deletions 03-instance-with-multiple-network/provider.tf

This file was deleted.

0 comments on commit 199e37d

Please sign in to comment.