Skip to content

[CC-27365] add user_role_grant resource #188

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

Merged
merged 1 commit into from
Mar 20, 2024

Conversation

fantapop
Copy link
Collaborator

Previously, the provider supported user role management but only in an authoritative way. Using the user_role_grants resource would overwrite all roles for a user. This caused churn for terraform users trying to manage user roles across multiple projects or via the console UI. We now add a resource, user_role_grant which allows management of a single user role grant. The existing user_role_grants resource is left to maintain the previous functionality for those that need it.

As part of this changes, I've also renamed the role_resource files which contain the user_role_grants resource after the full name of the role since that seems to be the convention.

Commit checklist

  • Changelog
  • Doc gen (make generate)
  • Integration test(s)
  • Acceptance test(s)
  • Example(s)

@fantapop fantapop requested review from marksoper, rgcase and pawalt March 14, 2024 23:35
@fantapop fantapop force-pushed the CC-27365-user-role-grant-resource branch from bbf2298 to b44492f Compare March 14, 2024 23:36
ID types.String `tfsdk:"id"`
UserId types.String `tfsdk:"user_id"`
Roles []Role `tfsdk:"roles"`
}

type UserRoleGrant struct {
UserID types.String `tfsdk:"user_id"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Double-checking that we don't need a unique ID here ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I experimented with this and it doesn't look like we need it. I did eventually run into an issue with the testing framework where we needed an ID but this was avoided by migrating to the new framework. See here:
https://developer.hashicorp.com/terraform/plugin/framework/acctests#no-id-found-in-attributes

Here is the PR where I migrated the framework.
#185

Also, I mentioned to you early on that we were planning to add a unique ID for this but decided against it because it didn't fit into our current public api definitions at all.

)
return
}
_, _, err = r.provider.service.AddUserToRole(
Copy link

Choose a reason for hiding this comment

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

Do we need to do any logic here to check if this assignment is necessary? Or should we just always do it because it's idempotent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In general I'm not concerned with idempotency as terraform sprinkles read in various places to determine if something is necessary. Drift can obviously happen and whether it happens during or close to a terraform run is just luck. It would be detected during the next run and updated. For this particular case however I can't recall if I've tested what happens when a role grant already exists. It should fail in this case but if our API is idempotent, it may think that it has been able to "adopt" this resource without using import. I'll do some testing around this. Maybe I can add an integration test.

Copy link

Choose a reason for hiding this comment

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

I think the API is idempotent. Which seems fine to me - I don't think we need to fail if the assignment already exists. Arguably this makes the import case even less necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So I think this is actually a problem. If terraform tries to create a resource which already exists and doesn't fail, it means two projects that are accidentally using the same resource will both think they own it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It could also lead to issues in the same project where a project thinks it has 2 managed resources but only one on the backend.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I pushed an update to check for the resource before attempting to "Create" it. I've also updated the tests to exercise a failure.

) (userID, roleName, resourceType, resourceID string, err error) {
idParts := strings.Split(id, ",")
if len(idParts) == 4 {
if idParts[2] == resourceTypeOrg && idParts[3] != "" {
Copy link

Choose a reason for hiding this comment

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

Maybe a uuid.Parse would be helpful here as a sanity check

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've updated that method and tests to do checking on the uuids.

@fantapop fantapop force-pushed the CC-27365-user-role-grant-resource branch from b44492f to b170eb4 Compare March 18, 2024 23:52
Copy link

@pawalt pawalt left a comment

Choose a reason for hiding this comment

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

LGTM pending docs review

@marksoper
Copy link
Contributor

@mikeCRL @codingconcepts @fantapop @pawalt @shannonbradshaw
Hi Mike, Rob-
I got word from @shannonbradshaw that you may be able to help with docs for this. A good first step would be to have a look at this PR to see if there are concerns and understand the docs implications.
As you probably know docs are critical for Terraform, as so much Terraform usage begins with examples

@fantapop fantapop force-pushed the CC-27365-user-role-grant-resource branch 3 times, most recently from 00f6e4b to cba8634 Compare March 20, 2024 18:03
Copy link

@pawalt pawalt left a comment

Choose a reason for hiding this comment

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

Idempotency changes LGTM

@fantapop
Copy link
Collaborator Author

I'm waiting on the new SDK tag to be available before merging this down. We can add documentation revisions to a later PR if any manifest. Just let me know.

@fantapop fantapop force-pushed the CC-27365-user-role-grant-resource branch 2 times, most recently from ca8e6f1 to 0967174 Compare March 20, 2024 22:35
Previously, the provider supported user role management but only in an
authoritative way.  Using the user_role_grants resource would overwrite
all roles for a user.  This caused churn for terraform users trying to
manage user roles across multiple projects or via the console UI.  We
now add a resource, user_role_grant which allows management of a single
user role grant.  The existing user_role_grants resource is left to
maintain the previous functionality for those that need it.

As part of this changes, I've also renamed the role_resource files which
contain the user_role_grants resource after the full name of the role
since that seems to be the convention.

Additionally, cloud SDK is Update to 1.9.0
@fantapop fantapop force-pushed the CC-27365-user-role-grant-resource branch from 0967174 to 1aecf10 Compare March 20, 2024 22:38
@fantapop fantapop merged commit 17200d6 into main Mar 20, 2024
@fantapop fantapop deleted the CC-27365-user-role-grant-resource branch March 20, 2024 22:40
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.

3 participants