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

Fix Management Group CreateUpdate Function #6668

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func dataSourceArmManagementGroupRead(d *schema.ResourceData, meta interface{})
recurse := true
resp, err := client.Get(ctx, groupName, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if utils.ResponseWasForbidden(resp.Response) {
return fmt.Errorf("Management Group %q was not found", groupName)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func resourceArmManagementGroupCreateUpdate(d *schema.ResourceData, meta interfa
armTenantID := meta.(*clients.Client).Account.TenantId

groupName := uuid.New().String()
if v, ok := d.GetOk("group_name"); ok {
if v, ok := d.GetOk("name"); ok {
groupName = v.(string)
}
if v, ok := d.GetOk("group_id"); ok {
Expand All @@ -107,11 +107,11 @@ func resourceArmManagementGroupCreateUpdate(d *schema.ResourceData, meta interfa
if d.IsNewResource() {
existing, err := client.Get(ctx, groupName, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("unable to check for presence of existing Management Group %q: %s", groupName, err)
// 403 is returned if it doesn't exist or user doesn't have proper permissions to view it
if utils.ResponseWasForbidden(existing.Response) {
log.Printf("[DEBUG] Management Group %q does not exist or authorization is forbidden from the user", groupName)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_management_group", *existing.ID)
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func resourceArmManagementGroupRead(d *schema.ResourceData, meta interface{}) er
recurse := true
resp, err := client.Get(ctx, id.GroupId, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if utils.ResponseWasForbidden(resp.Response) {
log.Printf("[INFO] Management Group %q doesn't exist - removing from state", d.Id())
d.SetId("")
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestAccAzureRMManagementGroup_withName(t *testing.T) {
func TestAccAzureRMManagementGroup_updateName(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_management_group", "test")

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMManagementGroupDestroy,
Expand Down Expand Up @@ -218,8 +218,8 @@ func testCheckAzureRMManagementGroupExists(resourceName string) resource.TestChe
return fmt.Errorf("Bad: Get on managementGroupsClient: %s", err)
}

if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Management Group does not exist: %s", groupName)
if resp.StatusCode == http.StatusForbidden {
return fmt.Errorf("Management Group does not exist or you do not have proper permissions: %s", groupName)
}

return nil
Expand All @@ -243,7 +243,7 @@ func testCheckAzureRMManagementGroupDestroy(s *terraform.State) error {
return nil
}

if resp.StatusCode != http.StatusNotFound {
if resp.StatusCode == http.StatusAccepted {
return fmt.Errorf("Management Group still exists: %s", *resp.Name)
}
}
Expand Down
4 changes: 4 additions & 0 deletions azurerm/utils/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ func ResponseWasStatusCode(resp autorest.Response, statusCode int) bool { // nol

return false
}

func ResponseWasForbidden(resp autorest.Response) bool {
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this was added recently so we can remove it.

return ResponseWasStatusCode(resp, http.StatusForbidden)
}