Skip to content

Commit b0bda97

Browse files
author
oleh_mykolaishyn
committed
feat: refactor conditions; support a records
*main.tf: remove suffix; update conditions; add a records *outputs.tf: update conditions *variables.tf: add new variables
1 parent 9e330c6 commit b0bda97

File tree

3 files changed

+46
-45
lines changed

3 files changed

+46
-45
lines changed

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)