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 18, 2022
1 parent 1a96b7c commit 2ec154b
Show file tree
Hide file tree
Showing 4 changed files with 665 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/provider/resource_auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,10 @@ 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),
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 @@ -976,3 +976,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 @@ -40,7 +40,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 @@ -205,6 +205,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 2ec154b

Please sign in to comment.