-
Notifications
You must be signed in to change notification settings - Fork 140
/
resource_fastly_service_dictionary_items.go
245 lines (191 loc) · 7.05 KB
/
resource_fastly_service_dictionary_items.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
package fastly
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"strings"
gofastly "github.com/fastly/go-fastly/v6/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceServiceDictionaryItems() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServiceDictionaryItemsCreate,
ReadContext: resourceServiceDictionaryItemsRead,
UpdateContext: resourceServiceDictionaryItemsUpdate,
DeleteContext: resourceServiceDictionaryItemsDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceServiceDictionaryItemsImport,
},
Schema: map[string]*schema.Schema{
"service_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the service that the dictionary belongs to",
},
"dictionary_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the dictionary that the items belong to",
},
"manage_items": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: "Whether to reapply changes if the state of the items drifts, i.e. if items are managed externally",
},
"items": {
Type: schema.TypeMap,
Optional: true,
Description: "A map representing an entry in the dictionary, (key/value)",
ValidateDiagFunc: validateDictionaryItems(),
Elem: schema.TypeString,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.HasChange("dictionary_id") == false && d.Get("manage_items") == false
},
},
},
}
}
func resourceServiceDictionaryItemsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
dictionaryID := d.Get("dictionary_id").(string)
items := d.Get("items").(map[string]interface{})
var batchDictionaryItems []*gofastly.BatchDictionaryItem
for key, val := range items {
batchDictionaryItems = append(batchDictionaryItems, &gofastly.BatchDictionaryItem{
Operation: gofastly.CreateBatchOperation,
ItemKey: key,
ItemValue: val.(string),
})
}
// Process the batch operations
err := executeBatchDictionaryOperations(conn, serviceID, dictionaryID, batchDictionaryItems)
if err != nil {
return diag.Errorf("Error creating dictionary items: service %s, dictionary %s, %s", serviceID, dictionaryID, err)
}
d.SetId(fmt.Sprintf("%s/%s", serviceID, dictionaryID))
return resourceServiceDictionaryItemsRead(ctx, d, meta)
}
func resourceServiceDictionaryItemsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
dictionaryID := d.Get("dictionary_id").(string)
if d.HasChange("items") {
var batchDictionaryItems []*gofastly.BatchDictionaryItem
o, n := d.GetChange("items")
os := o.(map[string]interface{})
ns := n.(map[string]interface{})
// Handle Removal
for key := range os {
if _, ok := ns[key]; !ok {
batchDictionaryItems = append(batchDictionaryItems, &gofastly.BatchDictionaryItem{
Operation: gofastly.DeleteBatchOperation,
ItemKey: key,
})
}
}
for key, val := range ns {
// Handle replaces
if _, ok := os[key]; ok {
batchDictionaryItems = append(batchDictionaryItems, &gofastly.BatchDictionaryItem{
Operation: gofastly.UpdateBatchOperation,
ItemKey: key,
ItemValue: val.(string),
})
}
// Handle additions
if _, ok := os[key]; !ok {
batchDictionaryItems = append(batchDictionaryItems, &gofastly.BatchDictionaryItem{
Operation: gofastly.CreateBatchOperation,
ItemKey: key,
ItemValue: val.(string),
})
}
}
// Process the batch operations
err := executeBatchDictionaryOperations(conn, serviceID, dictionaryID, batchDictionaryItems)
if err != nil {
return diag.Errorf("Error updating dictionary items: service %s, dictionary %s, %s", serviceID, dictionaryID, err)
}
}
return resourceServiceDictionaryItemsRead(ctx, d, meta)
}
func resourceServiceDictionaryItemsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
dictionaryID := d.Get("dictionary_id").(string)
dictList, err := conn.ListDictionaryItems(&gofastly.ListDictionaryItemsInput{
ServiceID: serviceID,
DictionaryID: dictionaryID,
})
if err != nil {
return diag.FromErr(err)
}
err = d.Set("items", flattenDictionaryItems(dictList))
return diag.FromErr(err)
}
func resourceServiceDictionaryItemsDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
dictionaryID := d.Get("dictionary_id").(string)
items := d.Get("items").(map[string]interface{})
var batchDictionaryItems []*gofastly.BatchDictionaryItem
for key := range items {
batchDictionaryItems = append(batchDictionaryItems, &gofastly.BatchDictionaryItem{
Operation: gofastly.DeleteBatchOperation,
ItemKey: key,
})
}
// Process the batch operations
err := executeBatchDictionaryOperations(conn, serviceID, dictionaryID, batchDictionaryItems)
if err != nil {
return diag.Errorf("Error creating dictionary items: service %s, dictionary %s, %s", serviceID, dictionaryID, err)
}
d.SetId("")
return nil
}
func resourceServiceDictionaryItemsImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
split := strings.Split(d.Id(), "/")
if len(split) != 2 {
return nil, fmt.Errorf("Invalid id: %s. The ID should be in the format [service_id]/[dictionary_id]", d.Id())
}
serviceID := split[0]
dictionaryID := split[1]
err := d.Set("service_id", serviceID)
if err != nil {
return nil, fmt.Errorf("Error importing dictionary items: service %s, dictionary %s, %s", serviceID, dictionaryID, err)
}
err = d.Set("dictionary_id", dictionaryID)
if err != nil {
return nil, fmt.Errorf("Error importing dictionary items: service %s, dictionary %s, %s", serviceID, dictionaryID, err)
}
return []*schema.ResourceData{d}, nil
}
func flattenDictionaryItems(dictItemList []*gofastly.DictionaryItem) map[string]string {
resultList := make(map[string]string)
for _, currentDictItem := range dictItemList {
resultList[currentDictItem.ItemKey] = currentDictItem.ItemValue
}
return resultList
}
func executeBatchDictionaryOperations(conn *gofastly.Client, serviceID, dictionaryID string, batchDictionaryItems []*gofastly.BatchDictionaryItem) error {
batchSize := gofastly.BatchModifyMaximumOperations
for i := 0; i < len(batchDictionaryItems); i += batchSize {
j := i + batchSize
if j > len(batchDictionaryItems) {
j = len(batchDictionaryItems)
}
err := conn.BatchModifyDictionaryItems(&gofastly.BatchModifyDictionaryItemsInput{
ServiceID: serviceID,
DictionaryID: dictionaryID,
Items: batchDictionaryItems[i:j],
})
if err != nil {
return err
}
}
return nil
}