Skip to content

Commit

Permalink
Merge pull request #1178 from okta/issue_okta_user_schema_property
Browse files Browse the repository at this point in the history
Fixes variable type with enum and one_of in user and group schemas (old JSON serialization errors)
  • Loading branch information
monde authored Jun 27, 2022
2 parents d000657 + 1bb4a32 commit 84aaf65
Show file tree
Hide file tree
Showing 13 changed files with 1,709 additions and 41 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/hashicorp/go-hclog v1.2.1
github.com/hashicorp/go-retryablehttp v0.7.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0
github.com/okta/okta-sdk-golang/v2 v2.12.2-0.20220602195034-d7ea6917663f
github.com/okta/okta-sdk-golang/v2 v2.13.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/okta/okta-sdk-golang/v2 v2.12.2-0.20220602195034-d7ea6917663f h1:6zPIZb9jjYbKfCdkpjERA6tpQKIiX+4+d4WJ8kEJJ8g=
github.com/okta/okta-sdk-golang/v2 v2.12.2-0.20220602195034-d7ea6917663f/go.mod h1:aL3K0likfyLVapi33OsegX+KJf4c6SDapDhlUcXFEvk=
github.com/okta/okta-sdk-golang/v2 v2.13.0 h1:Z5fmqKcFqJCJ1GCRVezElYqUgeL+8jrPybO7nVJjAm0=
github.com/okta/okta-sdk-golang/v2 v2.13.0/go.mod h1:aL3K0likfyLVapi33OsegX+KJf4c6SDapDhlUcXFEvk=
github.com/patrickmn/go-cache v0.0.0-20180815053127-5633e0862627 h1:pSCLCl6joCFRnjpeojzOpEYs4q7Vditq8fySFG5ap3Y=
github.com/patrickmn/go-cache v0.0.0-20180815053127-5633e0862627/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
4 changes: 4 additions & 0 deletions okta/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ func (manager *fixtureManager) GetFixtures(fixtureName string, rInt int, t *test
return tfConfig
}

return manager.ConfigReplace(tfConfig, rInt)
}

func (manager *fixtureManager) ConfigReplace(tfConfig string, rInt int) string {
return strings.ReplaceAll(tfConfig, uuidPattern, fmt.Sprintf("%d", rInt))
}
87 changes: 83 additions & 4 deletions okta/resource_okta_group_custom_schema_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ func alterCustomGroupSchema(ctx context.Context, m interface{}, index string, sc
bc := backoff.WithContext(bOff, ctx)

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 err != nil {
logger(m).Error(err.Error())
if resp != nil && resp.StatusCode == 500 {
Expand Down Expand Up @@ -177,14 +185,26 @@ func syncCustomGroupSchema(d *schema.ResourceData, subschema *okta.GroupSchemaAt
_ = 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", strToInterfaceSlice(subschema.Items.Enum))
_ = 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", strToInterfaceSlice(subschema.Enum))
_ = d.Set("enum", subschema.Enum)
}

return setNonPrimitives(d, map[string]interface{}{
"one_of": flattenOneOf(subschema.OneOf),
})
Expand Down Expand Up @@ -224,9 +244,9 @@ func buildGroupCustomSchemaAttribute(d *schema.ResourceData) (*okta.GroupSchemaA
return nil, err
}
}
var enum []string
var enum []interface{}
if rawEnum, ok := d.GetOk("enum"); ok {
enum = buildStringSlice(rawEnum.([]interface{}))
enum = rawEnum.([]interface{})
}
return &okta.GroupSchemaAttribute{
Title: d.Get("title").(string),
Expand Down Expand Up @@ -258,3 +278,62 @@ func groupSchemaCustomAttribute(s *okta.GroupSchema, index string) *okta.GroupSc
}
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 *okta.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 *okta.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]*okta.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]*okta.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)
}

}
}
Loading

0 comments on commit 84aaf65

Please sign in to comment.