forked from cloudflare/terraform-provider-cloudflare
/
resource_cloudflare_zone.go
262 lines (220 loc) · 6.46 KB
/
resource_cloudflare_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package cloudflare
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
// we enforce the use of the Cloudflare API 'legacy_id' field until the mapping of plan is fixed in cloudflare-go
const (
planIDFree = "free"
planIDPro = "pro"
planIDBusiness = "business"
planIDEnterprise = "enterprise"
)
// we keep a private map and we will have a function to check and validate the descriptive name from the RatePlan API with the legacy_id
var idForName = map[string]string{
"Free Website": planIDFree,
"Pro Website": planIDPro,
"Business Website": planIDBusiness,
"Enterprise Website": planIDEnterprise,
}
func resourceCloudflareZone() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareZoneCreate,
Read: resourceCloudflareZoneRead,
Update: resourceCloudflareZoneUpdate,
Delete: resourceCloudflareZoneDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^([a-zA-Z0-9][\\-a-zA-Z0-9]*\\.)+[\\-a-zA-Z0-9]{2,20}$"), ""),
},
"jump_start": {
Type: schema.TypeBool,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
},
"paused": {
Type: schema.TypeBool,
Optional: true,
},
"vanity_name_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"plan": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{planIDFree, planIDPro, planIDBusiness, planIDEnterprise}, false),
},
"meta": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"wildcard_proxiable": {
Type: schema.TypeBool,
},
"phishing_detected": {
Type: schema.TypeBool,
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"name_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceCloudflareZoneCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneName := d.Get("zone").(string)
jumpstart := d.Get("jump_start").(bool)
organization := cloudflare.Organization{
ID: client.OrganizationID,
}
log.Printf("[INFO] Creating Cloudflare Zone: name %s", zoneName)
zone, err := client.CreateZone(zoneName, jumpstart, organization)
if err != nil {
return fmt.Errorf("Error creating zone %q: %s", zoneName, err)
}
d.SetId(zone.ID)
if paused, ok := d.GetOk("paused"); ok {
if paused.(bool) == true {
_, err := client.ZoneSetPaused(zone.ID, paused.(bool))
if err != nil {
return fmt.Errorf("Error updating zone_id %q: %s", zone.ID, err)
}
}
}
if plan, ok := d.GetOk("plan"); ok {
if err := setRatePlan(client, zone.ID, plan.(string)); err != nil {
return err
}
}
return resourceCloudflareZoneRead(d, meta)
}
func resourceCloudflareZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Id()
zone, err := client.ZoneDetails(zoneID)
log.Printf("[DEBUG] ZoneDetails: %#v", zone)
log.Printf("[DEBUG] ZoneDetails error: %#v", err)
if err != nil {
if strings.Contains(err.Error(), "HTTP status 404") {
log.Printf("[INFO] Zone %s no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error finding Zone %q: %s", d.Id(), err)
}
d.Set("paused", zone.Paused)
d.Set("vanity_name_servers", zone.VanityNS)
d.Set("status", zone.Status)
d.Set("type", zone.Type)
d.Set("name_servers", zone.NameServers)
d.Set("meta", flattenMeta(d, zone.Meta))
d.Set("zone", zone.Name)
d.Set("plan", planIDForName(zone.Plan.Name))
return nil
}
func resourceCloudflareZoneUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Id()
log.Printf("[INFO] Updating Cloudflare Zone: id %s", zoneID)
if paused, ok := d.GetOkExists("paused"); ok {
log.Printf("[DEBUG] _ paused")
_, err := client.ZoneSetPaused(zoneID, paused.(bool))
if err != nil {
return fmt.Errorf("Error updating zone_id %q: %s", zoneID, err)
}
}
if plan, ok := d.GetOk("plan"); ok {
if err := setRatePlan(client, zoneID, plan.(string)); err != nil {
return err
}
}
return resourceCloudflareZoneRead(d, meta)
}
func resourceCloudflareZoneDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Id()
log.Printf("[INFO] Deleting Cloudflare Zone: id %s", zoneID)
_, err := client.DeleteZone(zoneID)
if err != nil {
return fmt.Errorf("Error deleting Cloudflare Zone: %s", err)
}
return nil
}
func flattenMeta(d *schema.ResourceData, meta cloudflare.ZoneMeta) map[string]interface{} {
cfg := map[string]interface{}{}
cfg["wildcard_proxiable"] = strconv.FormatBool(meta.WildcardProxiable)
cfg["phishing_detected"] = strconv.FormatBool(meta.PhishingDetected)
log.Printf("[DEBUG] flattenMeta %#v", cfg)
return cfg
}
func setRatePlan(client *cloudflare.API, zoneID string, planID string) error {
ratePlan, err := getZonePlanIDFor(client, zoneID, planNameForID(planID))
if err != nil {
return fmt.Errorf("Error fetching plans %s for zone %q: %s", planID, zoneID, err)
}
if _, err := client.ZoneSetRatePlan(zoneID, *ratePlan); err != nil {
return fmt.Errorf("Error setting plan %s for zone %q: %s", planID, zoneID, err)
}
return nil
}
func getZonePlanIDFor(client *cloudflare.API, zoneID, planName string) (*cloudflare.ZoneRatePlan, error) {
plans, err := client.AvailableZoneRatePlans(zoneID)
if err != nil {
return nil, err
}
for _, p := range plans {
if strings.EqualFold(p.Name, planName) {
return &p, nil
}
}
return nil, fmt.Errorf("plan %s not found amongst the available plans", planName)
}
func planIDForName(name string) string {
if p, ok := idForName[name]; ok {
return p
}
return ""
}
func planNameForID(id string) string {
for k, v := range idForName {
if strings.EqualFold(id, v) {
return k
}
}
return ""
}