-
Notifications
You must be signed in to change notification settings - Fork 132
/
block_fastly_service_v1_responseobject.go
182 lines (162 loc) · 5.06 KB
/
block_fastly_service_v1_responseobject.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
package fastly
import (
"fmt"
"log"
gofastly "github.com/fastly/go-fastly/v2/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type ResponseObjectServiceAttributeHandler struct {
*DefaultServiceAttributeHandler
}
func NewServiceResponseObject(sa ServiceMetadata) ServiceAttributeDefinition {
return &ResponseObjectServiceAttributeHandler{
&DefaultServiceAttributeHandler{
key: "response_object",
serviceMetadata: sa,
},
}
}
func (h *ResponseObjectServiceAttributeHandler) Process(d *schema.ResourceData, latestVersion int, conn *gofastly.Client) error {
or, nr := d.GetChange(h.GetKey())
if or == nil {
or = new(schema.Set)
}
if nr == nil {
nr = new(schema.Set)
}
ors := or.(*schema.Set)
nrs := nr.(*schema.Set)
removeResponseObject := ors.Difference(nrs).List()
addResponseObject := nrs.Difference(ors).List()
// DELETE old response object configurations
for _, rRaw := range removeResponseObject {
rf := rRaw.(map[string]interface{})
opts := gofastly.DeleteResponseObjectInput{
Service: d.Id(),
Version: latestVersion,
Name: rf["name"].(string),
}
log.Printf("[DEBUG] Fastly Response Object removal opts: %#v", opts)
err := conn.DeleteResponseObject(&opts)
if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
}
} else if err != nil {
return err
}
}
// POST new/updated Response Object
for _, rRaw := range addResponseObject {
rf := rRaw.(map[string]interface{})
opts := gofastly.CreateResponseObjectInput{
Service: d.Id(),
Version: latestVersion,
Name: rf["name"].(string),
Status: uint(rf["status"].(int)),
Response: rf["response"].(string),
Content: rf["content"].(string),
ContentType: rf["content_type"].(string),
RequestCondition: rf["request_condition"].(string),
CacheCondition: rf["cache_condition"].(string),
}
log.Printf("[DEBUG] Create Response Object Opts: %#v", opts)
_, err := conn.CreateResponseObject(&opts)
if err != nil {
return err
}
}
return nil
}
func (h *ResponseObjectServiceAttributeHandler) Read(d *schema.ResourceData, s *gofastly.ServiceDetail, conn *gofastly.Client) error {
log.Printf("[DEBUG] Refreshing Response Object for (%s)", d.Id())
responseObjectList, err := conn.ListResponseObjects(&gofastly.ListResponseObjectsInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up Response Object for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}
rol := flattenResponseObjects(responseObjectList)
if err := d.Set(h.GetKey(), rol); err != nil {
log.Printf("[WARN] Error setting Response Object for (%s): %s", d.Id(), err)
}
return nil
}
func (h *ResponseObjectServiceAttributeHandler) Register(s *schema.Resource) error {
s.Schema[h.GetKey()] = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique name to refer to this request object",
},
// Optional fields
"status": {
Type: schema.TypeInt,
Optional: true,
Default: 200,
Description: "The HTTP Status Code of the object",
},
"response": {
Type: schema.TypeString,
Optional: true,
Default: "OK",
Description: "The HTTP Response of the object",
},
"content": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The content to deliver for the response object",
},
"content_type": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The MIME type of the content",
},
"request_condition": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Name of the condition to be checked during the request phase to see if the object should be delivered",
},
"cache_condition": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Name of the condition checked after we have retrieved an object. If the condition passes then deliver this Request Object instead.",
},
},
},
}
return nil
}
func flattenResponseObjects(responseObjectList []*gofastly.ResponseObject) []map[string]interface{} {
var rol []map[string]interface{}
for _, ro := range responseObjectList {
// Convert ResponseObjects to a map for saving to state.
nro := map[string]interface{}{
"name": ro.Name,
"status": ro.Status,
"response": ro.Response,
"content": ro.Content,
"content_type": ro.ContentType,
"request_condition": ro.RequestCondition,
"cache_condition": ro.CacheCondition,
}
// prune any empty values that come from the default string value in structs
for k, v := range nro {
if v == "" {
delete(nro, k)
}
}
rol = append(rol, nro)
}
return rol
}