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

Fixing destroy when role scope is a Management Group #6107

Merged
merged 3 commits into from
Sep 9, 2020

Conversation

cmendible
Copy link
Contributor

Fixes #5125: When using azurerm_role_definition to create a role scoped to a Management Group instead to a Subscription or Resource Group, terraform destroy failed cause the scope sent to the API was empty.

This was caused by the parseRoleDefinitionId function which asumes that every roleDefinitionId starts with the scope which is not the case for Roles scoped to a Management Group.

Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

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

Hi @cmendible, thank you for this fix

I was wondering if you could add a test that fails without it, and passes with the change so we can ensure it doesn't fail in the future.

Also what is the ID of the role definition when it is scoped to a management group? doesn't it start with the group and then finish with the scope?

@cmendible
Copy link
Contributor Author

Hi @katbyte no problem I'll add the tests and come back to you with sample IDs

@ghost ghost removed the waiting-response label Mar 24, 2020
@cmendible
Copy link
Contributor Author

@katbyte when roles are scoped to a management group, the ID takes the following form:

/providers/Microsoft.Authorization/roleDefinitions/<GUID>

so the scope can not be extracted from it and therefore we must be read the assignable_scopes property.

@ghost ghost added size/M and removed size/XS labels Mar 25, 2020
Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

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

Getting an error running the test @cmendible :

------ Stdout: -------
=== RUN   TestAccAzureRMRoleDefinition_managementGroup
=== PAUSE TestAccAzureRMRoleDefinition_managementGroup
=== CONT  TestAccAzureRMRoleDefinition_managementGroup
--- FAIL: TestAccAzureRMRoleDefinition_managementGroup (7.72s)
    testing.go:640: Step 0 error: errors during apply:
        
        Error: Error checking for presence of existing Role Definition ID for "acctestrd-200325225805475020" (Scope "/providers/Microsoft.Management/managementGroups/testMG")
        
          on /opt/teamcity-agent/temp/buildTmp/tf-test132856266/main.tf line 10:
          (source code not available)
 

@cmendible
Copy link
Contributor Author

Error checking for presence of

Sorry about that, the management group must exist before the test runs. Is there a place where we can create this dependency before running the tests or should I embed the management group creation inside the testAccAzureRMRoleDefinition_managementGroup method?

@cmendible
Copy link
Contributor Author

@katbyte just added the management group to the test.

@cmendible cmendible requested a review from katbyte March 30, 2020 17:02
@cmendible
Copy link
Contributor Author

@katbyte just added the management group to the test.

@katbyte can you force and e2e test?

@cmendible
Copy link
Contributor Author

Hi @katbyte any news on this one?

@javierjeronimo
Copy link

Big organisations use Management-Groups to manage Azure Policy and Custom-Roles definitions, this is really needed. Thanks.

@Vrecuero
Copy link

Vrecuero commented May 28, 2020

I have the same problem. It would really be helpful for Azure-Terraform interactions regarding Management Groups.

Copy link
Member

@manicminer manicminer left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Member

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

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

Aside from needing to store/lookup the scope field somewhere this otherwise LGTM 👍

@ghost ghost added size/XL and removed size/M labels Aug 12, 2020
Copy link
Member

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@jackofallops jackofallops added this to the v2.27.0 milestone Sep 9, 2020
@jackofallops jackofallops merged commit 64680b0 into hashicorp:master Sep 9, 2020
@jackofallops
Copy link
Member

Tests passing, (failures unrelated)
image

jackofallops added a commit that referenced this pull request Sep 9, 2020
@ghost
Copy link

ghost commented Sep 10, 2020

This has been released in version 2.27.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 2.27.0"
}
# ... other configuration ...

@ghost
Copy link

ghost commented Oct 9, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Oct 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terraform destroy fails for azurerm_role_definition if role scope is a Management Group
7 participants