This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_opentelekomcloud_dns_ptrrecord_v2.go
272 lines (232 loc) · 7.52 KB
/
resource_opentelekomcloud_dns_ptrrecord_v2.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package opentelekomcloud
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/common/tags"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/ptrrecords"
)
func resourceDNSPtrRecordV2() *schema.Resource {
return &schema.Resource{
Create: resourceDNSPtrRecordV2Create,
Read: resourceDNSPtrRecordV2Read,
Update: resourceDNSPtrRecordV2Update,
Delete: resourceDNSPtrRecordV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"floatingip_id": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
ForceNew: false,
ValidateFunc: validation.IntBetween(300, 2147483647),
},
"tags": tagsSchema(),
"address": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceDNSPtrRecordV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region := GetRegion(d, config)
dnsClient, err := config.dnsV2Client(region)
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud DNS client: %s", err)
}
tagmap := d.Get("tags").(map[string]interface{})
taglist := []ptrrecords.Tag{}
for k, v := range tagmap {
tag := ptrrecords.Tag{
Key: k,
Value: v.(string),
}
taglist = append(taglist, tag)
}
createOpts := ptrrecords.CreateOpts{
PtrName: d.Get("name").(string),
Description: d.Get("description").(string),
TTL: d.Get("ttl").(int),
Tags: taglist,
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
fipID := d.Get("floatingip_id").(string)
n, err := ptrrecords.Create(dnsClient, region, fipID, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud DNS PTR record: %s", err)
}
log.Printf("[DEBUG] Waiting for DNS PTR record (%s) to become available", n.ID)
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Pending: []string{"PENDING_CREATE"},
Refresh: waitForDNSPtrRecord(dnsClient, n.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for PTR record (%s) to become ACTIVE for creation: %s",
n.ID, err)
}
d.SetId(n.ID)
log.Printf("[DEBUG] Created OpenTelekomCloud DNS PTR record %s: %#v", n.ID, n)
return resourceDNSPtrRecordV2Read(d, meta)
}
func resourceDNSPtrRecordV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dnsClient, err := config.dnsV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud DNS client: %s", err)
}
n, err := ptrrecords.Get(dnsClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "ptr_record")
}
log.Printf("[DEBUG] Retrieved PTR record %s: %#v", d.Id(), n)
// Obtain relevant info from parsing the ID
fipID, err := parseDNSV2PtrRecordID(d.Id())
if err != nil {
return err
}
d.Set("name", n.PtrName)
d.Set("description", n.Description)
d.Set("floatingip_id", fipID)
d.Set("ttl", n.TTL)
d.Set("address", n.Address)
// save tags
resourceTags, err := tags.Get(dnsClient, "DNS-ptr_record", d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error fetching OpenTelekomCloud DNS ptr record tags: %s", err)
}
tagmap := tagsToMap(resourceTags.Tags)
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("Error saving tags for OpenTelekomCloud DNS ptr record %s: %s", d.Id(), err)
}
return nil
}
func resourceDNSPtrRecordV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region := GetRegion(d, config)
dnsClient, err := config.dnsV2Client(region)
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud DNS client: %s", err)
}
tagmap := d.Get("tags").(map[string]interface{})
taglist := []ptrrecords.Tag{}
for k, v := range tagmap {
tag := ptrrecords.Tag{
Key: k,
Value: v.(string),
}
taglist = append(taglist, tag)
}
createOpts := ptrrecords.CreateOpts{
PtrName: d.Get("name").(string),
Description: d.Get("description").(string),
TTL: d.Get("ttl").(int),
Tags: taglist,
}
log.Printf("[DEBUG] Update Options: %#v", createOpts)
fipID := d.Get("floatingip_id").(string)
n, err := ptrrecords.Create(dnsClient, region, fipID, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenTelekomCloud DNS PTR record: %s", err)
}
log.Printf("[DEBUG] Waiting for DNS PTR record (%s) to become available", n.ID)
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Pending: []string{"PENDING_CREATE"},
Refresh: waitForDNSPtrRecord(dnsClient, n.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for PTR record (%s) to become ACTIVE for update: %s",
n.ID, err)
}
log.Printf("[DEBUG] Updated OpenTelekomCloud DNS PTR record %s: %#v", n.ID, n)
return resourceDNSPtrRecordV2Read(d, meta)
}
func resourceDNSPtrRecordV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dnsClient, err := config.dnsV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud DNS client: %s", err)
}
err = ptrrecords.Delete(dnsClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenTelekomCloud DNS PTR record: %s", err)
}
log.Printf("[DEBUG] Waiting for DNS PTR record (%s) to be deleted", d.Id())
stateConf := &resource.StateChangeConf{
Target: []string{"DELETED"},
Pending: []string{"ACTIVE", "PENDING_DELETE", "ERROR"},
Refresh: waitForDNSPtrRecord(dnsClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for PTR record (%s) to become DELETED for deletion: %s",
d.Id(), err)
}
d.SetId("")
return nil
}
func waitForDNSPtrRecord(dnsClient *golangsdk.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
ptrrecord, err := ptrrecords.Get(dnsClient, id).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return ptrrecord, "DELETED", nil
}
return nil, "", err
}
log.Printf("[DEBUG] OpenTelekomCloud DNS PTR record (%s) current status: %s", ptrrecord.ID, ptrrecord.Status)
return ptrrecord, ptrrecord.Status, nil
}
}
// PTR record ID, which is in {region}:{floatingip_id} format
func parseDNSV2PtrRecordID(id string) (string, error) {
idParts := strings.Split(id, ":")
if len(idParts) != 2 {
return "", fmt.Errorf("Unable to determine DNS PTR record ID from raw ID: %s", id)
}
fipID := idParts[1]
return fipID, nil
}