forked from cloudflare/terraform-provider-cloudflare
/
resource_cloudflare_custom_pages.go
175 lines (150 loc) · 4.93 KB
/
resource_cloudflare_custom_pages.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
package cloudflare
import (
"fmt"
"log"
"strings"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/pkg/errors"
)
func resourceCloudflareCustomPages() *schema.Resource {
return &schema.Resource{
// Pointing the `Create` at the `Update` method here is intentional.
// Custom pages don't really get "created" as they are always
// present in Cloudflare. We just update and toggle the settings to
// be customised.
Create: resourceCloudflareCustomPagesUpdate,
Read: resourceCloudflareCustomPagesRead,
Update: resourceCloudflareCustomPagesUpdate,
Delete: resourceCloudflareCustomPagesDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudflareCustomPagesImport,
},
Schema: map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"account_id"},
},
"account_id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"zone_id"},
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"basic_challenge",
"waf_challenge",
"waf_block",
"ratelimit_block",
"country_challenge",
"ip_block",
"under_attack",
"500_errors",
"1000_errors",
"always_online",
}, true),
},
"url": {
Type: schema.TypeString,
Required: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"default", "customized"}, true),
},
},
}
}
func resourceCloudflareCustomPagesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
accountID := d.Get("account_id").(string)
pageType := d.Get("type").(string)
if accountID == "" && zoneID == "" {
return fmt.Errorf("either `account_id` or `zone_id` must be set")
}
var pageOptions cloudflare.CustomPageOptions
if accountID != "" {
pageOptions = cloudflare.CustomPageOptions{AccountID: accountID}
} else {
pageOptions = cloudflare.CustomPageOptions{ZoneID: zoneID}
}
page, err := client.CustomPage(&pageOptions, pageType)
if err != nil {
return errors.New(err.Error())
}
// If the `page.State` comes back as "default", it's safe to assume we
// don't need to keep the ID managed anymore as it will be relying on
// Cloudflare's default pages.
if page.State == "default" {
log.Printf("[INFO] removing custom page configuration for '%s' as it is marked as being in the default state", pageType)
d.SetId("")
return nil
}
d.Set("state", page.State)
d.Set("url", page.URL)
d.Set("type", page.ID)
return nil
}
func resourceCloudflareCustomPagesUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)
zoneID := d.Get("zone_id").(string)
var pageOptions cloudflare.CustomPageOptions
if accountID != "" {
pageOptions = cloudflare.CustomPageOptions{AccountID: accountID}
} else {
pageOptions = cloudflare.CustomPageOptions{ZoneID: zoneID}
}
pageType := d.Get("type").(string)
customPageParameters := cloudflare.CustomPageParameters{
URL: d.Get("url").(*string),
State: "customized",
}
_, err := client.UpdateCustomPage(&pageOptions, pageType, customPageParameters)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to update '%s' custom page", pageType))
}
return resourceCloudflareCustomPagesRead(d, meta)
}
func resourceCloudflareCustomPagesDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)
zoneID := d.Get("zoneID").(string)
var pageOptions cloudflare.CustomPageOptions
if accountID != "" {
pageOptions = cloudflare.CustomPageOptions{AccountID: accountID}
} else {
pageOptions = cloudflare.CustomPageOptions{ZoneID: zoneID}
}
pageType := d.Get("type").(string)
customPageParameters := cloudflare.CustomPageParameters{
URL: nil,
State: "default",
}
_, err := client.UpdateCustomPage(&pageOptions, pageType, customPageParameters)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to update '%s' custom page", pageType))
}
return resourceCloudflareCustomPagesRead(d, meta)
}
func resourceCloudflareCustomPagesImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
attributes := strings.SplitN(d.Id(), "/", 3)
if len(attributes) != 3 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"requestType/ID/pageType\"", d.Id())
}
requestType, identifier, pageType := attributes[0], attributes[1], attributes[2]
d.Set("type", pageType)
if requestType == "account" {
d.Set("account_id", identifier)
} else {
d.Set("zone_id", identifier)
}
resourceCloudflareCustomPagesRead(d, meta)
return []*schema.ResourceData{d}, nil
}