forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_dns_zone.go
165 lines (136 loc) · 4.07 KB
/
data_source_dns_zone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package azurerm
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmDnsZone() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDnsZoneRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"resource_group_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"number_of_record_sets": {
Type: schema.TypeInt,
Computed: true,
},
"max_number_of_record_sets": {
Type: schema.TypeInt,
Computed: true,
},
"name_servers": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"zone_type": {
Type: schema.TypeString,
Computed: true,
},
"registration_virtual_network_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"resolution_virtual_network_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsForDataSourceSchema(),
},
}
}
func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).zonesClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
var (
resp dns.Zone
err error
)
if resourceGroup != "" {
resp, err = client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: DNS Zone %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading DNS Zone %q (Resource Group %q): %+v", name, resourceGroup, err)
}
} else {
rgClient := meta.(*ArmClient).resourceGroupsClient
resp, resourceGroup, err = findZone(client, rgClient, ctx, name)
if err != nil {
return err
}
if resourceGroup == "" {
return fmt.Errorf("Error: DNS Zone %q was not found", name)
}
}
d.SetId(*resp.ID)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if props := resp.ZoneProperties; props != nil {
d.Set("number_of_record_sets", props.NumberOfRecordSets)
d.Set("max_number_of_record_sets", props.MaxNumberOfRecordSets)
d.Set("zone_type", props.ZoneType)
registrationVNets := make([]string, 0)
if rvns := props.RegistrationVirtualNetworks; rvns != nil {
for _, rvn := range *rvns {
registrationVNets = append(registrationVNets, *rvn.ID)
}
}
if err := d.Set("registration_virtual_network_ids", registrationVNets); err != nil {
return err
}
resolutionVNets := make([]string, 0)
if rvns := props.ResolutionVirtualNetworks; rvns != nil {
for _, rvn := range *rvns {
resolutionVNets = append(resolutionVNets, *rvn.ID)
}
}
if err := d.Set("resolution_virtual_network_ids", resolutionVNets); err != nil {
return err
}
nameServers := make([]string, 0)
if ns := props.NameServers; ns != nil {
nameServers = *ns
}
if err := d.Set("name_servers", nameServers); err != nil {
return err
}
}
flattenAndSetTags(d, resp.Tags)
return nil
}
func findZone(client dns.ZonesClient, rgClient resources.GroupsClient, ctx context.Context, name string) (dns.Zone, string, error) {
groups, err := rgClient.List(ctx, "", nil)
if err != nil {
return dns.Zone{}, "", fmt.Errorf("Error listing Resource Groups: %+v", err)
}
for _, g := range groups.Values() {
resourceGroup := *g.Name
zones, err := client.ListByResourceGroup(ctx, resourceGroup, nil)
if err != nil {
return dns.Zone{}, "", fmt.Errorf("Error listing DNS Zones (Resource Group: %s): %+v", resourceGroup, err)
}
for _, z := range zones.Values() {
if *z.Name == name {
return z, resourceGroup, nil
}
}
}
return dns.Zone{}, "", nil
}