/
resource_illumio_workload_settings.go
313 lines (271 loc) · 9.27 KB
/
resource_illumio_workload_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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2021 Illumio, Inc. All Rights Reserved.
package illumiocore
import (
"context"
"fmt"
"strings"
"github.com/Jeffail/gabs/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/illumio/terraform-provider-illumio-core/models"
)
var validVENTypes = []string{"server", "endpoint"}
func resourceIllumioWorkloadSettings() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIllumioWorkloadSettingsCreate,
ReadContext: resourceIllumioWorkloadSettingsRead,
UpdateContext: resourceIllumioWorkloadSettingsUpdate,
DeleteContext: resourceIllumioWorkloadSettingsDelete,
SchemaVersion: 1,
Description: "Manages Illumio Workload Settings",
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "URI of the Workload Settings",
},
"workload_disconnected_timeout_seconds": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Workload Disconnected Timeout Seconds for Workload Settings",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scope": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Assigned labels for Workload Disconnected Timeout Seconds",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Optional: true,
Description: "Label URI",
},
},
},
},
"value": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Property value associated with the scope. Allowed range is 300 - 2147483647 or -1",
ValidateDiagFunc: validation.ToDiagFunc(
validation.Any(validation.IntBetween(300, 2147483647), validation.IntInSlice([]int{-1})),
),
},
"ven_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The VEN type that this property is applicable to. Must be "server" or "endpoint". An empty or missing value will default to "server" on the PCE`,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validVENTypes, true)),
},
},
},
},
"workload_goodbye_timeout_seconds": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Workload Goodbye Timeout Seconds for Workload Settings",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scope": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Assigned labels for Workload Goodbye Timeout Seconds",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Optional: true,
Description: "Label URI",
},
},
},
},
"value": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Property value associated with the scope. Allowed range is 300 - 2147483647 or -1",
ValidateDiagFunc: validation.ToDiagFunc(
validation.Any(validation.IntBetween(300, 2147483647), validation.IntInSlice([]int{-1})),
),
},
"ven_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The VEN type that this property is applicable to. Must be "server" or "endpoint". An empty or missing value will default to "server" on the PCE`,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validVENTypes, true)),
},
},
},
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: customdiff.Sequence(
customizeWorkloadTimeoutSettings("workload_disconnected_timeout_seconds"),
customizeWorkloadTimeoutSettings("workload_goodbye_timeout_seconds"),
),
}
}
// XXX: The PUT for adding/updating workload settings is a partial
// update, updating the settings map for each provided setting
// based on the unique combination of ven type and scopes.
// Terraform expects an idempotent update, so the plan diff
// will reflect any unchanged settings as being removed.
//
// We work around this by manually updating the diff to add any
// unchanged remote settings to the update set as-is.
func customizeWorkloadTimeoutSettings(key string) schema.CustomizeDiffFunc {
return func(ctx context.Context, d *schema.ResourceDiff, m any) error {
if d.HasChange(key) {
state, change := d.GetChange(key)
stateMap := mapWorkloadTimeoutSettingsState(state)
diffMap := mapWorkloadTimeoutSettingsState(change)
diff := change.(*schema.Set)
for k, v := range stateMap {
if _, ok := diffMap[k]; !ok {
parts := strings.Split(k, ":")
scopes := &schema.Set{}
if parts[1] != "" {
for _, s := range strings.Split(parts[1], "|") {
scopes.Add(map[string]any{"href": s})
}
}
setting := map[string]any{
"scope": scopes,
"value": v,
"ven_type": parts[0],
}
diff.Add(setting)
}
}
d.SetNew(key, diff)
}
return nil
}
}
// mapWorkloadTimeoutSettingsState converts a given workload
// timeout settings block into a lookup of the form
//
// {
// "{ven_type}:{scope[0]}|...|{scope[n]}" : {value},
// }
func mapWorkloadTimeoutSettingsState(state any) map[string]int {
st := map[string]int{}
for _, setting := range state.(*schema.Set).List() {
settingKey := ""
settingVal := 0
for k, v := range setting.(map[string]any) {
switch k {
case "scope":
for _, scope := range v.(*schema.Set).List() {
settingKey = settingKey + scope.(map[string]any)["href"].(string) + "|"
}
case "ven_type":
settingKey = v.(string) + ":" + settingKey
case "value":
settingVal = v.(int)
}
}
// trim trailing | character from scopes
settingKey = strings.TrimRight(settingKey, "|")
st[settingKey] = settingVal
}
return st
}
func resourceIllumioWorkloadSettingsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
d.SetId(fmt.Sprintf("/orgs/%v/settings/workloads", illumioClient.OrgID))
return resourceIllumioWorkloadSettingsUpdate(ctx, d, m)
}
func resourceIllumioWorkloadSettingsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
href := fmt.Sprintf("/orgs/%v/settings/workloads", illumioClient.OrgID)
_, data, err := illumioClient.Get(href, nil)
if err != nil {
return diag.FromErr(err)
}
d.SetId(href)
d.Set("href", href)
for _, k := range []string{
"workload_disconnected_timeout_seconds",
"workload_goodbye_timeout_seconds",
} {
d.Set(k, extractWorkloadSettingsTimeout(data, k))
}
return diags
}
func extractWorkloadSettingsTimeout(data *gabs.Container, key string) []map[string]interface{} {
if data.Exists(key) {
d := data.S(key)
m := []map[string]interface{}{}
for _, v := range d.Children() {
vm := extractMap(v, []string{"value", "ven_type"})
if v.Exists("scope") {
vm["scope"] = extractMapArray(v.S("scope"), []string{"href"})
} else {
vm["scope"] = []map[string]interface{}{}
}
m = append(m, vm)
}
return m
}
return nil
}
func resourceIllumioWorkloadSettingsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
workloadSettings := &models.WorkloadSettings{
WorkloadDisconnectedTimeoutSeconds: expandWorkloadSettingsTimeout(d, "workload_disconnected_timeout_seconds"),
WorkloadGoodbyeTimeoutSeconds: expandWorkloadSettingsTimeout(d, "workload_goodbye_timeout_seconds"),
}
_, err := illumioClient.Update(d.Id(), workloadSettings)
if err != nil {
return diag.FromErr(err)
}
return resourceIllumioWorkloadSettingsRead(ctx, d, m)
}
func expandWorkloadSettingsTimeout(d *schema.ResourceData, key string) []models.WorkloadSettingsTimeout {
var timeoutSettings []models.WorkloadSettingsTimeout
if items, ok := d.GetOk(key); ok {
wts := items.(*schema.Set).List()
for _, w := range wts {
m := models.WorkloadSettingsTimeout{}
wMap := w.(map[string]interface{})
m.Value = PtrTo(wMap["value"].(int))
if wMap["scope"].(*schema.Set).Len() > 0 {
m.Scope = PtrTo(models.GetHrefs(wMap["scope"].(*schema.Set).List()))
} else {
m.Scope = &[]models.Href{}
}
if venType, ok := wMap["ven_type"].(string); ok {
m.VENType = &venType
}
timeoutSettings = append(timeoutSettings, m)
}
}
return timeoutSettings
}
func resourceIllumioWorkloadSettingsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
d.SetId("")
var diags diag.Diagnostics
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "[illumio-core_workload_settings] Ignoring Delete Operation.",
})
return diags
}