Skip to content

Commit c3309f3

Browse files
committed
Merge branch 'feat/AZ-1568' into 'master'
feat(AZ-1568): add variable allowed_cidrs Closes AZ-1568 See merge request claranet/projects/cloud/azure/terraform/modules/db-sql!157
2 parents bf29f0d + c1577bd commit c3309f3

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ only along with [Firewall rules](https://docs.microsoft.com/en-us/azure/sql-data
99
and [Diagnostic settings](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-metrics-diag-logging)
1010
enabled.
1111

12+
## Migration from 8.x.x to 8.3.x
13+
14+
The variable `allowed_cidr_list` variable has been renamed to `allowed_cidrs` and the resource `azurerm_mssql_firewall_rule.main` is now using a `for_each` loop to create the firewall rules (was using a `count` before).
15+
In order to migrate your state without recreating the firewall rules, you can run :
16+
17+
```bash
18+
tofu state rm module.sql.azurerm_mssql_firewall_rule.main
19+
```
20+
21+
Then add to your IAC this code :
22+
23+
```hcl
24+
import {
25+
for_each = local.allowed_cidrs
26+
to = module.sql.azurerm_mssql_firewall_rule.main[each.key]
27+
id = "${nonsensitive(module.sql.resource.id)}/firewallRules/${each.key}"
28+
}
29+
```
30+
1231
<!-- BEGIN_TF_DOCS -->
1332
## Global versioning rule for Claranet Azure modules
1433

@@ -72,6 +91,8 @@ module "sql_elastic" {
7291
capacity = 2
7392
}
7493
94+
allowed_cidrs = ["1.2.3.4/32", "5.6.7.8/16"]
95+
7596
logs_destinations_ids = [
7697
module.logs.id,
7798
module.logs.storage_account_id,
@@ -130,6 +151,11 @@ module "sql_single" {
130151
131152
elastic_pool_enabled = false
132153
154+
allowed_cidrs = {
155+
"foo" = "1.2.3.4/32"
156+
"bar" = "5.6.7.8/16"
157+
}
158+
133159
logs_destinations_ids = [
134160
module.logs.id,
135161
module.logs.storage_account_id,
@@ -211,7 +237,7 @@ module "sql_single" {
211237
| administrator\_login | Administrator login for SQL Server. | `string` | n/a | yes |
212238
| administrator\_password | Administrator password for SQL Server. | `string` | n/a | yes |
213239
| alerting\_email\_addresses | List of email addresses to send reports for threat detection and vulnerability assessment. | `list(string)` | `[]` | no |
214-
| allowed\_cidr\_list | Allowed IP addresses to access the server in CIDR format. Default to all Azure services. | `list(string)` | <pre>[<br/> "0.0.0.0/32"<br/>]</pre> | no |
240+
| allowed\_cidrs | List/map of allowed CIDR ranges to access the SQL server. Default to all Azure services. | `any` | <pre>{<br/> "azure-services": "0.0.0.0/32"<br/>}</pre> | no |
215241
| allowed\_subnets\_ids | List of Subnet ID to allow to connect to the SQL Instance. | `list(string)` | `[]` | no |
216242
| azuread\_administrator | Azure AD Administrator configuration block of this SQL Server. | <pre>object({<br/> login_username = optional(string)<br/> object_id = optional(string)<br/> tenant_id = optional(string)<br/> azuread_authentication_only = optional(bool)<br/> })</pre> | `null` | no |
217243
| backup\_retention | Definition of long term backup retention for all the databases in this SQL Server. | <pre>object({<br/> weekly_retention = optional(number)<br/> monthly_retention = optional(number)<br/> yearly_retention = optional(number)<br/> week_of_year = optional(number)<br/> })</pre> | `{}` | no |

examples/main/modules.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module "sql_elastic" {
3030
capacity = 2
3131
}
3232

33+
allowed_cidrs = ["1.2.3.4/32", "5.6.7.8/16"]
34+
3335
logs_destinations_ids = [
3436
module.logs.id,
3537
module.logs.storage_account_id,
@@ -88,6 +90,11 @@ module "sql_single" {
8890

8991
elastic_pool_enabled = false
9092

93+
allowed_cidrs = {
94+
"foo" = "1.2.3.4/32"
95+
"bar" = "5.6.7.8/16"
96+
}
97+
9198
logs_destinations_ids = [
9299
module.logs.id,
93100
module.logs.storage_account_id,

r-sql.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ resource "azurerm_mssql_server" "main" {
3636
}
3737

3838
resource "azurerm_mssql_firewall_rule" "main" {
39-
count = try(length(var.allowed_cidr_list), 0)
39+
for_each = can(tomap(var.allowed_cidrs)) ? tomap(var.allowed_cidrs) : { for idx, cidr in var.allowed_cidrs : "rule-${idx}" => cidr }
4040

41-
name = "rule-${count.index}"
41+
name = each.key
4242
server_id = azurerm_mssql_server.main.id
4343

44-
start_ip_address = cidrhost(var.allowed_cidr_list[count.index], 0)
45-
end_ip_address = cidrhost(var.allowed_cidr_list[count.index], -1)
44+
start_ip_address = cidrhost(each.value, 0)
45+
end_ip_address = cidrhost(each.value, -1)
4646
}
4747

4848
resource "azurerm_mssql_elasticpool" "main" {

variables.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ variable "server_version" {
3434
default = "12.0"
3535
}
3636

37-
variable "allowed_cidr_list" {
38-
description = "Allowed IP addresses to access the server in CIDR format. Default to all Azure services."
39-
type = list(string)
40-
default = ["0.0.0.0/32"]
37+
variable "allowed_cidrs" {
38+
description = "List/map of allowed CIDR ranges to access the SQL server. Default to all Azure services."
39+
type = any
40+
nullable = false
41+
default = { azure-services = "0.0.0.0/32" }
42+
43+
validation {
44+
condition = can(tomap(var.allowed_cidrs)) || can(tolist(var.allowed_cidrs))
45+
error_message = "The `allowed_cidrs` argument must either be list(string) or map(string) of CIDRs."
46+
}
4147
}
4248

4349
variable "elastic_pool_enabled" {
@@ -336,4 +342,4 @@ variable "primary_user_assigned_identity_id" {
336342
description = "Specifies the primary user managed identity id. Required if type within the identity block is set to either SystemAssigned, UserAssigned or UserAssigned and should be set at same time as setting identity_ids."
337343
type = string
338344
default = null
339-
}
345+
}

0 commit comments

Comments
 (0)