-
Notifications
You must be signed in to change notification settings - Fork 89
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 not being able to update connections on organizations #244
Conversation
de8e692
to
0941fd7
Compare
}, | ||
}, | ||
}, | ||
Set: hash.StringKey("connection_id"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use just the connection_id for the set hashes as that will ignore anything that we update on the assign_membership_on_login
field. This is mostly the core of the fix, but in order to make it work properly several other things were needed. Please follow comments.
@@ -64,7 +60,6 @@ func newOrganization() *schema.Resource { | |||
"connections": { | |||
Type: schema.TypeSet, | |||
Optional: true, | |||
Computed: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't need to be a computed property, it's actually us within the config that decide the values of these and then it gets created on the API.
@@ -74,83 +69,32 @@ func newOrganization() *schema.Resource { | |||
"assign_membership_on_login": { | |||
Type: schema.TypeBool, | |||
Optional: true, | |||
Default: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This default is very important to correctly figure out the differences on the hashed sets.
}, | ||
}, | ||
} | ||
} | ||
|
||
func createOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
organization := buildOrganization(d) | ||
organization := expandOrganization(d) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renaming this so we're in line with the naming conventions "expand" and "flatten".
api := m.(*management.Management) | ||
if err := api.Organization.Create(organization); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(organization.GetID()) | ||
|
||
d.Partial(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of what caused the bug with the diffs I believe. The Partial func is a legacy func used when we had the SetPartial func as well (deprecated). This is not needed at all as on every command we run a refresh (read) even if we stop mid way here on the follow up plan or apply we'll read again and correct the state.
toAdd, toRemove := Diff(d, "connections") | ||
|
||
var err error | ||
toRemove.Elem(func(data ResourceData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After we updated the schema we were getting at times weird errors about the connection already being there (409s).
This was because if we change the bool flag on a connection we get the following diff:
- connections {
- assign_membership_on_login = false -> null
- connection_id = "con_E68qtlsIB68K6Yz6" -> null
}
+ connections {
+ assign_membership_on_login = true
+ connection_id = "con_E68qtlsIB68K6Yz6"
}
So to correctly update the connection without adding complicated diffing logic, we delete the connection and then re-add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: We opted instead to create an internal map in the updateConnections function in order to not delete and then add even if the diff shows that in terraform. Unfortunately we couldn't suppress the diff correctly.
return err | ||
} | ||
|
||
Set(d, "connections").Elem(func(data ResourceData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was also important to remove the HasChange() from here, so that now we always loop through the final connections and send a PATCH with whatever the value is, as this operation is idempotent. This ultimately makes us correctly update the values.
@@ -137,20 +99,114 @@ resource auth0_organization acme { | |||
} | |||
` | |||
|
|||
const testAccOrganizationUpdateAgain = testAccOrganizationAux + ` | |||
const testAccOrganizationUpdateAgain = testAccOrganizationGiven2Connections + ` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding more thorough test scenarios and assertions.
0941fd7
to
6f32c87
Compare
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", "connections.#", "1"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I observed while working on the organization members is that for whatever reason, the bulk of this code had issues with completely clearing out members. It's possible that the connections has the same issue, so for that reason I think having the following test case would be useful:
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.#", "0"),
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added another test case where we remove all connections 👍🏻 and tests pass
6f32c87
to
d030ba2
Compare
d030ba2
to
bfb8cf3
Compare
Description
Fixes: #65
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?
Does the description provide the correct amount of context?
Have you updated the documentation?
Is this code ready for production?