Skip to content

Commit

Permalink
Merge pull request #1378 from terraform-providers/f-role_definition-a…
Browse files Browse the repository at this point in the history
…uto_gen_name. Fixes #1371.
  • Loading branch information
katbyte committed Jun 12, 2018
2 parents 86865a8 + 9fe7aa7 commit e4e05b2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
13 changes: 12 additions & 1 deletion azurerm/resource_arm_role_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2018-01-01-preview/authorization"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -22,7 +23,8 @@ func resourceArmRoleDefinition() *schema.Resource {
Schema: map[string]*schema.Schema{
"role_definition_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
ForceNew: true,
},

Expand Down Expand Up @@ -81,6 +83,15 @@ func resourceArmRoleDefinitionCreateUpdate(d *schema.ResourceData, meta interfac
ctx := meta.(*ArmClient).StopContext

roleDefinitionId := d.Get("role_definition_id").(string)
if roleDefinitionId == "" {
uuid, err := uuid.GenerateUUID()
if err != nil {
return fmt.Errorf("Error generating UUID for Role Assignment: %+v", err)
}

roleDefinitionId = uuid
}

name := d.Get("name").(string)
scope := d.Get("scope").(string)
description := d.Get("description").(string)
Expand Down
42 changes: 42 additions & 0 deletions azurerm/resource_arm_role_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ func TestAccAzureRMRoleDefinition_update(t *testing.T) {
})
}

func TestAccAzureRMRoleDefinition_emptyName(t *testing.T) {
resourceName := "azurerm_role_definition.test"

ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRoleDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMRoleDefinition_emptyId(ri),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRoleDefinitionExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "name"),
),
},
},
})
}

func testCheckAzureRMRoleDefinitionExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -201,3 +223,23 @@ resource "azurerm_role_definition" "test" {
}
`, id, rInt)
}

func testAccAzureRMRoleDefinition_emptyId(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 = []
}
assignable_scopes = [
"${data.azurerm_subscription.primary.id}",
]
}
`, rInt)
}
3 changes: 1 addition & 2 deletions website/docs/r/role_definition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Manages a custom Role Definition, used to assign Roles to Users/Principals.
data "azurerm_subscription" "primary" {}
resource "azurerm_role_definition" "test" {
role_definition_id = "12345678-1234-5678-1234-123456780123"
name = "my-custom-role"
scope = "${data.azurerm_subscription.primary.id}"
description = "This is a custom role created via Terraform"
Expand All @@ -37,7 +36,7 @@ resource "azurerm_role_definition" "test" {

The following arguments are supported:

* `role_definition_id` - (Required) A unique UUID/GUID which identifies this role. Changing this forces a new resource to be created.
* `role_definition_id` - (Optional) A unique UUID/GUID which identifies this role - one will be generated if not specified. Changing this forces a new resource to be created.

* `name` - (Required) The name of the Role Definition. Changing this forces a new resource to be created.

Expand Down

0 comments on commit e4e05b2

Please sign in to comment.