forked from cloudflare/terraform-provider-cloudflare
/
resource_cloudflare_spectrum_application.go
285 lines (228 loc) · 7.45 KB
/
resource_cloudflare_spectrum_application.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
273
274
275
276
277
278
279
280
281
282
283
284
285
package cloudflare
import (
"fmt"
"log"
"strings"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/pkg/errors"
)
func resourceCloudflareSpectrumApplication() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareSpectrumApplicationCreate,
Read: resourceCloudflareSpectrumApplicationRead,
Update: resourceCloudflareSpectrumApplicationUpdate,
Delete: resourceCloudflareSpectrumApplicationDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudflareSpectrumApplicationImport,
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeString,
Required: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
},
"dns": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"origin_direct": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"origin_dns": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"origin_port": {
Type: schema.TypeInt,
Required: true,
},
"tls": {
Type: schema.TypeString,
Optional: true,
Default: "off",
ValidateFunc: validation.StringInSlice([]string{
"off", "flexible", "full", "strict",
}, false),
},
"ip_firewall": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"proxy_protocol": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceCloudflareSpectrumApplicationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
newSpectrumApp := applicationFromResource(d)
zoneID := d.Get("zone_id").(string)
log.Printf("[INFO] Creating Cloudflare Spectrum Application from struct: %+v", newSpectrumApp)
r, err := client.CreateSpectrumApplication(zoneID, newSpectrumApp)
if err != nil {
return errors.Wrap(err, "error creating spectrum application for zone")
}
if r.ID == "" {
return fmt.Errorf("failed to find id in Create response; resource was empty")
}
d.SetId(r.ID)
log.Printf("[INFO] Cloudflare Spectrum Application ID: %s", d.Id())
return resourceCloudflareSpectrumApplicationRead(d, meta)
}
func resourceCloudflareSpectrumApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
application := applicationFromResource(d)
log.Printf("[INFO] Updating Cloudflare Spectrum Application from struct: %+v", application)
_, err := client.UpdateSpectrumApplication(zoneID, application.ID, application)
if err != nil {
return errors.Wrap(err, "error creating spectrum application for zone")
}
return resourceCloudflareSpectrumApplicationRead(d, meta)
}
func resourceCloudflareSpectrumApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
applicationID := d.Id()
application, err := client.SpectrumApplication(zoneID, applicationID)
if err != nil {
if strings.Contains(err.Error(), "HTTP status 404") {
log.Printf("[INFO] Spectrum application %s in zone %s not found", applicationID, zoneID)
d.SetId("")
return nil
}
return errors.Wrap(err,
fmt.Sprintf("Error reading spectrum application resource from API for resource %s in zone %s", zoneID, applicationID))
}
d.Set("protocol", application.Protocol)
if err := d.Set("dns", flattenDNS(application.DNS)); err != nil {
log.Printf("[WARN] Error setting dns on spectrum application %q: %s", d.Id(), err)
}
if len(application.OriginDirect) > 0 {
if err := d.Set("origin_direct", application.OriginDirect); err != nil {
log.Printf("[WARN] Error setting origin direct on spectrum application %q: %s", d.Id(), err)
}
}
if application.OriginDNS != nil {
if err := d.Set("origin_dns", flattenOriginDNS(application.OriginDNS)); err != nil {
log.Printf("[WARN] Error setting origin dns on spectrum application %q: %s", d.Id(), err)
}
}
d.Set("origin_port", application.OriginPort)
d.Set("tls", application.TLS)
d.Set("ip_firewall", application.IPFirewall)
d.Set("proxy_protocol", application.ProxyProtocol)
return nil
}
func resourceCloudflareSpectrumApplicationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
applicationID := d.Id()
log.Printf("[INFO] Deleting Cloudflare Spectrum Application: %s in zone: %s", applicationID, zoneID)
err := client.DeleteSpectrumApplication(zoneID, applicationID)
if err != nil {
return fmt.Errorf("error deleting Cloudflare Spectrum Application: %s", err)
}
return nil
}
func resourceCloudflareSpectrumApplicationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// split the id so we can lookup
idAttr := strings.SplitN(d.Id(), "/", 2)
var zoneID string
var applicationID string
if len(idAttr) == 2 {
zoneID = idAttr[0]
applicationID = idAttr[1]
} else {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"zoneID/applicationID\"", d.Id())
}
d.Set("zone_id", zoneID)
d.SetId(applicationID)
return []*schema.ResourceData{d}, nil
}
func expandDNS(d interface{}) cloudflare.SpectrumApplicationDNS {
cfg := d.([]interface{})
dns := cloudflare.SpectrumApplicationDNS{}
m := cfg[0].(map[string]interface{})
dns.Type = m["type"].(string)
dns.Name = m["name"].(string)
return dns
}
func expandOriginDNS(d interface{}) *cloudflare.SpectrumApplicationOriginDNS {
cfg := d.([]interface{})
dns := &cloudflare.SpectrumApplicationOriginDNS{}
m := cfg[0].(map[string]interface{})
dns.Name = m["name"].(string)
return dns
}
func flattenDNS(dns cloudflare.SpectrumApplicationDNS) []map[string]interface{} {
flattened := map[string]interface{}{}
flattened["type"] = dns.Type
flattened["name"] = dns.Name
return []map[string]interface{}{flattened}
}
func flattenOriginDNS(dns *cloudflare.SpectrumApplicationOriginDNS) []map[string]interface{} {
flattened := map[string]interface{}{}
flattened["name"] = dns.Name
return []map[string]interface{}{flattened}
}
func applicationFromResource(d *schema.ResourceData) cloudflare.SpectrumApplication {
application := cloudflare.SpectrumApplication{
ID: d.Id(),
Protocol: d.Get("protocol").(string),
DNS: expandDNS(d.Get("dns")),
}
if originDirect, ok := d.GetOk("origin_direct"); ok {
application.OriginDirect = expandInterfaceToStringList(originDirect.([]interface{}))
}
if originDNS, ok := d.GetOk("origin_dns"); ok {
application.OriginDNS = expandOriginDNS(originDNS)
}
if originPort, ok := d.GetOk("origin_port"); ok {
application.OriginPort = originPort.(int)
}
if tls, ok := d.GetOk("tls"); ok {
application.TLS = tls.(string)
}
if ipFirewall, ok := d.GetOk("ip_firewall"); ok {
application.IPFirewall = ipFirewall.(bool)
}
if proxyProtocol, ok := d.GetOk("proxy_protocol"); ok {
application.ProxyProtocol = proxyProtocol.(bool)
}
return application
}