diff --git a/okta/config.go b/okta/config.go index e00d627e8..e9a46deb4 100644 --- a/okta/config.go +++ b/okta/config.go @@ -53,7 +53,7 @@ func (c *Config) loadAndValidate() error { okta.WithHttpClient(*httpClient), okta.WithRateLimitMaxBackOff(int64(c.maxWait)), okta.WithRateLimitMaxRetries(int32(c.retryCount)), - okta.WithUserAgentExtra("okta-terraform/3.6.0"), + okta.WithUserAgentExtra("okta-terraform/3.7.1"), ) if err != nil { return err diff --git a/okta/provider_sweeper_test.go b/okta/provider_sweeper_test.go index d52e67aa5..4119f7066 100644 --- a/okta/provider_sweeper_test.go +++ b/okta/provider_sweeper_test.go @@ -39,6 +39,7 @@ func TestMain(m *testing.M) { setupSweeper(userBaseSchema, sweepUserBaseSchema) setupSweeper(networkZone, sweepNetworkZones) setupSweeper(inlineHook, sweepInlineHooks) + setupSweeper(userType, sweepUserTypes) // add zones sweeper resource.TestMain(m) diff --git a/okta/resource_okta_app_user_base_schema.go b/okta/resource_okta_app_user_base_schema.go index cf5d68c63..a803aeb2e 100644 --- a/okta/resource_okta_app_user_base_schema.go +++ b/okta/resource_okta_app_user_base_schema.go @@ -22,9 +22,29 @@ func resourceAppUserBaseSchema() *schema.Resource { Required: true, }, }), + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceAppUserBaseSchemaResourceV0().CoreConfigSchema().ImpliedType(), + Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + rawState["user_type"] = "default" + return rawState, nil + }, + Version: 0, + }, + }, } } +func resourceAppUserBaseSchemaResourceV0() *schema.Resource { + return &schema.Resource{Schema: buildSchema(map[string]*schema.Schema{ + "app_id": { + Type: schema.TypeString, + Required: true, + }, + }, userBaseSchemaSchema)} +} + func resourceAppUserBaseSchemaCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if err := updateAppUserBaseSubschema(ctx, d, m); err != nil { return err diff --git a/okta/resource_okta_app_user_schema.go b/okta/resource_okta_app_user_schema.go index d774cb2c4..c9bf5789e 100644 --- a/okta/resource_okta_app_user_schema.go +++ b/okta/resource_okta_app_user_schema.go @@ -21,9 +21,29 @@ func resourceAppUserSchema() *schema.Resource { Required: true, }, }), + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceAppUserSchemaResourceV0().CoreConfigSchema().ImpliedType(), + Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + rawState["user_type"] = "default" + return rawState, nil + }, + Version: 0, + }, + }, } } +func resourceAppUserSchemaResourceV0() *schema.Resource { + return &schema.Resource{Schema: buildSchema(map[string]*schema.Schema{ + "app_id": { + Type: schema.TypeString, + Required: true, + }, + }, userSchemaSchema, userBaseSchemaSchema)} +} + func resourceAppUserSchemaCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if err := updateAppUserSubschema(ctx, d, m); err != nil { return err diff --git a/okta/resource_okta_user_base_schema.go b/okta/resource_okta_user_base_schema.go index 73a2d8dd6..3ffafe04c 100644 --- a/okta/resource_okta_user_base_schema.go +++ b/okta/resource_okta_user_base_schema.go @@ -32,10 +32,25 @@ func resourceUserBaseSchema() *schema.Resource { return []*schema.ResourceData{d}, nil }, }, - Schema: userBaseSchemaSchema, + SchemaVersion: 1, + Schema: buildSchema(userBaseSchemaSchema, userTypeSchema), + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceUserBaseSchemaResourceV0().CoreConfigSchema().ImpliedType(), + Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + rawState["user_type"] = "default" + return rawState, nil + }, + Version: 0, + }, + }, } } +func resourceUserBaseSchemaResourceV0() *schema.Resource { + return &schema.Resource{Schema: userBaseSchemaSchema} +} + func resourceUserBaseSchemaCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { schemaUrl, err := getUserTypeSchemaUrl(ctx, getOktaClientFromMetadata(m), d.Get("user_type").(string)) if err != nil { diff --git a/okta/resource_okta_user_schema.go b/okta/resource_okta_user_schema.go index 527f23550..01004d681 100644 --- a/okta/resource_okta_user_schema.go +++ b/okta/resource_okta_user_schema.go @@ -33,10 +33,25 @@ func resourceUserSchema() *schema.Resource { return []*schema.ResourceData{d}, nil }, }, - Schema: userSchemaSchema, + Schema: buildBaseUserSchema(userSchemaSchema), + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceUserSchemaResourceV0().CoreConfigSchema().ImpliedType(), + Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + rawState["user_type"] = "default" + return rawState, nil + }, + Version: 0, + }, + }, } } +func resourceUserSchemaResourceV0() *schema.Resource { + return &schema.Resource{Schema: buildSchema(userBaseSchemaSchema, userSchemaSchema)} +} + func resourceUserSchemaCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { schemaUrl, err := getUserTypeSchemaUrl(ctx, getOktaClientFromMetadata(m), d.Get("user_type").(string)) if err != nil { diff --git a/okta/resource_okta_user_type_test.go b/okta/resource_okta_user_type_test.go index 08c7d7c0d..cccf86ec2 100644 --- a/okta/resource_okta_user_type_test.go +++ b/okta/resource_okta_user_type_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -11,6 +12,19 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) +func sweepUserTypes(client *testClient) error { + userTypeList, _, _ := client.oktaClient.UserType.ListUserTypes(context.Background()) + var errorList []error + for _, ut := range userTypeList { + if strings.HasPrefix(ut.Name, testResourcePrefix) { + if _, err := client.oktaClient.UserType.DeleteUserType(context.Background(), ut.Id); err != nil { + errorList = append(errorList, err) + } + } + } + return condenseError(errorList) +} + func TestAccOktaUserType_crud(t *testing.T) { ri := acctest.RandInt() resourceName := fmt.Sprintf("%s.test", userType) diff --git a/okta/user_schema.go b/okta/user_schema.go index 1675c615f..bfd8d6718 100644 --- a/okta/user_schema.go +++ b/okta/user_schema.go @@ -7,24 +7,6 @@ import ( var ( userSchemaSchema = map[string]*schema.Schema{ - "index": { - Type: schema.TypeString, - Required: true, - Description: "Subschema unique string identifier", - ForceNew: true, - }, - "title": { - Type: schema.TypeString, - Required: true, - Description: "Subschema title (display name)", - }, - "type": { - Type: schema.TypeString, - Required: true, - ValidateDiagFunc: stringInSlice([]string{"string", "boolean", "number", "integer", "array", "object"}), - Description: "Subschema type: string, boolean, number, integer, array, or object", - ForceNew: true, - }, "array_type": { Type: schema.TypeString, Optional: true, @@ -64,11 +46,6 @@ var ( Optional: true, Description: "Custom Subschema description", }, - "required": { - Type: schema.TypeBool, - Optional: true, - Description: "Whether the subschema is required", - }, "min_length": { Type: schema.TypeInt, Optional: true, @@ -116,20 +93,6 @@ var ( }, }, }, - "permissions": { - Type: schema.TypeString, - Optional: true, - ValidateDiagFunc: stringInSlice([]string{"HIDE", "READ_ONLY", "READ_WRITE"}), - Description: "SubSchema permissions: HIDE, READ_ONLY, or READ_WRITE.", - Default: "READ_ONLY", - }, - "master": { - Type: schema.TypeString, - Optional: true, - // Accepting an empty value to allow for zero value (when provisioning is off) - ValidateDiagFunc: stringInSlice([]string{"PROFILE_MASTER", "OKTA", ""}), - Description: "SubSchema profile manager, if not set it will inherit its setting.", - }, "external_name": { Type: schema.TypeString, Optional: true, @@ -149,12 +112,6 @@ var ( ValidateDiagFunc: stringInSlice([]string{"UNIQUE_VALIDATED", "NOT_UNIQUE"}), ConflictsWith: []string{"one_of", "enum", "array_type"}, }, - "user_type": { - Type: schema.TypeString, - Optional: true, - Description: "Custom subschema user type", - Default: "default", - }, } userBaseSchemaSchema = map[string]*schema.Schema{ @@ -195,21 +152,25 @@ var ( Optional: true, Description: "Whether the subschema is required", }, + } + + userTypeSchema = map[string]*schema.Schema{ "user_type": { - Type: schema.TypeString, - Optional: true, - Description: "Custom subschema user type", - Default: "default", + Type: schema.TypeString, + Optional: true, + Description: "Custom subschema user type", + Default: "default", + ValidateDiagFunc: stringAtLeast(7), }, } ) func buildBaseUserSchema(target map[string]*schema.Schema) map[string]*schema.Schema { - return buildSchema(userBaseSchemaSchema, target) + return buildSchema(userBaseSchemaSchema, userTypeSchema, target) } func buildCustomUserSchema(target map[string]*schema.Schema) map[string]*schema.Schema { - return buildSchema(userSchemaSchema, target) + return buildSchema(userSchemaSchema, userBaseSchemaSchema, userTypeSchema, target) } func syncUserSchema(d *schema.ResourceData, subschema *sdk.UserSubSchema) error { diff --git a/okta/validators.go b/okta/validators.go index 5ae4da3f0..ff2ef0379 100644 --- a/okta/validators.go +++ b/okta/validators.go @@ -114,7 +114,20 @@ func stringLenBetween(min, max int) schema.SchemaValidateDiagFunc { return diag.Errorf("expected type of %s to be string", k) } if len(v) < min || len(v) > max { - return diag.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, min, max, v) + return diag.Errorf("expected length of %s to be in the range (%d - %d), got %d", k, min, max, len(v)) + } + return nil + } +} + +func stringAtLeast(min int) schema.SchemaValidateDiagFunc { + return func(i interface{}, k cty.Path) diag.Diagnostics { + v, ok := i.(string) + if !ok { + return diag.Errorf("expected type of %s to be string", k) + } + if len(strings.TrimSpace(v)) < min { + return diag.Errorf("expected minimum length of %s to be %d, got %d", k, min, len(v)) } return nil }