Skip to content

Commit

Permalink
azurerm_role_definition - enture role_definition_id is correctl… (#3913)
Browse files Browse the repository at this point in the history
We encountered the issue where role_definition_id remained nil after create, returning 409s on RoleDefinitionsClient.CreateOrUpdate with code RoleDefinitionWithSameNameExists.

Updating role definition resource was failing because resourceArmRoleDefinitionCreateUpdate checks if role_definition_id is nil and, if so, creates a new uuid and calls client.CreateOrUpdate which attempts to create a new resource instead of updating.
  • Loading branch information
notchairmk authored and katbyte committed Aug 2, 2019
1 parent 532334d commit a5b6317
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions azurerm/resource_arm_role_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ func resourceArmRoleDefinitionRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("Error loading Role Definition %q: %+v", d.Id(), err)
}

if id := resp.ID; id != nil {
roleDefinitionId, err := parseRoleDefinitionId(*id)
if err != nil {
return fmt.Errorf("Error parsing Role Definition ID: %+v", err)
}
if roleDefinitionId != nil {
d.Set("role_definition_id", roleDefinitionId.roleDefinitionId)
}
}

if props := resp.RoleDefinitionProperties; props != nil {
d.Set("name", props.RoleName)
d.Set("description", props.Description)
Expand Down
54 changes: 54 additions & 0 deletions azurerm/resource_arm_role_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,40 @@ func TestAccAzureRMRoleDefinition_update(t *testing.T) {
})
}

func TestAccAzureRMRoleDefinition_updateEmptyId(t *testing.T) {
resourceName := "azurerm_role_definition.test"
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRoleDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMRoleDefinition_emptyId(ri),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRoleDefinitionExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "permissions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.actions.0", "*"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.not_actions.#", "0"),
),
},
{
Config: testAccAzureRMRoleDefinition_updateEmptyId(ri),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRoleDefinitionExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "permissions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.actions.0", "*"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.not_actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "permissions.0.not_actions.0", "Microsoft.Authorization/*/read"),
),
},
},
})
}

func TestAccAzureRMRoleDefinition_emptyName(t *testing.T) {
resourceName := "azurerm_role_definition.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -303,3 +337,23 @@ resource "azurerm_role_definition" "test" {
}
`, rInt)
}

func testAccAzureRMRoleDefinition_updateEmptyId(rInt int) string {
return fmt.Sprintf(`
data "azurerm_subscription" "primary" {}
resource "azurerm_role_definition" "test" {
name = "acctestrd-%d"
scope = "${data.azurerm_subscription.primary.id}"
permissions {
actions = ["*"]
not_actions = ["Microsoft.Authorization/*/read"]
}
assignable_scopes = [
"${data.azurerm_subscription.primary.id}",
]
}
`, rInt)
}

0 comments on commit a5b6317

Please sign in to comment.