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

DXCDT-94 Stop ignoring errors when setting resource data within the client #94

Merged
merged 3 commits into from
Mar 30, 2022

Conversation

sergiught
Copy link
Contributor

@sergiught sergiught commented Mar 21, 2022

Description

This PR stops ignoring errors when setting resource data within the client.

        Error: 2 errors occurred:
        	* addons: '': source data must be an array or slice, got map
        	* mobile: '': source data must be an array or slice, got map

This fixes:

Checklist

Note: Checklist required to be completed before a PR is considered to be reviewable.

Auth0 Code of Conduct

Auth0 General Contribution Guidelines

Changes include test coverage?

  • Yes
  • Not needed

Does the description provide the correct amount of context?

  • Yes, the description provides enough context for the reviewer to understand what these changes accomplish

Have you updated the documentation?

  • Yes, I've updated the appropriate docs
  • Not needed

Is this code ready for production?

  • Yes, all code changes are intentional and no debugging calls are left over

At this point we will be getting the following errors from the tests as we now stop ignoring errors:

        Error: 2 errors occurred:
        	* addons: '': source data must be an array or slice, got map
        	* mobile: '': source data must be an array or slice, got map
Copy link
Contributor Author

@sergiught sergiught left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a few comments to help with reviewing this PR.

@@ -19,7 +19,6 @@ func newDataClient() *schema.Resource {
func newClientSchema() map[string]*schema.Schema {
clientSchema := datasourceSchemaFromResourceSchema(newClient().Schema)
delete(clientSchema, "client_secret_rotation_trigger")
delete(clientSchema, "client_secret")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are reusing the same schema as the client resource, if we delete the client_secret from here, when reading an entire client we will be getting an error when setting client_secret within the readClient() func as it's not present in the schema any more.

This should have never been deleted here because of this reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had originally agreed to omit client_secret from the data source. Unless I'm missing it down below, this change appears to repeal that decision (confirmed by the change in the test). I see that this method of deletion is problematic but it would be wise to scrub the client secret some other way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's indeed true, however at the time we weren't aware that this would lead to an unsurfaced error. Considering the resource already has the client_secret and this is something that the API simply returns, if not using the data_source we always have access to it through the resource. Having the client_secret will also help a lot with passing that information to other providers / resources for configuration purposes.

@@ -34,7 +34,10 @@ func TestAccDataClientByName(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: random.Template(testAccClientConfig, rand), // must initialize resource before reading with data source
Config: random.Template(testAccClientConfig, rand),
Check: resource.ComposeTestCheckFunc(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing the resource we just wanna make extra sure the client got created correctly so we can fetch it within the client data source below.

@@ -62,6 +64,9 @@ func TestAccDataClientById(t *testing.T) {
Steps: []resource.TestStep{
{
Config: random.Template(testAccClientConfig, rand),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - %v", rand)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing the resource we just wanna make extra sure the client got created correctly so we can fetch it within the client data source below.

Comment on lines -360 to -371
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"callback": {
Type: schema.TypeString,
Optional: true,
},
"slo_enabled": {
Type: schema.TypeBool,
Optional: true,
},
},
},
Copy link
Contributor Author

@sergiught sergiught Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to not need to introduce a breaking change to our users we need to remove the definition of each element of the schema.TypeMap as we're not allowed to have different types. This now follows the same strategy as for the other addon types. Like this we can think of deprecating the other ones in a subsequent PR.

Check the note on the TypeMap from the official docs: https://www.terraform.io/plugin/sdkv2/schemas/schema-types#typemap.

d.Set("mobile", c.Mobile)
d.Set("initiate_login_uri", c.InitiateLoginURI)
d.Set("signing_keys", c.SigningKeys)
result := multierror.Append(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We stop ignoring all the potential errors from the Set func using the multierror pkg. This actually surfaces issues with our client resource that we need to fix. Ultimately fixing them also fixes 3 open issues we have within our github that are referenced in the description.


func mapToState(input map[string]interface{}) map[string]interface{} {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to prevent us from introducing a breaking change we need to ensure that the TypeMap attribute always contains strings when in the state but actually use correct types when sending it to the Auth0 management API. We added a mapToState that will convert types like integers, bools, float to strings so all TypeMap key values are strings.

Comment on lines +914 to +916
if customSocial == nil {
return nil
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We return early in case customSocial is nil. This decreases the level of indentation within this func.

Comment on lines +950 to +952
if refreshToken == nil {
return nil
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We return early in case refreshToken is nil. This decreases the level of indentation within this func.

return []interface{}{m}
}

func flattenClientAddons(addons map[string]interface{}) []interface{} {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To properly fix the surfaced error we needed to add a custom flatten of the client addons.

return []interface{}{m}
}

func flattenClientMobile(mobile map[string]interface{}) []interface{} {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To properly fix the surfaced error we needed to add a custom flatten of the client mobile.

@sergiught sergiught self-assigned this Mar 22, 2022
@sergiught sergiught marked this pull request as ready for review March 22, 2022 00:03
@sergiught sergiught requested a review from a team as a code owner March 22, 2022 00:03
@sergiught sergiught requested a review from willvedd March 23, 2022 14:53
@sergiught sergiught changed the title DXCDT-80 Stop ignoring errors when setting resource data within the client DXCDT-94 Stop ignoring errors when setting resource data within the client Mar 28, 2022
@sergiught sergiught requested review from willvedd and removed request for a team and willvedd March 29, 2022 12:46
@sergiught sergiught merged commit 0ea12de into main Mar 30, 2022
@sergiught sergiught deleted the patch/DXCDT-80-client branch March 30, 2022 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants