Skip to content

Commit 53b7fa8

Browse files
committed
feat: update network security group handling and assertions in tests
Signed-off-by: Andre Licht <al@cloudeteer.de>
1 parent b44cbc9 commit 53b7fa8

4 files changed

Lines changed: 13 additions & 64 deletions

File tree

README.md

Lines changed: 1 addition & 10 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)
@@ -380,14 +379,6 @@ Type: `bool`
380379

381380
Default: `true`
382381

383-
### <a name="input_create_network_security_group"></a> [create\_network\_security\_group](#input\_create\_network\_security\_group)
384-
385-
Description: Create (`true`) and associate a default Network Security Group for the module-created network interface when `network_security_group_id` is omitted. If disabled (`false`) and `network_security_group_id` is omitted, no Network Security Group is associated with the module-created network interface.
386-
387-
Type: `bool`
388-
389-
Default: `true`
390-
391382
### <a name="input_create_public_ip_address"></a> [create\_public\_ip\_address](#input\_create\_public\_ip\_address)
392383

393384
Description: If set to `true` a Azure public IP address will be created and assigned to the default network interface.
@@ -675,7 +666,7 @@ Default: `null`
675666
Description: The resource ID of an existing Azure Network Security Group to associate with the network interface created by this module.
676667

677668
- Applies only when `create_network_interface` is `true`.
678-
- If omitted and both `create_network_interface` and `create_network_security_group` are `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.
679670

680671
Type: `string`
681672

r-network.tf

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,8 @@ 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-
)
11-
12-
}
13-
14-
resource "azurerm_network_security_group" "this" {
6+
network_security_group_id = var.network_security_group_id
157

16-
count = var.create_network_interface && var.create_network_security_group && 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
228
}
239

2410
resource "azurerm_network_interface" "this" {
@@ -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 && (var.create_network_security_group || var.network_security_group_id != null) ? 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: 9 additions & 31 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

@@ -70,53 +65,36 @@ run "should_use_existing_network_security_group_for_created_network_interface" {
7065
subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/snet"
7166
}
7267

73-
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."
76-
}
77-
7868
assert {
7969
condition = length(azurerm_network_interface_security_group_association.this) == 1
8070
error_message = "Expected the created network interface to have a network security group association when network_security_group_id is provided."
8171
}
8272
}
8373

84-
run "should_not_create_or_associate_network_security_group_when_disabled" {
74+
run "should_not_associate_network_security_group_when_not_provided" {
8575
command = plan
8676

8777
variables {
88-
create_network_security_group = false
89-
subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/snet"
90-
}
91-
92-
assert {
93-
condition = length(azurerm_network_security_group.this) == 0
94-
error_message = "Expected no network security group to be created when create_network_security_group is disabled."
78+
subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/snet"
9579
}
9680

9781
assert {
9882
condition = length(azurerm_network_interface_security_group_association.this) == 0
99-
error_message = "Expected no network security group association when create_network_security_group is disabled and no network_security_group_id is provided."
83+
error_message = "Expected no network security group association when no network_security_group_id is provided."
10084
}
10185
}
10286

103-
run "should_associate_existing_network_security_group_when_default_creation_disabled" {
87+
run "should_associate_existing_network_security_group_when_provided" {
10488
command = plan
10589

10690
variables {
107-
create_network_security_group = false
108-
network_security_group_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/networkSecurityGroups/nsg-existing"
109-
subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/snet"
110-
}
111-
112-
assert {
113-
condition = length(azurerm_network_security_group.this) == 0
114-
error_message = "Expected no network security group to be created when create_network_security_group is disabled."
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"
11593
}
11694

11795
assert {
11896
condition = length(azurerm_network_interface_security_group_association.this) == 1
119-
error_message = "Expected the provided network_security_group_id to still be associated when default network security group creation is disabled."
97+
error_message = "Expected the provided network_security_group_id to be associated with the created network interface."
12098
}
12199
}
122100

variables.tf

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@ variable "create_network_interface" {
194194
default = true
195195
}
196196

197-
variable "create_network_security_group" {
198-
description = "Create (`true`) and associate a default Network Security Group for the module-created network interface when `network_security_group_id` is omitted. If disabled (`false`) and `network_security_group_id` is omitted, no Network Security Group is associated with the module-created network interface."
199-
type = bool
200-
default = true
201-
}
202-
203197
variable "create_public_ip_address" {
204198
description = "If set to `true` a Azure public IP address will be created and assigned to the default network interface."
205199
default = false
@@ -550,7 +544,7 @@ variable "network_security_group_id" {
550544
The resource ID of an existing Azure Network Security Group to associate with the network interface created by this module.
551545
552546
- Applies only when `create_network_interface` is `true`.
553-
- If omitted and both `create_network_interface` and `create_network_security_group` are `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.
554548
EOT
555549

556550
default = null

0 commit comments

Comments
 (0)