-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
arm_role_assignment
: add principal_type
and skip_service_principal_aad_check
properties
#4168
Changes from 1 commit
855ee7e
84029ed
19e6594
c988716
b10b749
18bdc0f
808509e
9daeb7a
13173f9
fc6eecb
7b3452d
934b93c
1d42907
1e3a823
865796d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
|
@@ -64,6 +65,24 @@ func resourceArmRoleAssignment() *schema.Resource { | |
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"principal_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(authorization.Application), | ||
string(authorization.DirectoryObjectOrGroup), | ||
string(authorization.DirectoryRoleTemplate), | ||
string(authorization.Everyone), | ||
string(authorization.ForeignGroup), | ||
string(authorization.Group), | ||
string(authorization.MSI), | ||
string(authorization.ServicePrincipal), | ||
string(authorization.Unknown), | ||
string(authorization.User), | ||
}, false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -118,11 +137,19 @@ func resourceArmRoleAssignmentCreate(d *schema.ResourceData, meta interface{}) e | |
} | ||
} | ||
|
||
roleAssignmentProperties := authorization.RoleAssignmentProperties{ | ||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RoleDefinitionID: utils.String(roleDefinitionId), | ||
PrincipalID: utils.String(principalId), | ||
} | ||
|
||
principalType := d.Get("principal_type").(string) | ||
|
||
if principalType != "" { | ||
roleAssignmentProperties.PrincipalType = authorization.PrincipalType(principalType) | ||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
properties := authorization.RoleAssignmentCreateParameters{ | ||
RoleAssignmentProperties: &authorization.RoleAssignmentProperties{ | ||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RoleDefinitionID: utils.String(roleDefinitionId), | ||
PrincipalID: utils.String(principalId), | ||
}, | ||
RoleAssignmentProperties: &roleAssignmentProperties, | ||
} | ||
|
||
if err := resource.Retry(300*time.Second, retryRoleAssignmentsClient(scope, name, properties, meta)); err != nil { | ||
|
@@ -164,6 +191,12 @@ func resourceArmRoleAssignmentRead(d *schema.ResourceData, meta interface{}) err | |
d.Set("role_definition_id", props.RoleDefinitionID) | ||
d.Set("principal_id", props.PrincipalID) | ||
|
||
principalType := d.Get("principal_type").(string) | ||
|
||
if principalType != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we just mark the property as computed instead so its populated with whatever the default is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is my understanding that this needs to be defined by the user, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we shouldn't need this check at all? because right now we don't set it, the new property is optional with no default, so the user has to explicitly set it. And if in the case of the user not setting it there is nothing to read back then? If there is we can just mark it as computed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okie-dokie... fixed. :) |
||
d.Set("principal_type", props.PrincipalType) | ||
} | ||
|
||
//allows for import when role name is used (also if the role name changes a plan will show a diff) | ||
if roleId := props.RoleDefinitionID; roleId != nil { | ||
roleResp, err := roleDefinitionsClient.GetByID(ctx, *roleId) | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,8 +24,9 @@ func TestAccAzureRMRoleAssignment(t *testing.T) { | |||||
"requiresImport": testAccAzureRMRoleAssignment_requiresImport, | ||||||
}, | ||||||
"assignment": { | ||||||
"sp": testAccAzureRMActiveDirectoryServicePrincipal_servicePrincipal, | ||||||
"group": testAccAzureRMActiveDirectoryServicePrincipal_group, | ||||||
"sp": testAccAzureRMActiveDirectoryServicePrincipal_servicePrincipal, | ||||||
"spType": testAccAzureRMActiveDirectoryServicePrincipal_servicePrincipalWithType, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be better as:
Suggested change
|
||||||
"group": testAccAzureRMActiveDirectoryServicePrincipal_group, | ||||||
}, | ||||||
"management": { | ||||||
"assign": testAccAzureRMRoleAssignment_managementGroup, | ||||||
|
@@ -219,6 +220,27 @@ func testAccAzureRMActiveDirectoryServicePrincipal_servicePrincipal(t *testing.T | |||||
}) | ||||||
} | ||||||
|
||||||
func testAccAzureRMActiveDirectoryServicePrincipal_servicePrincipalWithType(t *testing.T) { | ||||||
resourceName := "azurerm_role_assignment.test" | ||||||
ri := tf.AccRandTimeInt() | ||||||
id := uuid.New().String() | ||||||
|
||||||
resource.ParallelTest(t, resource.TestCase{ | ||||||
PreCheck: func() { testAccPreCheck(t) }, | ||||||
Providers: testAccProviders, | ||||||
CheckDestroy: testCheckAzureRMRoleAssignmentDestroy, | ||||||
Steps: []resource.TestStep{ | ||||||
{ | ||||||
Config: testAccAzureRMRoleAssignment_servicePrincipalWithType(ri, id), | ||||||
Check: resource.ComposeTestCheckFunc( | ||||||
testCheckAzureRMRoleAssignmentExists("azurerm_role_assignment.test"), | ||||||
resource.TestCheckResourceAttr(resourceName, "principal_type", "ServicePrincipal"), | ||||||
), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
} | ||||||
|
||||||
func testAccAzureRMActiveDirectoryServicePrincipal_group(t *testing.T) { | ||||||
ri := tf.AccRandTimeInt() | ||||||
id := uuid.New().String() | ||||||
|
@@ -440,6 +462,28 @@ resource "azurerm_role_assignment" "test" { | |||||
`, rInt, roleAssignmentID) | ||||||
} | ||||||
|
||||||
func testAccAzureRMRoleAssignment_servicePrincipalWithType(rInt int, roleAssignmentID string) string { | ||||||
return fmt.Sprintf(` | ||||||
data "azurerm_subscription" "current" {} | ||||||
|
||||||
resource "azuread_application" "test" { | ||||||
name = "acctestspa-%d" | ||||||
} | ||||||
|
||||||
resource "azuread_service_principal" "test" { | ||||||
application_id = "${azuread_application.test.application_id}" | ||||||
} | ||||||
|
||||||
resource "azurerm_role_assignment" "test" { | ||||||
name = "%s" | ||||||
WodansSon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
scope = "${data.azurerm_subscription.current.id}" | ||||||
role_definition_name = "Reader" | ||||||
principal_id = "${azuread_service_principal.test.id}" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we fi the formatting here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kt I take that back... no we can't, if you do you get this error:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. switching from tabs to spaces gives you that error?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, switching form |
||||||
principal_type = "ServicePrincipal" | ||||||
} | ||||||
`, rInt, roleAssignmentID) | ||||||
} | ||||||
|
||||||
func testAccAzureRMRoleAssignment_group(rInt int, roleAssignmentID string) string { | ||||||
return fmt.Sprintf(` | ||||||
data "azurerm_subscription" "current" {} | ||||||
|
@@ -448,7 +492,7 @@ resource "azuread_group" "test" { | |||||
name = "acctestspa-%d" | ||||||
} | ||||||
|
||||||
resource "azurerm_role_assignment" "test" { | ||||||
resource ServicePrincipal { | ||||||
WodansSon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
name = "%s" | ||||||
scope = "${data.azurerm_subscription.current.id}" | ||||||
role_definition_name = "Reader" | ||||||
|
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.
presumably
Unknown
shouldn't be in this list?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.
No, it was on purpose
Unknown
is actually a valid value,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.
just because it's in the list supported by the API doesn't mean it should be exposed to users unfortunately, Unknown is generally present to indicate a value that's unsupported on this API version (for example if it's been created on a newer/older version which is no longer supported)
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.
@tombuildsstuff I have asked the service team about the expected behavior and the purpose of the
Unknown
value.