/
resource_illumio_label_group.go
317 lines (283 loc) · 8.72 KB
/
resource_illumio_label_group.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
314
315
316
317
// Copyright 2021 Illumio, Inc. All Rights Reserved.
package illumiocore
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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/client"
"github.com/illumio/terraform-provider-illumio-core/models"
)
func resourceIllumioLabelGroup() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIllumioLabelGroupCreate,
ReadContext: resourceIllumioLabelGroupRead,
UpdateContext: resourceIllumioLabelGroupUpdate,
DeleteContext: resourceIllumioLabelGroupDelete,
SchemaVersion: 1,
Description: "Manages Illumio Label Group",
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "URI of this label group",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the label group",
ValidateDiagFunc: nameValidation,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The long description of the label group",
},
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringLenBetween(1, LABEL_KEY_LENGTH_MAX),
),
Description: `Key in key-value pair of contained labels or label groups. The value must be a string between 1 and 64 characters long`,
},
"labels": {
Type: schema.TypeSet,
Optional: true,
Description: "Contained labels",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Required: true,
Description: "URI of label",
},
"key": {
Type: schema.TypeString,
Computed: true,
Description: "Key in key-value pair",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Value in key-value pair",
},
},
},
},
"sub_groups": {
Type: schema.TypeSet,
Optional: true,
Description: "Contained label groups",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Required: true,
Description: "URI of label group",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Key in key-value pair",
},
},
},
},
"external_data_set": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty),
Description: "The data source from which a resource originates",
},
"external_data_reference": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty),
Description: "A unique identifier within the external data source",
},
"update_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of update",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this label group was first created",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this label group was last updated",
},
"deleted_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this label group was last deleted",
},
"created_by": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "User who created this label group",
},
"updated_by": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "User who last updated this label group",
},
"deleted_by": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "User who deleted this label group",
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceIllumioLabelGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
orgID := illumioClient.OrgID
labels := models.GetHrefs(d.Get("labels").(*schema.Set).List())
subGroups := models.GetHrefs(d.Get("sub_groups").(*schema.Set).List())
labelGroup := &models.LabelGroup{
Name: PtrTo(d.Get("name").(string)),
Key: PtrTo(d.Get("key").(string)),
Description: PtrTo(d.Get("description").(string)),
ExternalDataSet: d.Get("external_data_set").(string),
ExternalDataReference: d.Get("external_data_reference").(string),
Labels: &labels,
SubGroups: &subGroups,
}
_, data, err := illumioClient.Create(fmt.Sprintf("/orgs/%d/sec_policy/draft/label_groups", orgID), labelGroup)
if err != nil {
return diag.FromErr(err)
}
pConfig.StoreHref("label_groups", data.S("href").Data().(string))
d.SetId(data.S("href").Data().(string))
return resourceIllumioLabelGroupRead(ctx, d, m)
}
func resourceIllumioLabelGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diagnostics diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
href := d.Id()
_, data, err := illumioClient.Get(href, nil)
if err != nil {
return diag.FromErr(err)
}
// set computed/optional values from api response
for _, key := range []string{
"href",
"name",
"description",
"key",
"external_data_set",
"external_data_reference",
"update_type",
"created_at",
"updated_at",
"deleted_at",
"created_by",
"updated_by",
"deleted_by",
} {
if data.Exists(key) {
d.Set(key, data.S(key).Data())
} else {
d.Set(key, nil)
}
}
if data.Exists("labels") {
labels := data.S("labels")
labelI := []map[string]interface{}{}
for _, l := range labels.Children() {
labelI = append(labelI, extractMap(l, []string{"href", "key", "value"}))
}
d.Set("labels", labelI)
} else {
d.Set("labels", nil)
}
if data.Exists("sub_groups") {
sub_groups := data.S("sub_groups")
sub_groupI := []map[string]interface{}{}
for _, sg := range sub_groups.Children() {
sub_groupI = append(sub_groupI, extractMap(sg, []string{"href", "name"}))
}
d.Set("sub_groups", sub_groupI)
} else {
d.Set("sub_groups", nil)
}
return diagnostics
}
func resourceIllumioLabelGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
labels := models.GetHrefs(d.Get("labels").(*schema.Set).List())
subGroups := models.GetHrefs(d.Get("sub_groups").(*schema.Set).List())
labelGroup := &models.LabelGroup{
Name: PtrTo(d.Get("name").(string)),
Description: PtrTo(d.Get("description").(string)),
ExternalDataSet: d.Get("external_data_set").(string),
ExternalDataReference: d.Get("external_data_reference").(string),
Labels: &labels,
SubGroups: &subGroups,
}
_, err := illumioClient.Update(d.Id(), labelGroup)
if err != nil {
return diag.FromErr(err)
}
pConfig.StoreHref("label_groups", d.Id())
return resourceIllumioLabelGroupRead(ctx, d, m)
}
func resourceIllumioLabelGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diagnostics diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
href := d.Id()
// if the label group is a child of any other label groups
// that aren't in the TF state, get their HREFs and add them
// to hrefs.csv for provisioning
parentHrefs, err := getParentGroupHrefs(illumioClient, href)
if err != nil {
return diag.FromErr(err)
}
for _, href := range parentHrefs {
pConfig.StoreHref("label_groups", href)
}
_, err = illumioClient.Delete(href)
if err != nil {
return diag.FromErr(err)
}
pConfig.StoreHref("label_groups", href)
d.SetId("")
return diagnostics
}
func getParentGroupHrefs(client *client.V2, labelGroupHref string) ([]string, error) {
_, data, err := client.Get(labelGroupHref+"/member_of", nil)
if err != nil {
return nil, err
}
parentGroupContainers := data.Children()
parentHrefs := make([]string, 0, len(parentGroupContainers))
for _, child := range parentGroupContainers {
if child.Exists("href") {
parentHrefs = append(parentHrefs, child.S("href").Data().(string))
}
}
return parentHrefs, nil
}