Skip to content

Commit e54da63

Browse files
authored
feat: add support for conditional creation and association of network security groups (#100)
Signed-off-by: Andre Licht <al@cloudeteer.de>
1 parent d11b3c0 commit e54da63

4 files changed

Lines changed: 31 additions & 29 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ The following resources are used by this module:
137137
- [azurerm_managed_disk.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk) (resource)
138138
- [azurerm_network_interface.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) (resource)
139139
- [azurerm_network_interface_security_group_association.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_security_group_association) (resource)
140-
- [azurerm_network_security_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group) (resource)
141140
- [azurerm_public_ip.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) (resource)
142141
- [azurerm_role_assignment.entra_id_login_admin](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
143142
- [azurerm_role_assignment.entra_id_login_user](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
@@ -667,7 +666,7 @@ Default: `null`
667666
Description: The resource ID of an existing Azure Network Security Group to associate with the network interface created by this module.
668667

669668
- Applies only when `create_network_interface` is `true`.
670-
- If omitted and `create_network_interface` is `true`, this module creates and associates a default Network Security Group.
669+
- If omitted, no Network Security Group is associated with the module-created network interface.
671670

672671
Type: `string`
673672

r-network.tf

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,10 @@ locals {
33
azurerm_network_interface.this[*].id,
44
(var.network_interface_ids != null ? var.network_interface_ids : [])
55
)
6-
network_security_group_id = (
7-
var.network_security_group_id != null ?
8-
var.network_security_group_id :
9-
try(one(azurerm_network_security_group.this[*].id), null)
10-
)
6+
network_security_group_id = var.network_security_group_id
117

128
}
139

14-
resource "azurerm_network_security_group" "this" {
15-
16-
count = var.create_network_interface && var.network_security_group_id == null ? 1 : 0
17-
18-
name = "nsg-${trimprefix(var.name, "vm-")}"
19-
location = var.location
20-
resource_group_name = var.resource_group_name
21-
tags = var.tags
22-
}
23-
2410
resource "azurerm_network_interface" "this" {
2511

2612
count = var.create_network_interface ? 1 : 0
@@ -45,7 +31,7 @@ resource "azurerm_network_interface" "this" {
4531
}
4632

4733
resource "azurerm_network_interface_security_group_association" "this" {
48-
count = var.create_network_interface ? 1 : 0
34+
count = var.create_network_interface && var.network_security_group_id != null ? 1 : 0
4935

5036
network_interface_id = azurerm_network_interface.this[0].id
5137
network_security_group_id = local.network_security_group_id

tests/local/input_network.tftest.hcl

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ run "should_use_subnet_id" {
2121
}
2222

2323
assert {
24-
condition = length(azurerm_network_security_group.this) == 1
25-
error_message = "Expected the module to create a default network security group when no network_security_group_id is provided."
26-
}
27-
28-
assert {
29-
condition = length(azurerm_network_interface_security_group_association.this) == 1
30-
error_message = "Expected the created network interface to have a network security group association."
24+
condition = length(azurerm_network_interface_security_group_association.this) == 0
25+
error_message = "Expected no network security group association when network_security_group_id is not provided."
3126
}
3227
}
3328

@@ -71,13 +66,35 @@ run "should_use_existing_network_security_group_for_created_network_interface" {
7166
}
7267

7368
assert {
74-
condition = length(azurerm_network_security_group.this) == 0
75-
error_message = "Expected no network security group to be created when network_security_group_id is provided."
69+
condition = length(azurerm_network_interface_security_group_association.this) == 1
70+
error_message = "Expected the created network interface to have a network security group association when network_security_group_id is provided."
71+
}
72+
}
73+
74+
run "should_not_associate_network_security_group_when_not_provided" {
75+
command = plan
76+
77+
variables {
78+
subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/snet"
79+
}
80+
81+
assert {
82+
condition = length(azurerm_network_interface_security_group_association.this) == 0
83+
error_message = "Expected no network security group association when no network_security_group_id is provided."
84+
}
85+
}
86+
87+
run "should_associate_existing_network_security_group_when_provided" {
88+
command = plan
89+
90+
variables {
91+
network_security_group_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/networkSecurityGroups/nsg-existing"
92+
subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/snet"
7693
}
7794

7895
assert {
7996
condition = length(azurerm_network_interface_security_group_association.this) == 1
80-
error_message = "Expected the created network interface to have a network security group association when network_security_group_id is provided."
97+
error_message = "Expected the provided network_security_group_id to be associated with the created network interface."
8198
}
8299
}
83100

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ variable "network_security_group_id" {
544544
The resource ID of an existing Azure Network Security Group to associate with the network interface created by this module.
545545
546546
- Applies only when `create_network_interface` is `true`.
547-
- If omitted and `create_network_interface` is `true`, this module creates and associates a default Network Security Group.
547+
- If omitted, no Network Security Group is associated with the module-created network interface.
548548
EOT
549549

550550
default = null

0 commit comments

Comments
 (0)