-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathservice_attribute_definition.go
103 lines (87 loc) · 3.73 KB
/
service_attribute_definition.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
package fastly
import (
"context"
gofastly "github.com/fastly/go-fastly/v9/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// ServiceAttributeDefinition provides an interface for service attributes.
// We compose a service resource out of attribute objects to allow us to construct both the VCL and Compute service
// resources from common components.
type ServiceAttributeDefinition interface {
// Register add the attribute to the resource schema.
Register(s *schema.Resource) error
// Read refreshes the attribute state against the Fastly API.
Read(ctx context.Context, d *schema.ResourceData, s *gofastly.ServiceDetail, conn *gofastly.Client) error
// Process creates or updates the attribute against the Fastly API.
Process(ctx context.Context, d *schema.ResourceData, latestVersion int, conn *gofastly.Client) error
// HasChange returns whether the state of the attribute has changed against Terraform stored state.
HasChange(d *schema.ResourceData) bool
// MustProcess returns whether we must process the resource (usually HasChange==true but allowing exceptions).
// For example: at present, the settings attributeHandler (block_fastly_service_settings.go) must process when
// default_ttl==0 and it is the initialVersion - as well as when default_ttl or default_host have changed.
MustProcess(d *schema.ResourceData, initialVersion bool) bool
}
// ServiceMetadata provides a container to pass service attributes into an Attribute handler.
type ServiceMetadata struct {
serviceType string
}
// DefaultServiceAttributeHandler provides a base implementation for ServiceAttributeDefinition.
type DefaultServiceAttributeHandler struct {
key string
serviceMetadata ServiceMetadata
}
// GetKey is provided since most attributes will just use their private "key" for interacting with the service.
func (h *DefaultServiceAttributeHandler) GetKey() string {
return h.key
}
// GetServiceMetadata is provided to allow internal methods to get the service Metadata
func (h *DefaultServiceAttributeHandler) GetServiceMetadata() ServiceMetadata {
return h.serviceMetadata
}
// HasChange returns whether or not the given key has been changed.
func (h *DefaultServiceAttributeHandler) HasChange(d *schema.ResourceData) bool {
return d.HasChange(h.key)
}
// MustProcess returns whether we must process the resource.
func (h *DefaultServiceAttributeHandler) MustProcess(d *schema.ResourceData, _ bool) bool {
return h.HasChange(d)
}
// VCLLoggingAttributes represents VCL log configuration.
type VCLLoggingAttributes struct {
format string
formatVersion *int
placement string
responseCondition string
}
// getVCLLoggingAttributes provides default values to Compute services for VCL only logging attributes
func (h *DefaultServiceAttributeHandler) getVCLLoggingAttributes(data map[string]any) VCLLoggingAttributes {
vla := VCLLoggingAttributes{
placement: "none",
}
if h.GetServiceMetadata().serviceType == ServiceTypeVCL {
if val, ok := data["format"]; ok {
vla.format = val.(string)
}
if val, ok := data["format_version"]; ok {
vla.formatVersion = gofastly.ToPointer(val.(int))
}
if val, ok := data["placement"]; ok {
vla.placement = val.(string)
}
if val, ok := data["response_condition"]; ok {
vla.responseCondition = val.(string)
}
}
return vla
}
// pruneVCLLoggingAttributes deletes the keys corresponding to VCL-only logging attributes which aren't present for
// Compute services.
func (h *DefaultServiceAttributeHandler) pruneVCLLoggingAttributes(data map[string]any) map[string]any {
if h.GetServiceMetadata().serviceType == ServiceTypeCompute {
delete(data, "format")
delete(data, "format_version")
delete(data, "placement")
delete(data, "response_condition")
}
return data
}