-
Notifications
You must be signed in to change notification settings - Fork 132
/
block_fastly_service_v1_director.go
253 lines (225 loc) · 6.59 KB
/
block_fastly_service_v1_director.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package fastly
import (
"fmt"
"log"
gofastly "github.com/fastly/go-fastly/v2/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type DirectorServiceAttributeHandler struct {
*DefaultServiceAttributeHandler
}
func NewServiceDirector(sa ServiceMetadata) ServiceAttributeDefinition {
return &DirectorServiceAttributeHandler{
&DefaultServiceAttributeHandler{
key: "director",
serviceMetadata: sa,
},
}
}
func (h *DirectorServiceAttributeHandler) Process(d *schema.ResourceData, latestVersion int, conn *gofastly.Client) error {
od, nd := d.GetChange(h.GetKey())
if od == nil {
od = new(schema.Set)
}
if nd == nil {
nd = new(schema.Set)
}
ods := od.(*schema.Set)
nds := nd.(*schema.Set)
removeDirector := ods.Difference(nds).List()
addDirector := nds.Difference(ods).List()
// DELETE old director configurations
for _, dRaw := range removeDirector {
df := dRaw.(map[string]interface{})
opts := gofastly.DeleteDirectorInput{
Service: d.Id(),
Version: latestVersion,
Name: df["name"].(string),
}
log.Printf("[DEBUG] Director Removal opts: %#v", opts)
err := conn.DeleteDirector(&opts)
if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
}
} else if err != nil {
return err
}
}
// POST new/updated Director
for _, dRaw := range addDirector {
df := dRaw.(map[string]interface{})
opts := gofastly.CreateDirectorInput{
Service: d.Id(),
Version: latestVersion,
Name: df["name"].(string),
Comment: df["comment"].(string),
Shield: df["shield"].(string),
Capacity: uint(df["capacity"].(int)),
Quorum: uint(df["quorum"].(int)),
Retries: uint(df["retries"].(int)),
}
switch df["type"].(int) {
case 1:
opts.Type = gofastly.DirectorTypeRandom
case 2:
opts.Type = gofastly.DirectorTypeRoundRobin
case 3:
opts.Type = gofastly.DirectorTypeHash
case 4:
opts.Type = gofastly.DirectorTypeClient
}
log.Printf("[DEBUG] Director Create opts: %#v", opts)
_, err := conn.CreateDirector(&opts)
if err != nil {
return err
}
if v, ok := df["backends"]; ok {
if len(v.(*schema.Set).List()) > 0 {
for _, b := range v.(*schema.Set).List() {
opts := gofastly.CreateDirectorBackendInput{
Service: d.Id(),
Version: latestVersion,
Director: df["name"].(string),
Backend: b.(string),
}
log.Printf("[DEBUG] Director Backend Create opts: %#v", opts)
_, err := conn.CreateDirectorBackend(&opts)
if err != nil {
return err
}
}
}
}
}
return nil
}
func (h *DirectorServiceAttributeHandler) Read(d *schema.ResourceData, s *gofastly.ServiceDetail, conn *gofastly.Client) error {
log.Printf("[DEBUG] Refreshing Directors for (%s)", d.Id())
directorList, err := conn.ListDirectors(&gofastly.ListDirectorsInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up Directors for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}
log.Printf("[DEBUG] Refreshing Backends for (%s)", d.Id())
backendList, err := conn.ListBackends(&gofastly.ListBackendsInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up Backends for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}
log.Printf("[DEBUG] Refreshing Director Backends for (%s)", d.Id())
var directorBackendList []*gofastly.DirectorBackend
for _, director := range directorList {
for _, backend := range backendList {
directorBackendGet, err := conn.GetDirectorBackend(&gofastly.GetDirectorBackendInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
Director: director.Name,
Backend: backend.Name,
})
if err == nil {
directorBackendList = append(directorBackendList, directorBackendGet)
}
}
}
dirl := flattenDirectors(directorList, directorBackendList)
if err := d.Set(h.GetKey(), dirl); err != nil {
log.Printf("[WARN] Error setting Directors for (%s): %s", d.Id(), err)
}
return nil
}
func (h *DirectorServiceAttributeHandler) Register(s *schema.Resource) error {
s.Schema[h.GetKey()] = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "A name to refer to this director",
},
"backends": {
Type: schema.TypeSet,
Required: true,
Description: "List of backends associated with this director",
Elem: &schema.Schema{Type: schema.TypeString},
},
// optional fields
"capacity": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
Description: "Load balancing weight for the backends",
},
"comment": {
Type: schema.TypeString,
Optional: true,
},
"shield": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Selected POP to serve as a 'shield' for origin servers.",
},
"quorum": {
Type: schema.TypeInt,
Optional: true,
Default: 75,
Description: "Percentage of capacity that needs to be up for the director itself to be considered up",
ValidateFunc: validateDirectorQuorum(),
},
"type": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "Type of load balance group to use. Integer, 1 to 4. Values: 1 (random), 3 (hash), 4 (client)",
ValidateFunc: validateDirectorType(),
},
"retries": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
Description: "How many backends to search if it fails",
},
},
},
}
return nil
}
func flattenDirectors(directorList []*gofastly.Director, directorBackendList []*gofastly.DirectorBackend) []map[string]interface{} {
var dl []map[string]interface{}
for _, d := range directorList {
// Convert Director to a map for saving to state.
nd := map[string]interface{}{
"name": d.Name,
"comment": d.Comment,
"shield": d.Shield,
"type": d.Type,
"quorum": int(d.Quorum),
"capacity": int(d.Capacity),
"retries": int(d.Retries),
}
var b []interface{}
for _, db := range directorBackendList {
if d.Name == db.Director {
b = append(b, db.Backend)
}
}
if len(b) > 0 {
nd["backends"] = schema.NewSet(schema.HashString, b)
}
// prune any empty values that come from the default string value in structs
for k, v := range nd {
if v == "" {
delete(nd, k)
}
}
dl = append(dl, nd)
}
return dl
}