Skip to content

Commit

Permalink
Fix infinite plan on user metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jul 18, 2022
1 parent 82d87a1 commit b94b80d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 43 deletions.
41 changes: 37 additions & 4 deletions auth0/resource_auth0_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,54 @@ func expandUser(d *schema.ResourceData) (*management.User, error) {
Blocked: Bool(d, "blocked"),
}

userMeta, err := JSON(d, "user_metadata")
userMetadata, err := expandMetadata(d, "user")
if err != nil {
return nil, err
}
user.UserMetadata = &userMeta
user.UserMetadata = &userMetadata

appMeta, err := JSON(d, "app_metadata")
appMetadata, err := expandMetadata(d, "app")
if err != nil {
return nil, err
}
user.AppMetadata = &appMeta
user.AppMetadata = &appMetadata

return user, nil
}

func expandMetadata(d *schema.ResourceData, metadataType string) (map[string]interface{}, error) {
if !d.HasChange(metadataType + "_metadata") {
return map[string]interface{}{}, nil
}

oldMetadata, newMetadata := d.GetChange(metadataType + "_metadata")
if oldMetadata == "" {
return JSON(d, metadataType+"_metadata")
}

if newMetadata == "" || newMetadata == nil {
return map[string]interface{}{}, nil
}

oldMap, err := structure.ExpandJsonFromString(oldMetadata.(string))
if err != nil {
return map[string]interface{}{}, err
}

newMap, err := structure.ExpandJsonFromString(newMetadata.(string))
if err != nil {
return map[string]interface{}{}, err
}

for key := range oldMap {
if _, ok := newMap[key]; !ok {
newMap[key] = nil
}
}

return newMap, nil
}

func flattenUserRoles(roleList *management.RoleList) []interface{} {
var roles []interface{}
for _, role := range roleList.Roles {
Expand Down
63 changes: 24 additions & 39 deletions auth0/resource_auth0_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,14 @@ resource auth0_user user {
family_name = "Lastname"
nickname = "{{.testName}}"
picture = "https://www.example.com/picture.jpg"
user_metadata = <<EOF
{
"foo": "bar",
"bar": { "baz": "qux" }
}
EOF
app_metadata = <<EOF
{
"foo": "bar",
"bar": { "baz": "qux" }
}
EOF
user_metadata = jsonencode({
"foo": "bar",
"baz": "qux"
})
app_metadata = jsonencode({
"foo": "bar",
"baz": "qux"
})
}
`

Expand All @@ -104,18 +100,12 @@ resource auth0_user user {
nickname = "{{.testName}}"
picture = "https://www.example.com/picture.jpg"
roles = [ auth0_role.owner.id, auth0_role.admin.id ]
user_metadata = <<EOF
{
"foo": "bar",
"bar": { "baz": "qux" }
}
EOF
app_metadata = <<EOF
{
"foo": "bar",
"bar": { "baz": "qux" }
}
EOF
user_metadata = jsonencode({
"foo": "bars",
})
app_metadata = jsonencode({
"foo": "bars",
})
}
resource auth0_role owner {
Expand Down Expand Up @@ -144,18 +134,6 @@ resource auth0_user user {
nickname = "{{.testName}}"
picture = "https://www.example.com/picture.jpg"
roles = [ auth0_role.admin.id ]
user_metadata = <<EOF
{
"foo": "bar",
"bar": { "baz": "qux" }
}
EOF
app_metadata = <<EOF
{
"foo": "bar",
"bar": { "baz": "qux" }
}
EOF
}
resource auth0_role admin {
Expand All @@ -173,15 +151,18 @@ func TestAccUser(t *testing.T) {
{
Config: template.ParseTestName(testAccUserCreate, strings.ToLower(t.Name())),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_user.user", "connection_name", "Username-Password-Authentication"),
resource.TestCheckResourceAttr("auth0_user.user", "username", strings.ToLower(t.Name())),
resource.TestCheckResourceAttr("auth0_user.user", "user_id", fmt.Sprintf("auth0|%s", strings.ToLower(t.Name()))),
resource.TestCheckResourceAttr("auth0_user.user", "email", fmt.Sprintf("%s@acceptance.test.com", strings.ToLower(t.Name()))),
resource.TestCheckResourceAttr("auth0_user.user", "name", "Firstname Lastname"),
resource.TestCheckResourceAttr("auth0_user.user", "family_name", "Lastname"),
resource.TestCheckResourceAttr("auth0_user.user", "given_name", "Firstname"),
resource.TestCheckResourceAttr("auth0_user.user", "family_name", "Lastname"),
resource.TestCheckResourceAttr("auth0_user.user", "nickname", strings.ToLower(t.Name())),
resource.TestCheckResourceAttr("auth0_user.user", "connection_name", "Username-Password-Authentication"),
resource.TestCheckResourceAttr("auth0_user.user", "roles.#", "0"),
resource.TestCheckResourceAttr("auth0_user.user", "picture", "https://www.example.com/picture.jpg"),
resource.TestCheckResourceAttr("auth0_user.user", "user_metadata", `{"baz":"qux","foo":"bar"}`),
resource.TestCheckResourceAttr("auth0_user.user", "app_metadata", `{"baz":"qux","foo":"bar"}`),
resource.TestCheckResourceAttr("auth0_user.user", "roles.#", "0"),
),
},
{
Expand All @@ -190,12 +171,16 @@ func TestAccUser(t *testing.T) {
resource.TestCheckResourceAttr("auth0_user.user", "roles.#", "2"),
resource.TestCheckResourceAttr("auth0_role.owner", "name", "owner"),
resource.TestCheckResourceAttr("auth0_role.admin", "name", "admin"),
resource.TestCheckResourceAttr("auth0_user.user", "user_metadata", `{"foo":"bars"}`),
resource.TestCheckResourceAttr("auth0_user.user", "app_metadata", `{"foo":"bars"}`),
),
},
{
Config: template.ParseTestName(testAccUserRemoveRole, strings.ToLower(t.Name())),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_user.user", "roles.#", "1"),
resource.TestCheckResourceAttr("auth0_user.user", "user_metadata", ""),
resource.TestCheckResourceAttr("auth0_user.user", "app_metadata", ""),
),
},
},
Expand Down

0 comments on commit b94b80d

Please sign in to comment.