Skip to content

Commit

Permalink
Force 'okta_app_user_schema' resource recreate when changing scope (#331
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bogdanprodan-okta authored Feb 16, 2021
1 parent 6c569ef commit dc11f02
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/okta_user_schema/basic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ resource "okta_user_schema" "testAcc_replace_with_uuid" {
permissions = "READ_ONLY"
master = "PROFILE_MASTER"
enum = ["S", "M", "L", "XL"]
scope = "SELF"

one_of {
const = "S"
Expand Down
1 change: 1 addition & 0 deletions examples/okta_user_schema/updated.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ resource "okta_user_schema" "testAcc_replace_with_uuid" {
master = "OKTA"
enum = ["S", "M", "L", "XXL"]
pattern = ".+"
scope = "NONE"

one_of {
const = "S"
Expand Down
1 change: 0 additions & 1 deletion okta/provider_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func setupSweeper(resourceType string, del func(*testClient) error) {
Name: resourceType,
F: func(_ string) error {
client, apiSupplement, err := sharedClient()

if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions okta/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var testAccProvidersFactories map[string]func() (*schema.Provider, error)
var testAccProvider *schema.Provider
var (
testAccProvidersFactories map[string]func() (*schema.Provider, error)
testAccProvider *schema.Provider
)

func init() {
testAccProvider = Provider()
Expand All @@ -29,7 +31,7 @@ func TestProvider(t *testing.T) {
}

func TestProvider_impl(t *testing.T) {
var _ = Provider()
_ = Provider()
}

func oktaConfig() (*Config, error) {
Expand Down
1 change: 0 additions & 1 deletion okta/resource_okta_app_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func resourceAppGroupAssignment() *schema.Resource {

assignment, _, err := getOktaClientFromMetadata(m).Application.
GetApplicationGroupAssignment(ctx, parts[0], parts[1], nil)

if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion okta/resource_okta_app_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func resourceAppUser() *schema.Resource {

assignment, _, err := getOktaClientFromMetadata(m).Application.
GetApplicationUser(ctx, parts[0], parts[1], nil)

if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion okta/resource_okta_app_user_base_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func resourceAppUserBaseSchema() *schema.Resource {
"app_id": {
Type: schema.TypeString,
Required: true,
}}),
},
}),
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Expand Down
21 changes: 21 additions & 0 deletions okta/resource_okta_app_user_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func resourceAppUserSchema() *schema.Resource {
Default: false,
ConflictsWith: []string{"enum"},
},
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateDiagFunc: stringInSlice([]string{"SELF", "NONE", ""}),
ForceNew: true, // since the `scope` is read-only attribute, the resource should be recreated
},
}),
SchemaVersion: 2,
StateUpgraders: []schema.StateUpgrader{
Expand Down Expand Up @@ -73,6 +80,13 @@ func resourceAppUserSchemaResourceV1() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateDiagFunc: stringInSlice([]string{"SELF", "NONE", ""}),
ForceNew: true, // since the `scope` is read-only attribute, the resource should be recreated
},
}, userSchemaSchema, userBaseSchemaSchema, userTypeSchema, userPatternSchema)}
}

Expand All @@ -82,6 +96,13 @@ func resourceAppUserSchemaResourceV0() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateDiagFunc: stringInSlice([]string{"SELF", "NONE", ""}),
ForceNew: true, // since the `scope` is read-only attribute, the resource should be recreated
},
}, userSchemaSchema, userBaseSchemaSchema)}
}

Expand Down
1 change: 1 addition & 0 deletions okta/resource_okta_group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package okta
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/okta-sdk-golang/v2/okta"
Expand Down
5 changes: 3 additions & 2 deletions okta/resource_okta_group_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package okta
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccOktaGroupMembership_crud(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion okta/resource_okta_group_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package okta
import (
"context"
"fmt"
"strings"

"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/okta/okta-sdk-golang/v2/okta"
"strings"
)

func resourceGroupRole() *schema.Resource {
Expand Down
38 changes: 18 additions & 20 deletions okta/resource_okta_profile_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,25 @@ import (
"github.com/oktadeveloper/terraform-provider-okta/sdk"
)

var (
mappingResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The mapping property key.",
},
"expression": {
Type: schema.TypeString,
Required: true,
},
"push_status": {
Type: schema.TypeString,
Optional: true,
Default: dontPush,
ValidateDiagFunc: stringInSlice([]string{push, dontPush}),
},
var mappingResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The mapping property key.",
},
}
)
"expression": {
Type: schema.TypeString,
Required: true,
},
"push_status": {
Type: schema.TypeString,
Optional: true,
Default: dontPush,
ValidateDiagFunc: stringInSlice([]string{push, dontPush}),
},
},
}

const (
push = "PUSH"
Expand Down
2 changes: 1 addition & 1 deletion okta/resource_okta_user_base_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func testOktaUserBaseSchemasExists(name string) resource.TestCheckFunc {
if !ok {
return fmt.Errorf("not found: %s", name)
}
var schemaUserType = "default"
schemaUserType := "default"
if rs.Primary.Attributes["user_type"] != "" {
schemaUserType = rs.Primary.Attributes["user_type"]
}
Expand Down
17 changes: 16 additions & 1 deletion okta/resource_okta_user_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func resourceUserSchema() *schema.Resource {
userSchemaSchema,
userTypeSchema,
userPatternSchema,
map[string]*schema.Schema{
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateDiagFunc: stringInSlice([]string{"SELF", "NONE", ""}),
},
},
),
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
Expand All @@ -55,7 +63,14 @@ func resourceUserSchema() *schema.Resource {
}

func resourceUserSchemaResourceV0() *schema.Resource {
return &schema.Resource{Schema: buildSchema(userBaseSchemaSchema, userSchemaSchema)}
return &schema.Resource{Schema: buildSchema(userBaseSchemaSchema, userSchemaSchema, map[string]*schema.Schema{
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateDiagFunc: stringInSlice([]string{"SELF", "NONE", ""}),
},
})}
}

// Sometime Okta API does not update or create custom property on the first try, thus that require running
Expand Down
6 changes: 4 additions & 2 deletions okta/resource_okta_user_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestAccOktaUserSchema_crud(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "enum.3", "XL"),
resource.TestCheckResourceAttr(resourceName, "one_of.#", "4"),
resource.TestCheckResourceAttr(resourceName, "pattern", ""),
resource.TestCheckResourceAttr(resourceName, "scope", "SELF"),
),
},
{
Expand All @@ -87,6 +88,7 @@ func TestAccOktaUserSchema_crud(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "enum.3", "XXL"),
resource.TestCheckResourceAttr(resourceName, "one_of.#", "4"),
resource.TestCheckResourceAttr(resourceName, "pattern", ".+"),
resource.TestCheckResourceAttr(resourceName, "scope", "NONE"),
),
},
{
Expand Down Expand Up @@ -208,7 +210,7 @@ func TestAccOktaUserSchema_arrayString(t *testing.T) {
func checkOktaUserSchemasDestroy() resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
var schemaUserType = "default"
schemaUserType := "default"
if rs.Primary.Attributes["user_type"] != "" {
schemaUserType = rs.Primary.Attributes["user_type"]
}
Expand All @@ -229,7 +231,7 @@ func testOktaUserSchemasExists(name string) resource.TestCheckFunc {
return fmt.Errorf("not found: %s", name)
}

var schemaUserType = "default"
schemaUserType := "default"
if rs.Primary.Attributes["user_type"] != "" {
schemaUserType = rs.Primary.Attributes["user_type"]
}
Expand Down
6 changes: 4 additions & 2 deletions okta/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,12 @@ func populateUserProfile(d *schema.ResourceData) *okta.UserProfile {
profile["login"] = d.Get("login").(string)
profile["email"] = d.Get("email").(string)

getSetParams := []string{"city", "costCenter", "countryCode", "department", "displayName", "division",
getSetParams := []string{
"city", "costCenter", "countryCode", "department", "displayName", "division",
"employeeNumber", "honorificPrefix", "honorificSuffix", "locale", "manager", "managerId", "middleName",
"mobilePhone", "nickName", "organization", "preferredLanguage", "primaryPhone", "profileUrl",
"secondEmail", "state", "streetAddress", "timezone", "title", "userType", "zipCode"}
"secondEmail", "state", "streetAddress", "timezone", "title", "userType", "zipCode",
}

for i := range getSetParams {
if res, ok := d.GetOk(camelCaseToUnderscore(getSetParams[i])); ok {
Expand Down
6 changes: 0 additions & 6 deletions okta/user_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ var (
ConflictsWith: []string{"array_type"},
Elem: &schema.Schema{Type: schema.TypeString},
},
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateDiagFunc: stringInSlice([]string{"SELF", "NONE", ""}),
},
"one_of": {
Type: schema.TypeList,
ForceNew: true,
Expand Down
2 changes: 0 additions & 2 deletions sdk/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type (
func (m *ApiSupplement) GetProfileMappingBySourceId(ctx context.Context, sourceId, targetId string) (*Mapping, *okta.Response, error) {
url := fmt.Sprintf("/api/v1/mappings?sourceId=%s&targetId=%s", sourceId, targetId)
req, err := m.RequestExecutor.NewRequest("GET", url, nil)

if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -90,7 +89,6 @@ func (m *ApiSupplement) FindProfileMappingSource(ctx context.Context, name, typ
}

req, err := m.RequestExecutor.NewRequest("GET", uri, nil)

if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit dc11f02

Please sign in to comment.