Skip to content

Commit

Permalink
Moved to lists of objects for NIC associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Cheney committed Jun 30, 2020
1 parent 8f15848 commit a54f72f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
51 changes: 36 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,45 @@ locals {
identity_id = try(coalesce(var.identity_id, var.defaults.identity_id), null)
storage_account_type = coalesce(var.storage_account_type, var.defaults.storage_account_type, "Standard_LRS")

application_security_group_ids = { for p in setproduct(local.names, keys(var.application_security_group_ids)) :
format("%s-%s", p[0], p[1]) => {
vm_name = p[0]
application_security_group_id = var.application_security_group_ids[p[1]]
application_security_groups = {
for object in var.application_security_groups :
object.name => object
}

load_balancer_backend_address_pools = {
for object in var.load_balancer_backend_address_pools :
object.name => object
}

application_gateway_backend_address_pools = {
for object in var.application_gateway_backend_address_pools :
object.name => object
}

vms_to_application_security_groups = {
for prod in setproduct(local.names, keys(local.application_security_groups)) :
format("%s-%s", prod[0], prod[1]) => {
vm_name = prod[0]
application_security_group_name = local.application_security_groups[prod[1]].name
application_security_group_id = local.application_security_groups[prod[1]].id
}
}

load_balancer_backend_address_pool_ids = { for p in setproduct(local.names, keys(var.load_balancer_backend_address_pool_ids)) :
format("%s-%s", p[0], p[1]) => {
vm_name = p[0]
backend_address_pool_id = var.load_balancer_backend_address_pool_ids[p[1]]
vms_to_load_balancer_backend_address_pools = {
for prod in setproduct(local.names, keys(local.load_balancer_backend_address_pools)) :
format("%s-%s", prod[0], prod[1]) => {
vm_name = prod[0]
backend_address_pool_name = local.load_balancer_backend_address_pools[prod[1]].name
backend_address_pool_id = local.load_balancer_backend_address_pools[prod[1]].id
}
}

application_gateway_backend_address_pool_ids = { for p in setproduct(local.names, keys(var.application_gateway_backend_address_pool_ids)) :
format("%s-%s", p[0], p[1]) => {
vm_name = p[0]
backend_address_pool_id = var.application_gateway_backend_address_pool_ids[p[1]]
vms_to_application_gateway_backend_address_pools = {
for prod in setproduct(local.names, keys(local.application_gateway_backend_address_pools)) :
format("%s-%s", prod[0], prod[1]) => {
vm_name = prod[0]
backend_address_pool_name = local.application_gateway_backend_address_pools[prod[1]].name
backend_address_pool_id = local.application_gateway_backend_address_pools[prod[1]].id
}
}
}
Expand Down Expand Up @@ -61,20 +82,20 @@ resource "azurerm_network_interface" "vm" {
}

resource "azurerm_network_interface_application_security_group_association" "vm" {
for_each = local.application_security_group_ids
for_each = local.vms_to_application_security_groups
network_interface_id = azurerm_network_interface.vm[each.value.vm_name].id
application_security_group_id = each.value.application_security_group_id
}

resource "azurerm_network_interface_backend_address_pool_association" "vm" {
for_each = local.load_balancer_backend_address_pool_ids
for_each = local.vms_to_load_balancer_backend_address_pools
network_interface_id = azurerm_network_interface.vm[each.value.vm_name].id
ip_configuration_name = "ipconfiguration1"
backend_address_pool_id = each.value.backend_address_pool_id
}

resource "azurerm_network_interface_application_gateway_backend_address_pool_association" "example" {
for_each = local.application_gateway_backend_address_pool_ids
for_each = local.vms_to_application_gateway_backend_address_pools
network_interface_id = azurerm_network_interface.vm[each.value.vm_name].id
ip_configuration_name = "ipconfiguration1"
backend_address_pool_id = each.value.backend_address_pool_id
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ output "vm" {
"ssh_command" = "ssh ${vm.admin_username}@${vm.private_ip_address}"
}
}
}
}
37 changes: 23 additions & 14 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ variable "boot_diagnostics_uri" {
// ==============================================================================

variable "availability_set_name" {
description = "Create an availability set with the specified name. Do not use both availability_set_name and availability_set_id."
description = "Create an availability set with the specified name. Use either availability_set_name or availability_set_id."
type = string
default = ""
}

variable "availability_set_id" {
description = "Availability set resource ID - attaches NIC. Do not use both availability_set_name and availability_set_id."
description = "Resource ID for existing availability set. Use either availability_set_name or availability_set_id."
type = string
default = null
}
Expand All @@ -158,22 +158,31 @@ variable "availability_set_id" {
// These are maps as we need the key to create the Terraform ident
// The ids are not known at plan time

variable "application_security_group_ids" {
description = "Application security group: name => id"
type = map(string)
default = {}
variable "application_security_groups" {
description = "List of application security group objects, name and id."
type = list(object({
name = string
id = string
}))
default = []
}

variable "load_balancer_backend_address_pool_ids" {
description = "Load balancer's backend pools: name => id"
type = map(string)
default = {}
variable "load_balancer_backend_address_pools" {
description = "List of load balancer's backend pools objects, name and id."
type = list(object({
name = string
id = string
}))
default = []
}

variable "application_gateway_backend_address_pool_ids" {
description = "Application gateway backend pools: name => id"
type = map(string)
default = {}
variable "application_gateway_backend_address_pools" {
description = "List of application gateway backend pools objects, name and id."
type = list(object({
name = string
id = string
}))
default = []
}

// ==============================================================================
Expand Down

0 comments on commit a54f72f

Please sign in to comment.