Skip to content

Commit bdf4d35

Browse files
authored
Merge pull request #9 from data-platform-hq/refactor
feat: refactor conditions; support a records
2 parents 9e330c6 + f85ebaf commit bdf4d35

File tree

4 files changed

+52
-51
lines changed

4 files changed

+52
-51
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ No modules.
6060

6161
| Name | Type |
6262
|------|------|
63+
| [azurerm_private_dns_a_record.example](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_dns_a_record) | resource |
6364
| [azurerm_private_dns_zone.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_dns_zone) | resource |
6465
| [azurerm_private_dns_zone_virtual_network_link.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_dns_zone_virtual_network_link) | resource |
6566

6667
## Inputs
6768

6869
| Name | Description | Type | Default | Required |
6970
|------|-------------|------|---------|:--------:|
70-
| <a name="input_custom_dns_zone_vnet_link_name"></a> [custom\_dns\_zone\_vnet\_link\_name](#input\_custom\_dns\_zone\_vnet\_link\_name) | The name of the Private DNS Zone Virtual Network Link | `string` | `null` | no |
71-
| <a name="input_dns_zone_name"></a> [dns\_zone\_name](#input\_dns\_zone\_name) | Name of Private DNS Zone | `string` | `""` | no |
72-
| <a name="input_external_dns_zone_name"></a> [external\_dns\_zone\_name](#input\_external\_dns\_zone\_name) | Name of Imported Private DNS Zone. Provide value in case creation of new Private DNS Zone is disabled | `string` | `""` | no |
73-
| <a name="input_prefix"></a> [prefix](#input\_prefix) | Custom prefix to add to resource name | `string` | `""` | no |
74-
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | Azure location | `string` | n/a | yes |
71+
| <a name="input_create_private_dns_zone"></a> [create\_private\_dns\_zone](#input\_create\_private\_dns\_zone) | Boolean flag that determines whether Private DNS Zones is created by this module | `bool` | `true` | no |
72+
| <a name="input_dns_zone_name"></a> [dns\_zone\_name](#input\_dns\_zone\_name) | The name of the Private DNS Zone that needs to be created or linked to the virtual network | `string` | n/a | yes |
73+
| <a name="input_private_dns_a_records"></a> [private\_dns\_a\_records](#input\_private\_dns\_a\_records) | List of objects with parameters to create A Record in Private DNS Zone | <pre>list(object({<br> name = string<br> ttl = optional(number, 300)<br> records = list(string)<br> }))</pre> | `[]` | no |
74+
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | Resource group where Private DNS zone would be created or it is already exists | `string` | n/a | yes |
7575
| <a name="input_tags"></a> [tags](#input\_tags) | Resource tags | `map(string)` | `{}` | no |
7676
| <a name="input_vnet_map"></a> [vnet\_map](#input\_vnet\_map) | Map of Virtual Network Name to Id, used to create VNet Link to Private DNS | `map(string)` | `{}` | no |
7777

@@ -80,9 +80,9 @@ No modules.
8080
| Name | Description |
8181
|------|-------------|
8282
| <a name="output_id"></a> [id](#output\_id) | Private DNS Zone Id |
83-
| <a name="output_link_id"></a> [link\_id](#output\_link\_id) | List of Virtual Network Link Ids |
8483
| <a name="output_name"></a> [name](#output\_name) | Private DNS Zone Name |
8584
| <a name="output_resource_group"></a> [resource\_group](#output\_resource\_group) | Private DNS Zone Resource Group |
85+
| <a name="output_vnet_link_name_to_id_map"></a> [vnet\_link\_name\_to\_id\_map](#output\_vnet\_link\_name\_to\_id\_map) | Map of Virtual Network Link names to it's ids |
8686
<!-- END_TF_DOCS -->
8787

8888
## License

main.tf

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
locals {
2-
prefix = length(var.prefix) == 0 ? "" : var.prefix
3-
private_dns_zone_vnet_link = var.custom_dns_zone_vnet_link_name == null ? "link-${local.prefix}" : "${local.prefix}-${var.custom_dns_zone_vnet_link_name}"
4-
}
5-
61
resource "azurerm_private_dns_zone" "this" {
7-
count = length(var.dns_zone_name) == 0 ? 0 : 1
2+
count = var.create_private_dns_zone ? 1 : 0
83

94
name = var.dns_zone_name
105
resource_group_name = var.resource_group
116
tags = var.tags
127
}
138

9+
resource "azurerm_private_dns_a_record" "example" {
10+
for_each = {
11+
for object in var.private_dns_a_records :
12+
object.name => object
13+
}
14+
15+
name = each.value.name
16+
zone_name = try(azurerm_private_dns_zone.this[0].name, var.dns_zone_name)
17+
resource_group_name = try(azurerm_private_dns_zone.this[0].resource_group_name, var.resource_group)
18+
ttl = each.value.ttl
19+
records = each.value.records
20+
}
21+
1422
resource "azurerm_private_dns_zone_virtual_network_link" "this" {
1523
for_each = var.vnet_map
1624

17-
name = "${local.private_dns_zone_vnet_link}-${each.key}"
18-
private_dns_zone_name = length(var.dns_zone_name) == 0 ? var.external_dns_zone_name : azurerm_private_dns_zone.this[0].name
19-
resource_group_name = var.resource_group
25+
name = "vnet-link-${each.key}"
26+
private_dns_zone_name = try(azurerm_private_dns_zone.this[0].name, var.dns_zone_name)
27+
resource_group_name = try(azurerm_private_dns_zone.this[0].resource_group_name, var.resource_group)
2028
virtual_network_id = each.value
2129
tags = var.tags
22-
23-
lifecycle {
24-
precondition {
25-
condition = alltrue([length(var.dns_zone_name) == 0, length(var.external_dns_zone_name) == 0]) ? false : true
26-
error_message = "Provide either 'dns_zone_name' value to create new Private DNS Zone or 'external_dns_zone_name' value to create link with already existing Private DNS Zone"
27-
}
28-
}
2930
}

outputs.tf

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
output "id" {
2-
value = length(var.dns_zone_name) == 0 ? "" : azurerm_private_dns_zone.this[0].id
2+
value = try(azurerm_private_dns_zone.this[0].id, null)
33
description = "Private DNS Zone Id"
44
}
55

66
output "name" {
7-
value = length(var.dns_zone_name) == 0 ? "" : azurerm_private_dns_zone.this[0].name
7+
value = try(azurerm_private_dns_zone.this[0].name, null)
88
description = "Private DNS Zone Name"
99
}
1010

1111
output "resource_group" {
12-
value = length(var.dns_zone_name) == 0 ? "" : azurerm_private_dns_zone.this[0].resource_group_name
12+
value = try(azurerm_private_dns_zone.this[0].resource_group_name, null)
1313
description = "Private DNS Zone Resource Group"
1414
}
1515

16-
output "link_id" {
17-
value = length(var.vnet_map) == 0 ? [] : [for vnet_link in azurerm_private_dns_zone_virtual_network_link.this : vnet_link.id]
18-
description = "List of Virtual Network Link Ids"
16+
output "vnet_link_name_to_id_map" {
17+
value = {
18+
for k, v in var.vnet_map :
19+
azurerm_private_dns_zone_virtual_network_link.this[k].name => azurerm_private_dns_zone_virtual_network_link.this[k].id
20+
}
21+
description = "Map of Virtual Network Link names to it's ids"
1922
}

variables.tf

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
1-
variable "resource_group" {
1+
variable "dns_zone_name" {
22
type = string
3-
description = "Azure location"
3+
description = "The name of the Private DNS Zone that needs to be created or linked to the virtual network"
44
}
55

6-
variable "custom_dns_zone_vnet_link_name" {
6+
variable "resource_group" {
77
type = string
8-
description = "The name of the Private DNS Zone Virtual Network Link"
9-
default = null
8+
description = "Resource group where Private DNS zone would be created or it is already exists"
109
}
1110

12-
variable "tags" {
13-
type = map(string)
14-
description = "Resource tags"
15-
default = {}
11+
variable "create_private_dns_zone" {
12+
type = bool
13+
description = "Boolean flag that determines whether Private DNS Zones is created by this module"
14+
default = true
1615
}
1716

18-
variable "prefix" {
19-
type = string
20-
description = "Custom prefix to add to resource name"
21-
default = ""
17+
variable "private_dns_a_records" {
18+
type = list(object({
19+
name = string
20+
ttl = optional(number, 300)
21+
records = list(string)
22+
}))
23+
description = "List of objects with parameters to create A Record in Private DNS Zone"
24+
default = []
2225
}
2326

24-
variable "dns_zone_name" {
25-
type = string
26-
description = "Name of Private DNS Zone"
27-
default = ""
27+
variable "tags" {
28+
type = map(string)
29+
description = "Resource tags"
30+
default = {}
2831
}
2932

3033
variable "vnet_map" {
3134
type = map(string)
3235
description = "Map of Virtual Network Name to Id, used to create VNet Link to Private DNS"
3336
default = {}
3437
}
35-
36-
variable "external_dns_zone_name" {
37-
type = string
38-
description = "Name of Imported Private DNS Zone. Provide value in case creation of new Private DNS Zone is disabled"
39-
default = ""
40-
}

0 commit comments

Comments
 (0)