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

azurerm_role_definition: role_definition_id is now optional #1378

Merged
merged 4 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
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.
Copy link
Member

Choose a reason for hiding this comment

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

Extra period one will be generated if not specified..


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

Expand Down