/
resource_ibm_private_dns_zones.go
230 lines (192 loc) · 6.08 KB
/
resource_ibm_private_dns_zones.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package dnsservices
import (
"fmt"
"strings"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
pdnsInstanceID = "instance_id"
pdnsZoneName = "name"
pdnsZoneDescription = "description"
pdnsZoneLabel = "label"
pdnsZoneCreatedOn = "created_on"
pdnsZoneModifiedOn = "modified_on"
pdnsZoneState = "state"
pdnsZoneID = "zone_id"
)
func ResourceIBMPrivateDNSZone() *schema.Resource {
return &schema.Resource{
Create: resourceIBMPrivateDNSZoneCreate,
Read: resourceIBMPrivateDNSZoneRead,
Update: resourceIBMPrivateDNSZoneUpdate,
Delete: resourceIBMPrivateDNSZoneDelete,
Exists: resourceIBMPrivateDNSZoneExists,
Importer: &schema.ResourceImporter{},
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{
pdnsInstanceID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Instance ID",
},
pdnsZoneID: {
Type: schema.TypeString,
Computed: true,
Description: "Zone ID",
},
pdnsZoneName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Zone name",
},
pdnsZoneDescription: {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "Zone description",
},
pdnsZoneState: {
Type: schema.TypeString,
Computed: true,
Description: "Zone state",
},
pdnsZoneLabel: {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "Label",
},
pdnsZoneCreatedOn: {
Type: schema.TypeString,
Computed: true,
Description: "Creation date",
},
pdnsZoneModifiedOn: {
Type: schema.TypeString,
Computed: true,
Description: "Modification date",
},
},
}
}
func resourceIBMPrivateDNSZoneCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
var (
instanceID string
zoneName string
zoneDescription string
zoneLabel string
)
instanceID = d.Get(pdnsInstanceID).(string)
zoneName = d.Get(pdnsZoneName).(string)
if v, ok := d.GetOk(pdnsZoneDescription); ok {
zoneDescription = v.(string)
}
if v, ok := d.GetOk(pdnsZoneLabel); ok {
zoneLabel = v.(string)
}
createZoneOptions := sess.NewCreateDnszoneOptions(instanceID)
createZoneOptions.SetName(zoneName)
createZoneOptions.SetDescription(zoneDescription)
createZoneOptions.SetLabel(zoneLabel)
response, detail, err := sess.CreateDnszone(createZoneOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error creating pdns zone:%s\n%s", err, detail)
}
d.SetId(fmt.Sprintf("%s/%s", *response.InstanceID, *response.ID))
d.Set(pdnsZoneID, *response.ID)
return resourceIBMPrivateDNSZoneRead(d, meta)
}
func resourceIBMPrivateDNSZoneRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
idSet := strings.Split(d.Id(), "/")
getZoneOptions := sess.NewGetDnszoneOptions(idSet[0], idSet[1])
response, detail, err := sess.GetDnszone(getZoneOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching pdns zone:%s\n%s", err, detail)
}
d.Set(pdnsZoneID, response.ID)
d.Set(pdnsInstanceID, response.InstanceID)
d.Set(pdnsZoneName, response.Name)
d.Set(pdnsZoneDescription, response.Description)
d.Set(pdnsZoneLabel, response.Label)
d.Set(pdnsZoneCreatedOn, response.CreatedOn.String())
d.Set(pdnsZoneModifiedOn, response.ModifiedOn.String())
d.Set(pdnsZoneState, response.State)
return nil
}
func resourceIBMPrivateDNSZoneUpdate(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
idSet := strings.Split(d.Id(), "/")
// Check DNS zone is present?
getZoneOptions := sess.NewGetDnszoneOptions(idSet[0], idSet[1])
_, response, err := sess.GetDnszone(getZoneOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching pdns zone:%s\n%s", err, response)
}
// Update DNS zone if attributes has any change
if d.HasChange(pdnsZoneLabel) || d.HasChange(pdnsZoneDescription) {
updateZoneOptions := sess.NewUpdateDnszoneOptions(idSet[0], idSet[1])
description := d.Get(pdnsZoneDescription).(string)
label := d.Get(pdnsZoneLabel).(string)
updateZoneOptions.SetDescription(description)
updateZoneOptions.SetLabel(label)
_, detail, err := sess.UpdateDnszone(updateZoneOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error updating pdns zone:%s\n%s", err, detail)
}
}
return resourceIBMPrivateDNSZoneRead(d, meta)
}
func resourceIBMPrivateDNSZoneDelete(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
idSet := strings.Split(d.Id(), "/")
deleteZoneOptions := sess.NewDeleteDnszoneOptions(idSet[0], idSet[1])
response, err := sess.DeleteDnszone(deleteZoneOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error deleting pdns zone:%s\n%s", err, response)
}
d.SetId("")
return nil
}
func resourceIBMPrivateDNSZoneExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return false, err
}
idSet := strings.Split(d.Id(), "/")
if len(idSet) < 2 {
return false, fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/zoneID", d.Id())
}
getZoneOptions := sess.NewGetDnszoneOptions(idSet[0], idSet[1])
_, response, err := sess.GetDnszone(getZoneOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return false, nil
}
return false, err
}
return true, nil
}