-
Notifications
You must be signed in to change notification settings - Fork 642
/
resource_ibm_dns_linked_zone.go
243 lines (214 loc) · 7.9 KB
/
resource_ibm_dns_linked_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
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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package dnsservices
import (
"context"
"fmt"
"strings"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
//"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
DnsLinkedZoneInstanceID = "instance_id"
DnsLinkedZoneName = "name"
DnsLinkedZoneDescription = "description"
DnsLinkedZoneOwnerInstanceID = "owner_instance_id"
DnsLinkedZoneOwnerZoneID = "owner_zone_id"
DnsLinkedZoneLinkedTo = "linked_to"
DnsLinkedZoneState = "state"
DnsLinkedZoneLabel = "label"
DnsLinkedZoneApprovalRequiredBefore = "approval_required_before"
DnsLinkedZoneCreatedOn = "created_on"
DnsLinkedZoneModifiedOn = "modified_on"
// DnsLinkedZoneOwnerInstanceID = "owner_instance_id"
)
func ResourceIBMDNSLinkedZone() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMDNSLinkedZoneCreate,
ReadContext: resourceIBMDNSLinkedZoneRead,
UpdateContext: resourceIBMDNSLinkedZoneUpdate,
DeleteContext: resourceIBMDNSLinkedZoneDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
DnsLinkedZoneInstanceID: {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of a DNS Linked zone.",
},
DnsLinkedZoneName: {
Type: schema.TypeString,
Required: true,
Description: "The name of the DNS Linked zone.",
},
DnsLinkedZoneDescription: {
Type: schema.TypeString,
Optional: true,
Description: "Descriptive text of the DNS Linked zone",
},
DnsLinkedZoneOwnerInstanceID: {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of the owner DNS instance",
},
DnsLinkedZoneOwnerZoneID: {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of the owner DNS zone",
},
DnsLinkedZoneLinkedTo: {
Type: schema.TypeString,
Optional: true,
Description: "The zone that is linked to the DNS Linked zone",
},
DnsLinkedZoneState: {
Type: schema.TypeString,
Computed: true,
Description: "The state of the DNS Linked zone",
},
DnsLinkedZoneLabel: {
Type: schema.TypeString,
Optional: true,
Description: "The label of the DNS Linked zone",
},
DnsLinkedZoneApprovalRequiredBefore: {
Type: schema.TypeString,
Optional: true,
Description: "DNS Linked Approval required before",
},
DnsLinkedZoneCreatedOn: {
Type: schema.TypeString,
Computed: true,
Description: "DNS Linked Zone Creation date",
},
DnsLinkedZoneModifiedOn: {
Type: schema.TypeString,
Computed: true,
Description: "DNS Linked Zone Modification date",
},
},
}
}
func resourceIBMDNSLinkedZoneCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return diag.FromErr(err)
}
var (
description string
label string
)
instanceID := d.Get(DnsLinkedZoneInstanceID).(string)
if v, ok := d.GetOk(DnsLinkedZoneDescription); ok {
description = v.(string)
}
if v, ok := d.GetOk(DnsLinkedZoneLabel); ok {
label = v.(string)
}
ownerInstanceID := d.Get(DnsLinkedZoneOwnerInstanceID).(string)
ownerZoneID := d.Get(DnsLinkedZoneOwnerZoneID).(string)
createLinkedZoneOptions := sess.NewCreateLinkedZoneOptions(instanceID)
createLinkedZoneOptions.SetDescription(description)
createLinkedZoneOptions.SetLabel(label)
createLinkedZoneOptions.SetOwnerInstanceID(ownerInstanceID)
createLinkedZoneOptions.SetOwnerZoneID(ownerZoneID)
mk := "dns_linked_zone_" + instanceID
conns.IbmMutexKV.Lock(mk)
defer conns.IbmMutexKV.Unlock(mk)
resource, response, err := sess.CreateLinkedZone(createLinkedZoneOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error creating DNS Linked zone:%s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s/%s", instanceID, *resource.ID))
return resourceIBMDNSLinkedZoneRead(ctx, d, meta)
}
func resourceIBMDNSLinkedZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return diag.FromErr(err)
}
idSet := strings.Split(d.Id(), "/")
if len(idSet) < 2 {
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/linkedDnsZoneID", d.Id()))
}
instanceID := idSet[0]
linkedDnsZoneID := idSet[1]
getLinkedZoneOptions := sess.NewGetLinkedZoneOptions(instanceID, linkedDnsZoneID)
resource, response, err := sess.GetLinkedZone(getLinkedZoneOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("[ERROR] Error reading DNS Linked zone:%s\n%s", err, response))
}
d.Set(DnsLinkedZoneInstanceID, idSet[0])
d.Set(DnsLinkedZoneDescription, *resource.Description)
d.Set(DnsLinkedZoneLabel, *resource.Label)
d.Set(DnsLinkedZoneCreatedOn, resource.CreatedOn.String())
d.Set(DnsLinkedZoneModifiedOn, resource.ModifiedOn.String())
return nil
}
func resourceIBMDNSLinkedZoneUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return diag.FromErr(err)
}
idSet := strings.Split(d.Id(), "/")
if len(idSet) < 2 {
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/resolverID/secondaryZoneID", d.Id()))
}
instanceID := idSet[0]
linkedDnsZoneID := idSet[1]
// Check DNS zone is present
getLinkedZoneOptions := sess.NewGetLinkedZoneOptions(instanceID, linkedDnsZoneID)
_, response, err := sess.GetLinkedZone(getLinkedZoneOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching secondary zone:%s\n%s", err, response))
}
// Update DNS Linked zone if attributes has any change
if d.HasChange(DnsLinkedZoneDescription) ||
d.HasChange(DnsLinkedZoneLabel) {
updateLinkedZoneOptions := sess.NewUpdateLinkedZoneOptions(instanceID, linkedDnsZoneID)
description := d.Get(DnsLinkedZoneDescription).(string)
label := d.Get(DnsLinkedZoneLabel).(string)
updateLinkedZoneOptions.SetDescription(description)
updateLinkedZoneOptions.SetLabel(label)
mk := "dns_linked_zone_" + instanceID
conns.IbmMutexKV.Lock(mk)
defer conns.IbmMutexKV.Unlock(mk)
_, response, err := sess.UpdateLinkedZone(updateLinkedZoneOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error updating DNS Services zone:%s\n%s", err, response))
}
}
return resourceIBMDNSLinkedZoneRead(ctx, d, meta)
}
func resourceIBMDNSLinkedZoneDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return diag.FromErr(err)
}
idSet := strings.Split(d.Id(), "/")
if len(idSet) < 2 {
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/linkedDnsZoneID", d.Id()))
}
instanceID := idSet[0]
linkedDnsZoneID := idSet[1]
deleteLinkedZoneOptions := sess.NewDeleteLinkedZoneOptions(instanceID, linkedDnsZoneID)
mk := "linked_dns_zone_" + instanceID
conns.IbmMutexKV.Lock(mk)
defer conns.IbmMutexKV.Unlock(mk)
response, err := sess.DeleteLinkedZone(deleteLinkedZoneOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return nil
}
return diag.FromErr(fmt.Errorf("[ERROR] Error reading DNS Services secondary zone:%s\n%s", err, response))
}
d.SetId("")
return nil
}