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

Update resource_builtin_role_assignment resource to ignore assignments defined in the server side #369

Merged
merged 4 commits into from
Feb 4, 2022

Conversation

vtorosyan
Copy link
Contributor

Context

When Grafana starts, by default it creates built-in role assignments for fixed roles. Additionally, users can create built-in role assignments via API. In these scenarios, when the resource is not properly imported, terraform will try to destroy what is in server side, but not in the configuration.

The change ensures that this does not happen as it can cause unintended behaviour for users.

Implementation details

The only workaround, with the given resources is to do in advance check to see if the fetched assignments has been ever in the state, or is present in the configuration, otherwise ignore it.

Testing

I have manually tested the following scenarios:

  • Having built-in role assignments in server, and creating a new assignment in terraform
  • Updating the above created assignments after it's first time creation
  • Removing roles from the above assignments
  • Destroying entire resource -> in this scenario what has not been defined in the config, won't be destroyed from the backend.

Additionally, the newly added test covers the scenario mentioned in the issue.

Related issues

Fixes #362

@vtorosyan vtorosyan requested a review from a team as a code owner February 2, 2022 19:02
@vtorosyan vtorosyan requested review from Jguer and gamab February 2, 2022 19:02
Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

The solution seems like a good one to me and it's well tested. I can't test it on my side but it LGTM!

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccBuiltInRoleAssignmentCheckDestroy(&br),
Copy link
Member

Choose a reason for hiding this comment

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

Should the CheckDestroy function also check that the default assignements weren't destroyed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Really good catch. I actually just realized that testAccBuiltInRoleAssignmentCheckDestroy checks nothing, as the pointer to &br never updates to the created built-in role assignment, so we always pass an empty struct to the method, which returns success on it's turn as there is indeed no assignment for empty built-in role.

Working on a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@inkel inkel left a comment

Choose a reason for hiding this comment

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

I've just left a minor request. My initial review looks good, but I want to give it another round to fully understand what are we testing here.

Thank you so much for your hard work!

grafana/resource_builtin_role_assignment.go Outdated Show resolved Hide resolved
…s defined in the server side

When Grafana starts, by default it creates built-in role assignments for fixed roles. Additionally, users can create built-in role assignments via API. In these scenarios, when the resource is not properly imported, terraform will try to destroy what is in server side, but not in the configuration.

The change ensures that this does not happen as it can cause unintended behaviour for users.
@vtorosyan vtorosyan merged commit 4c86b85 into master Feb 4, 2022
@vtorosyan vtorosyan deleted the vt/uid_fix branch February 4, 2022 12:18
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.

BuiltInRole deploy not idempotent
4 participants