forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_zone_data_source.go
137 lines (114 loc) · 3.58 KB
/
dns_zone_data_source.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
package dns
import (
"context"
"fmt"
"time"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmDnsZone() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDnsZoneRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
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,
},
"tags": tags.SchemaDataSource(),
},
}
}
func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.ZonesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
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 {
resp, resourceGroup, err = findZone(client, ctx, name)
if err != nil {
return err
}
if resp.ID == nil || *resp.ID == "" {
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)
nameServers := make([]string, 0)
if ns := props.NameServers; ns != nil {
nameServers = *ns
}
if err := d.Set("name_servers", nameServers); err != nil {
return err
}
}
return tags.FlattenAndSet(d, resp.Tags)
}
func findZone(client *dns.ZonesClient, ctx context.Context, name string) (dns.Zone, string, error) {
zonesIterator, err := client.ListComplete(ctx, nil)
if err != nil {
return dns.Zone{}, "", fmt.Errorf("listing DNS Zones: %+v", err)
}
var found *dns.Zone
for zonesIterator.NotDone() {
zone := zonesIterator.Value()
if *zone.Name == name {
if found != nil {
return dns.Zone{}, "", fmt.Errorf("found multiple DNS zones with name %q, please specify the resource group", name)
}
found = &zone
}
if err := zonesIterator.NextWithContext(ctx); err != nil {
return dns.Zone{}, "", fmt.Errorf("listing DNS Zones: %+v", err)
}
}
if found == nil {
return dns.Zone{}, "", fmt.Errorf("could not find DNS zone with name: %q", name)
}
id, _ := parse.DnsZoneID(*found.ID)
return *found, id.ResourceGroup, nil
}