-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Role Assignments / Role Definitions #414
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
96a8290
Updating the built-in role definition id's
tombuildsstuff 5d76fd5
Vendorng the authorrization sdk
tombuildsstuff 024ff3e
Adding more fields to the built-in role definition data source
tombuildsstuff 4811eb1
Removing the ARM prefix for data sources
tombuildsstuff bc076d5
New Resource: `azurerm_role_definition`
tombuildsstuff 04505da
Adding in Assignable Scopes to the Data Source
tombuildsstuff 9fa85bf
New Data Source: `azurerm_role_definition`
tombuildsstuff 0238dbd
New Resource: `azurerm_role_assignment`
tombuildsstuff 6d8ebaa
Import Tests for Role Assignments
tombuildsstuff 3311808
Adding Import tests for Role Definitions
tombuildsstuff e6b5e61
Vendoring github.com/google/uuid
tombuildsstuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func dataSourceArmBuiltInRoleDefinition() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmBuiltInRoleDefinitionRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"Contributor", | ||
"Reader", | ||
"Owner", | ||
"VirtualMachineContributor", | ||
}, false), | ||
}, | ||
|
||
// Computed | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"permissions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"actions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"not_actions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"assignable_scopes": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmBuiltInRoleDefinitionRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).roleDefinitionsClient | ||
name := d.Get("name").(string) | ||
roleDefinitionIds := map[string]string{ | ||
"Contributor": "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c", | ||
"Owner": "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635", | ||
"Reader": "/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7", | ||
"VirtualMachineContributor": "/providers/Microsoft.Authorization/roleDefinitions/d73bb868-a0df-4d4d-bd69-98a00b01fccb", | ||
} | ||
roleDefinitionId := roleDefinitionIds[name] | ||
|
||
d.SetId(roleDefinitionId) | ||
|
||
role, err := client.GetByID(roleDefinitionId) | ||
if err != nil { | ||
return fmt.Errorf("Error loadng Role Definition: %+v", err) | ||
} | ||
|
||
if props := role.Properties; props != nil { | ||
d.Set("name", props.RoleName) | ||
d.Set("description", props.Description) | ||
d.Set("type", props.Type) | ||
|
||
permissions := flattenRoleDefinitionPermissions(props.Permissions) | ||
if err := d.Set("permissions", permissions); err != nil { | ||
return err | ||
} | ||
|
||
assignableScopes := flattenRoleDefinitionAssignableScopes(props.AssignableScopes) | ||
if err := d.Set("assignable_scopes", assignableScopes); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureRMBuiltInRoleDefinition_contributor(t *testing.T) { | ||
dataSourceName := "data.azurerm_builtin_role_definition.test" | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceBuiltInRoleDefinition("Contributor"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAzureRMClientConfigAttr(dataSourceName, "id", "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "description"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "type"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.0", "*"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.#", "3"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.0", "Microsoft.Authorization/*/Delete"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.1", "Microsoft.Authorization/*/Write"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.2", "Microsoft.Authorization/elevateAccess/Action"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMBuiltInRoleDefinition_owner(t *testing.T) { | ||
dataSourceName := "data.azurerm_builtin_role_definition.test" | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceBuiltInRoleDefinition("Owner"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAzureRMClientConfigAttr(dataSourceName, "id", "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "description"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "type"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.0", "*"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMBuiltInRoleDefinition_reader(t *testing.T) { | ||
dataSourceName := "data.azurerm_builtin_role_definition.test" | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceBuiltInRoleDefinition("Reader"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAzureRMClientConfigAttr(dataSourceName, "id", "/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "description"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "type"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.0", "*/read"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMBuiltInRoleDefinition_virtualMachineContributor(t *testing.T) { | ||
dataSourceName := "data.azurerm_builtin_role_definition.test" | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceBuiltInRoleDefinition("VirtualMachineContributor"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAzureRMClientConfigAttr(dataSourceName, "id", "/providers/Microsoft.Authorization/roleDefinitions/d73bb868-a0df-4d4d-bd69-98a00b01fccb"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "description"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "type"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.#", "17"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.0", "Microsoft.Authorization/*/read"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.actions.15", "Microsoft.Resources/subscriptions/resourceGroups/read"), | ||
resource.TestCheckResourceAttr(dataSourceName, "permissions.0.not_actions.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceBuiltInRoleDefinition(name string) string { | ||
return fmt.Sprintf(` | ||
data "azurerm_builtin_role_definition" "test" { | ||
name = "%s" | ||
} | ||
`, name) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these values (
Contributor
,Owner
,Reader
,VirtualMachineContributor
) specific to our account or azure in general?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're Azure in General - this Data Source is mostly there so users don't have to refer to GUIDs tbh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!