Skip to content

Commit 9463175

Browse files
authored
Added new module for nsg and nsr (#423)
* Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Added new module for nsg and nsr * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.me and added validations * Updated readme.md * Updated readme.md * Updated readme.md * Updated readme.md * Added module to release-please
1 parent 5d611a3 commit 9463175

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

modules/azure-nsg-nsr/README.MD

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## Requirements
2+
3+
| Name | Version |
4+
|------|---------|
5+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.7.0 |
6+
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >= 4.16.0 |
7+
8+
---
9+
10+
## Providers
11+
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | >= 4.16.0 |
15+
16+
---
17+
18+
## Resources
19+
20+
| Name | Type |
21+
|------|------|
22+
| [azurerm_network_security_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/4.16.0/docs/resources/network_security_group) | resource |
23+
| [azurerm_network_security_rule.this](https://registry.terraform.io/providers/hashicorp/azurerm/4.16.0/docs/resources/network_security_rule) | resource |
24+
25+
---
26+
27+
## Inputs
28+
29+
### Network Security Group Configuration
30+
31+
| Name | Description | Type | Default | Required |
32+
|------|-------------|------|---------|:--------:|
33+
| `name` | The name of the Network Security Group. | `string` | - | yes |
34+
| `location` | The Azure region where the NSG will be created. | `string` | - | yes |
35+
| `resource_group_name` | The name of the resource group in which to create the NSG. | `string` | - | yes |
36+
| `tags` | A map of tags to assign to the NSG. | `map(string)` | `{}` | no |
37+
38+
### Network Security Rules
39+
40+
| Name | Description | Type | Default | Required |
41+
|------|-------------|------|---------|:--------:|
42+
| `name` | The name of the security rule. | `string` | - | yes |
43+
| `priority` | The priority of the rule. | `number` | - | yes |
44+
| `direction` | The direction of the rule (Inbound/Outbound). | `string` | - | yes |
45+
| `access` | The access type (Allow/Deny). | `string` | - | yes |
46+
| `protocol` | The network protocol (Tcp, Udp, Icmp, etc.). | `string` | - | yes |
47+
| `source_port_range` | The source port range. | `string` | - | show bellow |
48+
| `source_port_ranges` | A list of source port ranges. | `list(string)` | - | show bellow |
49+
| `destination_port_range` | The destination port range. | `string` | - | show bellow |
50+
| `destination_port_ranges` | A list of destination port ranges. | `list(string)` | - | show bellow |
51+
| `source_address_prefix` | The source address prefix. | `string` | - | show bellow |
52+
| `source_address_prefixes` | A list of source address prefixes. | `list(string)` | - | show bellow |
53+
| `destination_address_prefix` | The destination address prefix. | `string` | - | show bellow |
54+
| `destination_address_prefixes` | A list of destination address prefixes. | `list(string)` | - | show bellow |
55+
56+
**source_port_range** and **source_port_ranges** are required to have at least one of them but you can't have both at the same time.
57+
58+
**destination_port_range** and **destination_port_ranges** are required to have at least one of them but you can't have both at the same time.
59+
60+
**source_address_prefix** and **source_address_prefixes** are required to have at least one of them but you can't have both at the same time.
61+
62+
**destination_address_prefix** and **destination_address_prefixes** are required to have at least one of them but you can't have both at the same time.
63+
64+
## Example Usage
65+
66+
```hcl
67+
68+
nsg = {
69+
name = "example-nsg"
70+
location = "East US"
71+
resource_group_name = "example-rg"
72+
tags = {
73+
env = "Production"
74+
}
75+
}
76+
77+
rules = {
78+
rule1 = {
79+
name = "AllowSSH"
80+
priority = 100
81+
direction = "Inbound"
82+
access = "Allow"
83+
protocol = "Tcp"
84+
source_port_range = "*"
85+
destination_port_range = "22"
86+
source_address_prefix = "10.0.0.0/24"
87+
destination_address_prefix = "*"
88+
}
89+
rule2 = {
90+
name = "AllowHTTP"
91+
priority = 200
92+
direction = "Inbound"
93+
access = "Allow"
94+
protocol = "Tcp"
95+
source_port_range = "*"
96+
destination_port_range = "80"
97+
source_address_prefix = "0.0.0.0/0"
98+
destination_address_prefix = "*"
99+
}
100+
}
101+
```

modules/azure-nsg-nsr/main.tf

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# RESOURCES SECTION
2+
# https://registry.terraform.io/providers/hashicorp/azurerm/4.16.0/docs/resources/network_security_group
3+
resource "azurerm_network_security_group" "this" {
4+
name = var.nsg.name
5+
location = var.nsg.location
6+
resource_group_name = var.nsg.resource_group_name
7+
tags = var.nsg.tags
8+
}
9+
10+
# https://registry.terraform.io/providers/hashicorp/azurerm/4.16.0/docs/resources/network_security_rule
11+
resource "azurerm_network_security_rule" "this" {
12+
for_each = var.rules
13+
name = each.value.name
14+
priority = each.value.priority
15+
direction = each.value.direction
16+
access = each.value.access
17+
protocol = each.value.protocol
18+
source_port_range = each.value.source_port_range
19+
source_port_ranges = each.value.source_port_ranges
20+
destination_port_range = each.value.destination_port_range
21+
destination_port_ranges = each.value.destination_port_ranges
22+
source_address_prefix = each.value.source_address_prefix
23+
source_address_prefixes = each.value.source_address_prefixes
24+
destination_address_prefix = each.value.destination_address_prefix
25+
destination_address_prefixes = each.value.destination_address_prefixes
26+
resource_group_name = var.nsg.resource_group_name
27+
network_security_group_name = var.nsg.name
28+
}

modules/azure-nsg-nsr/outputs.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# OUTPUTS SECTION
2+
output "id" {
3+
value = azurerm_network_security_group.this.id
4+
}

modules/azure-nsg-nsr/variables.tf

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# VARIABLES SECTION
2+
variable "nsg" {
3+
description = "Network Security Group configuration"
4+
type = object({
5+
name = string
6+
location = string
7+
resource_group_name = string
8+
tags = optional(map(string))
9+
})
10+
}
11+
12+
variable "rules" {
13+
description = "Network Security Rule configuration"
14+
type = map(object({
15+
name = string
16+
priority = number
17+
direction = string
18+
access = string
19+
protocol = string
20+
source_port_range = optional(string)
21+
source_port_ranges = optional(list(string))
22+
destination_port_range = optional(string)
23+
destination_port_ranges = optional(list(string))
24+
source_address_prefix = optional(string)
25+
source_address_prefixes = optional(list(string))
26+
destination_address_prefix = optional(string)
27+
destination_address_prefixes = optional(list(string))
28+
}))
29+
validation {
30+
condition = alltrue([
31+
for rule in var.rules :
32+
(rule.source_port_range == null || rule.source_port_ranges == null)
33+
|| (rule.source_port_range != null && rule.source_port_ranges == null)
34+
|| (rule.source_port_range == null && rule.source_port_ranges != null)
35+
])
36+
error_message = "Only one of 'source_port_range' or 'source_port_ranges' can be specified for each rule."
37+
}
38+
validation {
39+
condition = alltrue([
40+
for rule in var.rules :
41+
(rule.destination_port_range == null || rule.destination_port_ranges == null)
42+
|| (rule.destination_port_range != null && rule.destination_port_ranges == null)
43+
|| (rule.destination_port_range == null && rule.destination_port_ranges != null)
44+
])
45+
error_message = "Only one of 'destination_port_range' or 'destination_port_ranges' can be specified for each rule."
46+
}
47+
validation {
48+
condition = alltrue([
49+
for rule in var.rules :
50+
(rule.source_address_prefix == null || rule.source_address_prefixes == null)
51+
|| (rule.source_address_prefix != null && rule.source_address_prefixes == null)
52+
|| (rule.source_address_prefix == null && rule.source_address_prefixes != null)
53+
])
54+
error_message = "Only one of 'source_adress_prefix' or 'source_adress_prefixes' can be specified for each rule."
55+
}
56+
validation {
57+
condition = alltrue([
58+
for rule in var.rules :
59+
(rule.destination_address_prefix == null || rule.destination_address_prefixes == null)
60+
|| (rule.destination_address_prefix != null && rule.destination_address_prefixes == null)
61+
|| (rule.destination_address_prefix == null && rule.destination_address_prefixes != null)
62+
])
63+
error_message = "Only one of 'destination_adress_prefix' or 'destination_adress_prefixes' can be specified for each rule."
64+
}
65+
}

modules/azure-nsg-nsr/versions.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.7.0"
3+
4+
required_providers {
5+
azurerm = {
6+
source = "hashicorp/azurerm"
7+
version = ">= 4.16.0"
8+
}
9+
}
10+
}

release-please-config.json

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
},
7575
"modules/azure-public-prefix": {
7676
"package-name": "azure-public-prefix"
77+
},
78+
"modules/azure-nsg-nsr": {
79+
"package-name": "azure-nsg-nsr"
7780
}
7881
}
7982
}

0 commit comments

Comments
 (0)