-
Notifications
You must be signed in to change notification settings - Fork 137
/
block_fastly_service_v1_settings.go
77 lines (65 loc) · 2.44 KB
/
block_fastly_service_v1_settings.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
package fastly
import (
"fmt"
"log"
gofastly "github.com/fastly/go-fastly/v2/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type SettingsServiceAttributeHandler struct {
}
func NewServiceSettings() ServiceAttributeDefinition {
return &SettingsServiceAttributeHandler{}
}
func (h *SettingsServiceAttributeHandler) Process(d *schema.ResourceData, latestVersion int, conn *gofastly.Client) error {
opts := gofastly.UpdateSettingsInput{
ServiceID: d.Id(),
ServiceVersion: latestVersion,
// default_ttl has the same default value of 3600 that is provided by
// the Fastly API, so it's safe to include here
DefaultTTL: uint(d.Get("default_ttl").(int)),
}
if attr, ok := d.GetOk("default_host"); ok {
opts.DefaultHost = gofastly.String(attr.(string))
}
log.Printf("[DEBUG] Update Settings opts: %#v", opts)
_, err := conn.UpdateSettings(&opts)
return err
}
func (h *SettingsServiceAttributeHandler) Read(d *schema.ResourceData, s *gofastly.ServiceDetail, conn *gofastly.Client) error {
settingsOpts := gofastly.GetSettingsInput{
ServiceID: d.Id(),
ServiceVersion: s.ActiveVersion.Number,
}
if settings, err := conn.GetSettings(&settingsOpts); err == nil {
d.Set("default_host", settings.DefaultHost)
d.Set("default_ttl", settings.DefaultTTL)
} else {
return fmt.Errorf("[ERR] Error looking up Version settings for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}
return nil
}
func (h *SettingsServiceAttributeHandler) HasChange(d *schema.ResourceData) bool {
return d.HasChange("default_ttl") || d.HasChange("default_host")
}
// If the requested default_ttl is 0, and this is the first
// version being created, HasChange will return false, but we need
// to set it anyway, so ensure we update the settings in that
// case.
func (h *SettingsServiceAttributeHandler) MustProcess(d *schema.ResourceData, initialVersion bool) bool {
return d.HasChange("default_host") || d.HasChange("default_ttl") || (d.Get("default_ttl") == 0 && initialVersion)
}
func (h *SettingsServiceAttributeHandler) Register(s *schema.Resource) error {
s.Schema["default_ttl"] = &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 3600,
Description: "The default Time-to-live (TTL) for the version",
}
s.Schema["default_host"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The default hostname for the version",
}
return nil
}