forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_dns_ns_record.go
231 lines (189 loc) · 5.67 KB
/
resource_arm_dns_ns_record.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package azurerm
import (
"fmt"
"net/http"
"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmDnsNsRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsNsRecordCreateOrUpdate,
Read: resourceArmDnsNsRecordRead,
Update: resourceArmDnsNsRecordCreateOrUpdate,
Delete: resourceArmDnsNsRecordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": resourceGroupNameSchema(),
"zone_name": {
Type: schema.TypeString,
Required: true,
},
"records": {
Type: schema.TypeList,
//TODO: add `Required: true` once we remove the `record` attribute
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: []string{"record"},
},
"record": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Deprecated: "This field has been replaced by `records`",
ConflictsWith: []string{"records"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nsdname": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"ttl": {
Type: schema.TypeInt,
Required: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmDnsNsRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
dnsClient := meta.(*ArmClient).dnsClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)
ttl := int64(d.Get("ttl").(int))
tags := d.Get("tags").(map[string]interface{})
records, err := expandAzureRmDnsNsRecords(d)
if err != nil {
return err
}
parameters := dns.RecordSet{
Name: &name,
RecordSetProperties: &dns.RecordSetProperties{
Metadata: expandTags(tags),
TTL: &ttl,
NsRecords: &records,
},
}
eTag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
resp, err := dnsClient.CreateOrUpdate(ctx, resGroup, zoneName, name, dns.NS, parameters, eTag, ifNoneMatch)
if err != nil {
return err
}
if resp.ID == nil {
return fmt.Errorf("Cannot read DNS NS Record %s (resource group %s) ID", name, resGroup)
}
d.SetId(*resp.ID)
return resourceArmDnsNsRecordRead(d, meta)
}
func resourceArmDnsNsRecordRead(d *schema.ResourceData, meta interface{}) error {
dnsClient := meta.(*ArmClient).dnsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["NS"]
zoneName := id.Path["dnszones"]
resp, err := dnsClient.Get(ctx, resGroup, zoneName, name, dns.NS)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading DNS NS record %s: %+v", name, err)
}
d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)
if err := d.Set("records", flattenAzureRmDnsNsRecords(resp.NsRecords)); err != nil {
return fmt.Errorf("Error settings `records`: %+v", err)
}
//TODO: remove this once we remove the `record` attribute
if err := d.Set("record", flattenAzureRmDnsNsRecordsSet(resp.NsRecords)); err != nil {
return fmt.Errorf("Error settings `record`: %+v", err)
}
flattenAndSetTags(d, resp.Metadata)
return nil
}
func resourceArmDnsNsRecordDelete(d *schema.ResourceData, meta interface{}) error {
dnsClient := meta.(*ArmClient).dnsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["NS"]
zoneName := id.Path["dnszones"]
resp, error := dnsClient.Delete(ctx, resGroup, zoneName, name, dns.NS, "")
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error deleting DNS NS Record %s: %+v", name, error)
}
return nil
}
//TODO: remove this once we remove the `record` attribute
func flattenAzureRmDnsNsRecordsSet(records *[]dns.NsRecord) []map[string]interface{} {
results := make([]map[string]interface{}, 0, len(*records))
if records != nil {
for _, record := range *records {
nsRecord := make(map[string]interface{})
nsRecord["nsdname"] = *record.Nsdname
results = append(results, nsRecord)
}
}
return results
}
func flattenAzureRmDnsNsRecords(records *[]dns.NsRecord) []string {
results := make([]string, 0, len(*records))
if records != nil {
for _, record := range *records {
results = append(results, *record.Nsdname)
}
}
return results
}
func expandAzureRmDnsNsRecords(d *schema.ResourceData) ([]dns.NsRecord, error) {
var records []dns.NsRecord
//TODO: remove this once we remove the `record` attribute
if d.HasChange("records") || !d.HasChange("record") {
recordStrings := d.Get("records").([]interface{})
records = make([]dns.NsRecord, len(recordStrings))
for i, v := range recordStrings {
record := v.(string)
nsRecord := dns.NsRecord{
Nsdname: &record,
}
records[i] = nsRecord
}
} else {
recordList := d.Get("record").(*schema.Set).List()
if len(recordList) != 0 {
records = make([]dns.NsRecord, len(recordList))
for i, v := range recordList {
record := v.(map[string]interface{})
nsdname := record["nsdname"].(string)
nsRecord := dns.NsRecord{
Nsdname: &nsdname,
}
records[i] = nsRecord
}
}
}
return records, nil
}