Skip to content

Commit

Permalink
Fix update behavior or client_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Oct 13, 2022
1 parent c48755f commit ef3be75
Show file tree
Hide file tree
Showing 6 changed files with 671 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/auth0/terraform-provider-auth0
go 1.18

require (
github.com/auth0/go-auth0 v0.11.0
github.com/auth0/go-auth0 v0.0.0-20221013131223-12cfcb1d8468
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/terraform-plugin-docs v0.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/auth0/go-auth0 v0.0.0-20221013131223-12cfcb1d8468 h1:E/ai8y3Ixaz1jAfLDxixoI3DZxnv46g49C8UoEd5XKw=
github.com/auth0/go-auth0 v0.0.0-20221013131223-12cfcb1d8468/go.mod h1:XtmeQ7vZzyss3AAaLXMpupn28Y1Xj/DCt1IGEJRZ2gY=
github.com/auth0/go-auth0 v0.11.0 h1:mxqbDMe91wjX3hvtPU/T7BdJqjgNQUJ02ZaROm/WRKA=
github.com/auth0/go-auth0 v0.11.0/go.mod h1:XtmeQ7vZzyss3AAaLXMpupn28Y1Xj/DCt1IGEJRZ2gY=
github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0 h1:0NmehRCgyk5rljDQLKUO+cRJCnduDyn11+zGZIc9Z48=
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/resource_auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,12 +780,15 @@ func readClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag
d.Set("refresh_token", flattenClientRefreshTokenConfiguration(client.GetRefreshToken())),
d.Set("encryption_key", client.GetEncryptionKey()),
d.Set("addons", flattenClientAddons(client.Addons)),
d.Set("client_metadata", client.GetClientMetadata()),
d.Set("mobile", flattenClientMobile(client.GetMobile())),
d.Set("initiate_login_uri", client.GetInitiateLoginURI()),
d.Set("signing_keys", client.SigningKeys),
)

if client.ClientMetadata != nil {
result = multierror.Append(result, d.Set("client_metadata", *client.ClientMetadata))
}

return diag.FromErr(result.ErrorOrNil())
}

Expand Down
67 changes: 67 additions & 0 deletions internal/provider/resource_auth0_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,3 +970,70 @@ func TestAccClientSSOIntegrationWithSAML(t *testing.T) {
},
})
}

func TestAccClientMetadataBehavior(t *testing.T) {
httpRecorder := recorder.New(t)

resource.Test(t, resource.TestCase{
ProviderFactories: testProviders(httpRecorder),
Steps: []resource.TestStep{
{
Config: template.ParseTestName(`
resource "auth0_client" "my_client" {
name = "Acceptance Test - Metadata - {{.testName}}"
client_metadata = {
foo = "zoo"
bar = "baz"
}
}`, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - Metadata - %s", t.Name())),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.%", "2"),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.foo", "zoo"),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.bar", "baz"),
),
},
{
Config: template.ParseTestName(`
resource "auth0_client" "my_client" {
name = "Acceptance Test - Metadata - {{.testName}}"
client_metadata = {
foo = "newZooButOldFoo"
newBar = "newBaz"
}
}`, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - Metadata - %s", t.Name())),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.%", "2"),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.foo", "newZooButOldFoo"),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.newBar", "newBaz"),
),
},
{
Config: template.ParseTestName(`
resource "auth0_client" "my_client" {
name = "Acceptance Test - Metadata - {{.testName}}"
client_metadata = {
bar = "baz"
}
}`, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - Metadata - %s", t.Name())),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.%", "1"),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.bar", "baz"),
),
},
{
Config: template.ParseTestName(`
resource "auth0_client" "my_client" {
name = "Acceptance Test - Metadata - {{.testName}}"
client_metadata = { }
}`, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - Metadata - %s", t.Name())),
resource.TestCheckResourceAttr("auth0_client.my_client", "client_metadata.%", "0"),
),
},
},
})
}
20 changes: 19 additions & 1 deletion internal/provider/structure_auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func expandClient(d *schema.ResourceData) *management.Client {
TokenEndpointAuthMethod: value.String(config.GetAttr("token_endpoint_auth_method")),
InitiateLoginURI: value.String(config.GetAttr("initiate_login_uri")),
EncryptionKey: value.MapOfStrings(config.GetAttr("encryption_key")),
ClientMetadata: value.MapOfStrings(config.GetAttr("client_metadata")),
ClientMetadata: expandClientMetadata(d),
RefreshToken: expandClientRefreshToken(d),
JWTConfiguration: expandClientJWTConfiguration(d),
Addons: expandClientAddons(d),
Expand Down Expand Up @@ -204,6 +204,24 @@ func expandClientMobileIOS(iosConfig cty.Value) *management.ClientMobileIOS {
return &ios
}

func expandClientMetadata(d *schema.ResourceData) *map[string]interface{} {
if !d.HasChange("client_metadata") {
return nil
}

oldMetadata, newMetadata := d.GetChange("client_metadata")
oldMetadataMap := oldMetadata.(map[string]interface{})
newMetadataMap := newMetadata.(map[string]interface{})

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

return &newMetadataMap
}

func expandClientAddons(d *schema.ResourceData) map[string]interface{} {
if !d.HasChange("addons") {
return nil
Expand Down
Loading

0 comments on commit ef3be75

Please sign in to comment.