Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with organization.metadata field not getting set to empty #245

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions auth0/resource_auth0_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ func updateOrganization(ctx context.Context, d *schema.ResourceData, m interface
func deleteOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
if err := api.Organization.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
if err, ok := err.(management.Error); ok && err.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

return nil
Expand Down Expand Up @@ -208,10 +207,12 @@ func updateOrganizationConnections(d *schema.ResourceData, api *management.Manag
}

func expandOrganization(d *schema.ResourceData) *management.Organization {
metadata := Map(d, "metadata")

organization := &management.Organization{
Name: String(d, "name"),
DisplayName: String(d, "display_name"),
Metadata: Map(d, "metadata"),
Metadata: &metadata,
}

List(d, "branding").Elem(func(d ResourceData) {
Expand Down
26 changes: 23 additions & 3 deletions auth0/resource_auth0_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ resource auth0_organization acme {
name = "test-{{.testName}}"
display_name = "Acme Inc. {{.testName}}"

metadata = {
some_key = "some_value"
}

connections {
connection_id = auth0_connection.acme.id
}
Expand All @@ -81,17 +85,25 @@ const testAccOrganizationUpdate = testAccOrganizationGiven2Connections + `
resource auth0_organization acme {
name = "test-{{.testName}}"
display_name = "Acme Inc. {{.testName}}"

metadata = {
some_key = "some_value"
another_key = "another_value"
}

branding {
logo_url = "https://acme.com/logo.svg"
colors = {
primary = "#e3e2f0"
page_background = "#e3e2ff"
}
}

connections {
connection_id = auth0_connection.acme.id
assign_membership_on_login = false
}

connections {
connection_id = auth0_connection.acmeinc.id
assign_membership_on_login = true
Expand All @@ -104,6 +116,10 @@ resource auth0_organization acme {
name = "test-{{.testName}}"
display_name = "Acme Inc. {{.testName}}"

metadata = {
some_key = "some_value"
}

branding {
logo_url = "https://acme.com/logo.svg"
colors = {
Expand Down Expand Up @@ -158,7 +174,8 @@ func TestAccOrganization(t *testing.T) {
resource.TestCheckResourceAttr("auth0_organization.acme", "name", fmt.Sprintf("test-%s", strings.ToLower(t.Name()))),
resource.TestCheckResourceAttr("auth0_organization.acme", "display_name", fmt.Sprintf("Acme Inc. %s", strings.ToLower(t.Name()))),
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.#", "0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "1"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.some_key", "some_value"),
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.#", "1"),
resource.TestCheckResourceAttrSet("auth0_organization.acme", "connections.0.connection_id"),
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.0.assign_membership_on_login", "false"),
Expand All @@ -174,7 +191,9 @@ func TestAccOrganization(t *testing.T) {
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.%", "2"),
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.primary", "#e3e2f0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.page_background", "#e3e2ff"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "2"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.some_key", "some_value"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.another_key", "another_value"),
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.#", "2"),
resource.TestCheckResourceAttrSet("auth0_organization.acme", "connections.0.connection_id"),
resource.TestCheckResourceAttrSet("auth0_organization.acme", "connections.1.connection_id"),
Expand All @@ -192,7 +211,8 @@ func TestAccOrganization(t *testing.T) {
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.%", "2"),
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.primary", "#e3e2f0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.page_background", "#e3e2ff"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "1"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.some_key", "some_value"),
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.#", "1"),
resource.TestCheckResourceAttrSet("auth0_organization.acme", "connections.0.connection_id"),
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.0.assign_membership_on_login", "false"),
Expand Down
25 changes: 16 additions & 9 deletions auth0/resource_auth0_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,21 @@ func readUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
d.Set("picture", user.Picture),
)

userMeta, err := structure.FlattenJsonToString(user.UserMetadata)
if err != nil {
return diag.FromErr(err)
var userMeta string
if user.UserMetadata != nil {
userMeta, err = structure.FlattenJsonToString(*user.UserMetadata)
if err != nil {
return diag.FromErr(err)
}
}
result = multierror.Append(result, d.Set("user_metadata", userMeta))

appMeta, err := structure.FlattenJsonToString(user.AppMetadata)
if err != nil {
return diag.FromErr(err)
var appMeta string
if user.AppMetadata != nil {
appMeta, err = structure.FlattenJsonToString(*user.AppMetadata)
if err != nil {
return diag.FromErr(err)
}
}
result = multierror.Append(result, d.Set("app_metadata", appMeta))

Expand Down Expand Up @@ -255,16 +261,17 @@ func buildUser(d *schema.ResourceData) (*management.User, error) {
Blocked: Bool(d, "blocked"),
}

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

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

return user, nil
}
Expand Down
Loading