/
resource_okta_group_custom_schema_property.go
349 lines (323 loc) · 11.8 KB
/
resource_okta_group_custom_schema_property.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package okta
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/terraform-provider-okta/sdk"
)
func resourceGroupCustomSchemaProperty() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGroupSchemaCreateOrUpdate,
ReadContext: resourceGroupSchemaRead,
UpdateContext: resourceGroupSchemaCreateOrUpdate,
DeleteContext: resourceGroupSchemaDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Description: `Creates a Group Schema property.
This resource allows you to create and configure a custom group schema property.
**IMPORTANT:** With 'enum', list its values as strings even though the 'type'
may be something other than string. This is a limitation of the schema defintion
in the Terraform Plugin SDK runtime and we juggle the type correctly when making
Okta API calls. Same holds for the 'const' value of 'one_of' as well as the
'array_*' variation of 'enum' and 'one_of'.`,
Schema: buildSchema(
userBaseSchemaSchema,
userSchemaSchema,
map[string]*schema.Schema{
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},
"master": {
Type: schema.TypeString,
Optional: true,
Description: "Master priority for the group schema property. It can be set to `PROFILE_MASTER`, `OVERRIDE` or `OKTA`. Default: `PROFILE_MASTER`",
Default: "PROFILE_MASTER",
},
"master_override_priority": {
Type: schema.TypeList,
Optional: true,
Description: "Prioritized list of profile sources (required when `master` is `OVERRIDE`).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Default: "APP",
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
),
}
}
// Sometime Okta API does not update or create custom property on the first try, thus that require running
// `terraform apply` several times. This simple retry resolves that issue. (If) After this issue will be resolved,
// this retry logic will be demolished.
func resourceGroupSchemaCreateOrUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
logger(m).Info("creating group custom schema property", "name", d.Get("index").(string))
err := validateUserSchema(d)
if err != nil {
return diag.FromErr(err)
}
groupCustomSchemaAttribute, err := buildGroupCustomSchemaAttribute(d)
if err != nil {
return diag.FromErr(err)
}
custom := buildCustomGroupSchema(d.Get("index").(string), groupCustomSchemaAttribute)
subSchema, err := alterCustomGroupSchema(ctx, m, d.Get("index").(string), custom, false)
if err != nil {
return diag.Errorf("failed to create or update group custom schema property %s: %v", d.Get("index").(string), err)
}
d.SetId(d.Get("index").(string))
err = syncCustomGroupSchema(d, subSchema)
if err != nil {
return diag.Errorf("failed to set group custom schema property: %v", err)
}
return nil
}
func resourceGroupSchemaRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
logger(m).Info("reading group custom schema property", "name", d.Get("index").(string))
s, _, err := getOktaClientFromMetadata(m).GroupSchema.GetGroupSchema(ctx)
if err != nil {
return diag.Errorf("failed to get group custom schema property: %v", err)
}
customAttribute := groupSchemaCustomAttribute(s, d.Id())
if customAttribute == nil {
d.SetId("")
return nil
}
_ = d.Set("index", d.Id())
err = syncCustomGroupSchema(d, customAttribute)
if err != nil {
return diag.Errorf("failed to set group custom schema property: %v", err)
}
return nil
}
func alterCustomGroupSchema(ctx context.Context, m interface{}, index string, schema *sdk.GroupSchema, isDeleteOperation bool) (*sdk.GroupSchemaAttribute, error) {
var schemaAttribute *sdk.GroupSchemaAttribute
boc := newExponentialBackOffWithContext(ctx, 120*time.Second)
err := backoff.Retry(func() error {
// NOTE: Enums on the schema can be typed other than string but the
// Terraform SDK is staticly defined at runtime for string so we need to
// juggle types on the fly.
retypeGroupSchemaPropertyEnums(schema)
updated, resp, err := getOktaClientFromMetadata(m).GroupSchema.UpdateGroupSchema(ctx, *schema)
stringifyGroupSchemaPropertyEnums(schema)
if doNotRetry(m, err) {
return backoff.Permanent(err)
}
if err != nil {
if resp != nil && resp.StatusCode == 500 {
return fmt.Errorf("updating group custom schema property caused 500 error: %w", err)
}
if strings.Contains(err.Error(), "Wait until the data clean up process finishes and then try again") {
return err
}
return backoff.Permanent(err)
}
s, _, err := getOktaClientFromMetadata(m).GroupSchema.GetGroupSchema(ctx)
if err != nil {
return backoff.Permanent(fmt.Errorf("failed to get group custom schema property: %v", err))
}
schemaAttribute = groupSchemaCustomAttribute(s, index)
if isDeleteOperation && schemaAttribute == nil {
return nil
} else if schemaAttribute != nil && reflect.DeepEqual(schemaAttribute, updated.Definitions.Custom.Properties[index]) {
return nil
}
return errors.New("failed to apply changes after several retries")
}, boc)
if err != nil {
logger(m).Error("failed to apply changes after several retries", err)
}
return schemaAttribute, err
}
func resourceGroupSchemaDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
custom := buildCustomGroupSchema(d.Id(), nil)
_, err := alterCustomGroupSchema(ctx, m, d.Get("index").(string), custom, true)
if err != nil {
return diag.Errorf("failed to delete group schema property %s: %v", d.Get("index").(string), err)
}
return nil
}
func buildCustomGroupSchema(index string, schema *sdk.GroupSchemaAttribute) *sdk.GroupSchema {
return &sdk.GroupSchema{
Definitions: &sdk.GroupSchemaDefinitions{
Custom: &sdk.GroupSchemaCustom{
Id: "#custom",
Properties: map[string]*sdk.GroupSchemaAttribute{
index: schema,
},
Type: "object",
},
},
}
}
func syncCustomGroupSchema(d *schema.ResourceData, subschema *sdk.GroupSchemaAttribute) error {
syncBaseGroupSchema(d, subschema)
_ = d.Set("description", subschema.Description)
if subschema.MinLengthPtr != nil {
_ = d.Set("min_length", *subschema.MinLengthPtr)
}
if subschema.MaxLengthPtr != nil {
_ = d.Set("max_length", *subschema.MaxLengthPtr)
}
_ = d.Set("scope", subschema.Scope)
_ = d.Set("external_name", subschema.ExternalName)
_ = d.Set("external_namespace", subschema.ExternalNamespace)
_ = d.Set("unique", subschema.Unique)
// NOTE: Enums on the schema can be typed other than string but the
// Terraform SDK is staticly defined at runtime for string so we need to
// juggle types on the fly.
if subschema.Items != nil {
stringifyOneOfSlice(subschema.Items.Type, &subschema.Items.OneOf)
stringifyEnumSlice(subschema.Items.Type, &subschema.Items.Enum)
_ = d.Set("array_type", subschema.Items.Type)
_ = d.Set("array_one_of", flattenOneOf(subschema.Items.OneOf))
_ = d.Set("array_enum", subschema.Items.Enum)
}
stringifyOneOfSlice(subschema.Type, &subschema.OneOf)
stringifyEnumSlice(subschema.Type, &subschema.Enum)
if len(subschema.Enum) > 0 {
_ = d.Set("enum", subschema.Enum)
}
return setNonPrimitives(d, map[string]interface{}{
"one_of": flattenOneOf(subschema.OneOf),
})
}
func syncBaseGroupSchema(d *schema.ResourceData, subschema *sdk.GroupSchemaAttribute) {
_ = d.Set("title", subschema.Title)
_ = d.Set("type", subschema.Type)
_ = d.Set("required", subschema.Required)
if subschema.Master != nil {
_ = d.Set("master", subschema.Master.Type)
if subschema.Master.Type == "OVERRIDE" {
arr := make([]map[string]interface{}, len(subschema.Master.Priority))
for i, st := range subschema.Master.Priority {
arr[i] = map[string]interface{}{
"type": st.Type,
"value": st.Value,
}
}
_ = setNonPrimitives(d, map[string]interface{}{"master_override_priority": arr})
}
}
if len(subschema.Permissions) > 0 {
_ = d.Set("permissions", subschema.Permissions[0].Action)
}
}
func buildGroupCustomSchemaAttribute(d *schema.ResourceData) (*sdk.GroupSchemaAttribute, error) {
items, err := buildNullableItems(d)
if err != nil {
return nil, err
}
var oneOf []*sdk.UserSchemaAttributeEnum
if rawOneOf, ok := d.GetOk("one_of"); ok {
oneOf, err = buildOneOf(rawOneOf.([]interface{}), d.Get("type").(string))
if err != nil {
return nil, err
}
}
attribute := &sdk.GroupSchemaAttribute{
Title: d.Get("title").(string),
Type: d.Get("type").(string),
Description: d.Get("description").(string),
Required: boolPtr(d.Get("required").(bool)),
Permissions: []*sdk.UserSchemaAttributePermission{
{
Action: d.Get("permissions").(string),
Principal: "SELF",
},
},
Scope: d.Get("scope").(string),
Master: getNullableMaster(d),
Items: items,
OneOf: oneOf,
ExternalName: d.Get("external_name").(string),
ExternalNamespace: d.Get("external_namespace").(string),
Unique: d.Get("unique").(string),
}
if min, ok := d.GetOk("min_length"); ok {
attribute.MinLengthPtr = int64Ptr(min.(int))
}
if max, ok := d.GetOk("max_length"); ok {
attribute.MaxLengthPtr = int64Ptr(max.(int))
}
if rawEnum, ok := d.GetOk("enum"); ok {
attribute.Enum = rawEnum.([]interface{})
}
return attribute, nil
}
func groupSchemaCustomAttribute(s *sdk.GroupSchema, index string) *sdk.GroupSchemaAttribute {
if s == nil || s.Definitions == nil || s.Definitions.Custom == nil {
return nil
}
return s.Definitions.Custom.Properties[index]
}
// retypeGroupSchemaPropertyEnums takes a schema and ensures the enums in its
// GroupSchemaAttribute(s) have the correct golang type values instead of the
// strings limitation due to the TF SDK.
func retypeGroupSchemaPropertyEnums(schema *sdk.GroupSchema) {
if schema.Definitions != nil && schema.Definitions.Base != nil {
retypeGroupPropertiesEnum(schema.Definitions.Base.Properties)
}
if schema.Definitions != nil && schema.Definitions.Custom != nil {
retypeGroupPropertiesEnum(schema.Definitions.Custom.Properties)
}
}
// stringifyGroupSchemaPropertyEnums takes a schema and ensures the enums in its
// GroupSchemaAttribute(s) have string values to satisfy the TF schema
func stringifyGroupSchemaPropertyEnums(schema *sdk.GroupSchema) {
if schema.Definitions != nil && schema.Definitions.Base != nil {
stringifyGroupPropertiesEnum(schema.Definitions.Base.Properties)
}
if schema.Definitions != nil && schema.Definitions.Custom != nil {
stringifyGroupPropertiesEnum(schema.Definitions.Custom.Properties)
}
}
func retypeGroupPropertiesEnum(properties map[string]*sdk.GroupSchemaAttribute) {
for _, val := range properties {
if val == nil {
continue
}
enum := retypeEnumSlice(val.Type, val.Enum)
val.Enum = enum
attributeEnum := retypeOneOfSlice(val.Type, val.OneOf)
val.OneOf = attributeEnum
if val.Items != nil {
enum := retypeEnumSlice(val.Items.Type, val.Items.Enum)
val.Items.Enum = enum
retypeOneOfSlice(val.Type, val.OneOf)
attributeEnum := retypeOneOfSlice(val.Items.Type, val.Items.OneOf)
val.Items.OneOf = attributeEnum
}
}
}
func stringifyGroupPropertiesEnum(properties map[string]*sdk.GroupSchemaAttribute) {
for _, val := range properties {
if val != nil && val.Enum != nil {
stringifyEnumSlice(val.Type, &val.Enum)
}
if val != nil && val.OneOf != nil {
stringifyOneOfSlice(val.Type, &val.OneOf)
}
if val != nil && val.Items != nil {
stringifyEnumSlice(val.Items.Type, &val.Items.Enum)
stringifyOneOfSlice(val.Items.Type, &val.Items.OneOf)
}
}
}