Skip to content

Commit 9437912

Browse files
committed
provider/fastly: Add support for Cache Settings (hashicorp#6781)
* provider/fastly: Add cache settings Docs, tests, and implementation for Cache Settings support
1 parent b20744b commit 9437912

3 files changed

Lines changed: 378 additions & 0 deletions

File tree

builtin/providers/fastly/resource_fastly_service_v1.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func resourceServiceV1() *schema.Resource {
2121
Read: resourceServiceV1Read,
2222
Update: resourceServiceV1Update,
2323
Delete: resourceServiceV1Delete,
24+
Importer: &schema.ResourceImporter{
25+
State: schema.ImportStatePassthrough,
26+
},
2427

2528
Schema: map[string]*schema.Schema{
2629
"name": &schema.Schema{
@@ -193,6 +196,43 @@ func resourceServiceV1() *schema.Resource {
193196
Optional: true,
194197
},
195198

199+
"cache_setting": &schema.Schema{
200+
Type: schema.TypeSet,
201+
Optional: true,
202+
Elem: &schema.Resource{
203+
Schema: map[string]*schema.Schema{
204+
// required fields
205+
"name": &schema.Schema{
206+
Type: schema.TypeString,
207+
Required: true,
208+
Description: "A name to refer to this Cache Setting",
209+
},
210+
"cache_condition": &schema.Schema{
211+
Type: schema.TypeString,
212+
Required: true,
213+
Description: "Condition to check if this Cache Setting applies",
214+
},
215+
"action": &schema.Schema{
216+
Type: schema.TypeString,
217+
Optional: true,
218+
Description: "Action to take",
219+
},
220+
// optional
221+
"stale_ttl": &schema.Schema{
222+
Type: schema.TypeInt,
223+
Optional: true,
224+
Description: "Max 'Time To Live' for stale (unreachable) objects.",
225+
Default: 300,
226+
},
227+
"ttl": &schema.Schema{
228+
Type: schema.TypeInt,
229+
Optional: true,
230+
Description: "The 'Time To Live' for the object",
231+
},
232+
},
233+
},
234+
},
235+
196236
"gzip": &schema.Schema{
197237
Type: schema.TypeSet,
198238
Optional: true,
@@ -560,6 +600,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
560600
"s3logging",
561601
"condition",
562602
"request_setting",
603+
"cache_setting",
563604
"vcl",
564605
} {
565606
if d.HasChange(v) {
@@ -1020,6 +1061,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
10201061
}
10211062
}
10221063
}
1064+
10231065
// Find differences in VCLs
10241066
if d.HasChange("vcl") {
10251067
// Note: as above with Gzip and S3 logging, we don't utilize the PUT
@@ -1086,6 +1128,56 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
10861128
}
10871129
}
10881130

1131+
// Find differences in Cache Settings
1132+
if d.HasChange("cache_setting") {
1133+
oc, nc := d.GetChange("cache_setting")
1134+
if oc == nil {
1135+
oc = new(schema.Set)
1136+
}
1137+
if nc == nil {
1138+
nc = new(schema.Set)
1139+
}
1140+
1141+
ocs := oc.(*schema.Set)
1142+
ncs := nc.(*schema.Set)
1143+
1144+
remove := ocs.Difference(ncs).List()
1145+
add := ncs.Difference(ocs).List()
1146+
1147+
// Delete removed Cache Settings
1148+
for _, dRaw := range remove {
1149+
df := dRaw.(map[string]interface{})
1150+
opts := gofastly.DeleteCacheSettingInput{
1151+
Service: d.Id(),
1152+
Version: latestVersion,
1153+
Name: df["name"].(string),
1154+
}
1155+
1156+
log.Printf("[DEBUG] Fastly Cache Settings removal opts: %#v", opts)
1157+
err := conn.DeleteCacheSetting(&opts)
1158+
if err != nil {
1159+
return err
1160+
}
1161+
}
1162+
1163+
// POST new Cache Settings
1164+
for _, dRaw := range add {
1165+
opts, err := buildCacheSetting(dRaw.(map[string]interface{}))
1166+
if err != nil {
1167+
log.Printf("[DEBUG] Error building Cache Setting: %s", err)
1168+
return err
1169+
}
1170+
opts.Service = d.Id()
1171+
opts.Version = latestVersion
1172+
1173+
log.Printf("[DEBUG] Fastly Cache Settings Addition opts: %#v", opts)
1174+
_, err = conn.CreateCacheSetting(opts)
1175+
if err != nil {
1176+
return err
1177+
}
1178+
}
1179+
}
1180+
10891181
// validate version
10901182
log.Printf("[DEBUG] Validating Fastly Service (%s), Version (%s)", d.Id(), latestVersion)
10911183
valid, msg, err := conn.ValidateVersion(&gofastly.ValidateVersionInput{
@@ -1282,6 +1374,7 @@ func resourceServiceV1Read(d *schema.ResourceData, meta interface{}) error {
12821374
if err := d.Set("request_setting", rl); err != nil {
12831375
log.Printf("[WARN] Error setting Request Settings for (%s): %s", d.Id(), err)
12841376
}
1377+
12851378
// refresh VCLs
12861379
log.Printf("[DEBUG] Refreshing VCLs for (%s)", d.Id())
12871380
vclList, err := conn.ListVCLs(&gofastly.ListVCLsInput{
@@ -1298,6 +1391,22 @@ func resourceServiceV1Read(d *schema.ResourceData, meta interface{}) error {
12981391
log.Printf("[WARN] Error setting VCLs for (%s): %s", d.Id(), err)
12991392
}
13001393

1394+
// refresh Cache Settings
1395+
log.Printf("[DEBUG] Refreshing Cache Settings for (%s)", d.Id())
1396+
cslList, err := conn.ListCacheSettings(&gofastly.ListCacheSettingsInput{
1397+
Service: d.Id(),
1398+
Version: s.ActiveVersion.Number,
1399+
})
1400+
if err != nil {
1401+
return fmt.Errorf("[ERR] Error looking up Cache Settings for (%s), version (%s): %s", d.Id(), s.ActiveVersion.Number, err)
1402+
}
1403+
1404+
csl := flattenCacheSettings(cslList)
1405+
1406+
if err := d.Set("cache_setting", csl); err != nil {
1407+
log.Printf("[WARN] Error setting Cache Settings for (%s): %s", d.Id(), err)
1408+
}
1409+
13011410
} else {
13021411
log.Printf("[DEBUG] Active Version for Service (%s) is empty, no state to refresh", d.Id())
13031412
}
@@ -1497,6 +1606,31 @@ func buildHeader(headerMap interface{}) (*gofastly.CreateHeaderInput, error) {
14971606
return &opts, nil
14981607
}
14991608

1609+
func buildCacheSetting(cacheMap interface{}) (*gofastly.CreateCacheSettingInput, error) {
1610+
df := cacheMap.(map[string]interface{})
1611+
opts := gofastly.CreateCacheSettingInput{
1612+
Name: df["name"].(string),
1613+
StaleTTL: uint(df["stale_ttl"].(int)),
1614+
CacheCondition: df["cache_condition"].(string),
1615+
}
1616+
1617+
if v, ok := df["ttl"]; ok {
1618+
opts.TTL = uint(v.(int))
1619+
}
1620+
1621+
act := strings.ToLower(df["action"].(string))
1622+
switch act {
1623+
case "cache":
1624+
opts.Action = gofastly.CacheSettingActionCache
1625+
case "pass":
1626+
opts.Action = gofastly.CacheSettingActionPass
1627+
case "restart":
1628+
opts.Action = gofastly.CacheSettingActionRestart
1629+
}
1630+
1631+
return &opts, nil
1632+
}
1633+
15001634
func flattenGzips(gzipsList []*gofastly.Gzip) []map[string]interface{} {
15011635
var gl []map[string]interface{}
15021636
for _, g := range gzipsList {
@@ -1662,6 +1796,32 @@ func buildRequestSetting(requestSettingMap interface{}) (*gofastly.CreateRequest
16621796

16631797
return &opts, nil
16641798
}
1799+
1800+
func flattenCacheSettings(csList []*gofastly.CacheSetting) []map[string]interface{} {
1801+
var csl []map[string]interface{}
1802+
for _, cl := range csList {
1803+
// Convert Cache Settings to a map for saving to state.
1804+
clMap := map[string]interface{}{
1805+
"name": cl.Name,
1806+
"action": cl.Action,
1807+
"cache_condition": cl.CacheCondition,
1808+
"stale_ttl": cl.StaleTTL,
1809+
"ttl": cl.TTL,
1810+
}
1811+
1812+
// prune any empty values that come from the default string value in structs
1813+
for k, v := range clMap {
1814+
if v == "" {
1815+
delete(clMap, k)
1816+
}
1817+
}
1818+
1819+
csl = append(csl, clMap)
1820+
}
1821+
1822+
return csl
1823+
}
1824+
16651825
func flattenVCLs(vclList []*gofastly.VCL) []map[string]interface{} {
16661826
var vl []map[string]interface{}
16671827
for _, vcl := range vclList {

0 commit comments

Comments
 (0)