Skip to content

Commit 8aa35e8

Browse files
authored
Merge pull request #333 from darshanhs90/darshanhs90/preview
update remove role assignment commandlet
2 parents 087cd3a + 287b8c9 commit 8aa35e8

File tree

12 files changed

+4651
-314
lines changed

12 files changed

+4651
-314
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
* Add -PolicySetDefinition, -Sku and -NotScope parameters to New-AzureRmPolicyAssignment and Set-AzureRmPolicyAssignment cmdlets
134134
* Add support to pass in policy url to New-AzureRmPolicyDefinition and Set-AzureRmPolicyDefinition cmdlets
135135
* Add -Mode parameter to New-AzureRmPolicyDefinition cmdlet
136+
* Add Support for removal of roleassignment using PSRoleAssignment object
137+
- Users can now use PSRoleassignmnet inputobject with Remove-AzureRMRoleAssignment commandlet to remove the roleassignment.
136138
* Add ManagedApplication cmdlets
137139
- New-AzureRmManagedApplication cmdlet to create a managed application
138140
- Get-AzureRmManagedApplication cmdlet to list all managed applications under a subscription or to get a specific managed application

src/Common/Commands.Common.Graph.RBAC/ActiveDirectory/ParameterSet.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,7 @@ public static class ParameterSet
133133
public const string SpObjectIdWithDisplayName = "SpObjectIdWithDisplayNameParameterSet";
134134

135135
public const string SPNWithDisplayName = "SPNWithDisplayNameParameterSet";
136-
}
136+
137+
public const string RoleAssignment = "RoleAssignmentParameterSet";
138+
}
137139
}

src/ResourceManager/Resources/AzureRM.Resources.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ PrivateData = @{
159159
* Add -PolicySetDefinition, -Sku and -NotScope parameters to New-AzureRmPolicyAssignment and Set-AzureRmPolicyAssignment cmdlets
160160
* Add support to pass in policy url to New-AzureRmPolicyDefinition and Set-AzureRmPolicyDefinition cmdlets
161161
* Add -Mode parameter to New-AzureRmPolicyDefinition cmdlet
162+
* Add Support for removal of roleassignment using PSRoleAssignment object
163+
- Users can now use PSRoleassignmnet inputobject with Remove-AzureRMRoleAssignment commandlet to remove the roleassignment.
162164
* Add ManagedApplicationDefinition cmdlets
163165
* Add ManagedApplication cmdlets
164166
'

src/ResourceManager/Resources/ChangeLog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
* Add -PolicySetDefinition, -Sku and -NotScope parameters to New-AzureRmPolicyAssignment and Set-AzureRmPolicyAssignment cmdlets
3737
* Add support to pass in policy url to New-AzureRmPolicyDefinition and Set-AzureRmPolicyDefinition cmdlets
3838
* Add -Mode parameter to New-AzureRmPolicyDefinition cmdlet
39-
39+
* Add Support for removal of roleassignment using PSRoleAssignment object
40+
- Users can now use PSRoleassignmnet inputobject with Remove-AzureRMRoleAssignment commandlet to remove the roleassignment.
41+
4042
## Version 4.3.1
4143

4244
## Version 4.3.0

src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@
694694
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests\RaUserPermissions_Setup.json">
695695
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
696696
</None>
697+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests\RaDeleteByPSRoleAssignment.json">
698+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
699+
</None>
697700
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests\RaUserPermissions_Test.json">
698701
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
699702
</None>

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/RoleAssignmentTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public void RaByScope()
5757
{
5858
ResourcesController.NewInstance.RunPsTest("Test-RaByScope");
5959
}
60+
61+
[Fact]
62+
[Trait(Category.AcceptanceType, Category.CheckIn)]
63+
public void RaDeleteByPSRoleAssignment()
64+
{
65+
ResourcesController.NewInstance.RunPsTest("Test-RaDeleteByPSRoleAssignment");
66+
}
6067

6168
[Fact]
6269
[Trait(Category.AcceptanceType, Category.CheckIn)]

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/RoleAssignmentTests.ps1

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ function Test-RaNegativeScenarios
5656
Assert-Throws { Get-AzureRmRoleAssignment -ServicePrincipalName $badSpn } $badObjectResult
5757
}
5858

59+
<#
60+
.SYNOPSIS
61+
Tests verifies delete scenario for RoleAssignments by using PSRoleAssignment Object
62+
#>
63+
function Test-RaDeleteByPSRoleAssignment
64+
{
65+
# Setup
66+
$definitionName = 'Reader'
67+
$users = Get-AzureRmADUser | Select-Object -First 1 -Wait
68+
$subscription = Get-AzureRmSubscription
69+
$resourceGroups = Get-AzureRmResourceGroup | Select-Object -Last 1 -Wait
70+
$scope = '/subscriptions/'+ $subscription[0].Id +'/resourceGroups/' + $resourceGroups[0].ResourceGroupName
71+
Assert-AreEqual 1 $users.Count "There should be at least one user to run the test."
72+
73+
# Test
74+
[Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClient]::RoleAssignmentNames.Enqueue("fa1a4d3b-2cca-406b-8956-6b6b32377641")
75+
$newAssignment = New-AzureRmRoleAssignment `
76+
-ObjectId $users[0].Id.Guid `
77+
-RoleDefinitionName $definitionName `
78+
-Scope $scope
79+
80+
Remove-AzureRmRoleAssignment $newAssignment
81+
82+
# Assert
83+
VerifyRoleAssignmentDeleted $newAssignment
84+
}
85+
5986
<#
6087
.SYNOPSIS
6188
Tests verifies creation and deletion of a RoleAssignments by Scope
@@ -193,11 +220,11 @@ function Test-RaValidateInputParameters ($cmdName)
193220
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id.Guid -RoleDefinitionName $definitionName } $invalidScope
194221

195222
# Check if ResourceType is valid
196-
Assert-AreEqual $resource.ResourceType "Microsoft.KeyVault/vaults"
197-
223+
Assert-AreEqual $resource.ResourceType "Microsoft.ServiceBus/namespaces"
224+
$subscription = Get-AzureRmSubscription | Select-Object -Last 1 -Wait
198225
# Below invalid resource type should not return 'Not supported api version'.
199226
$resource.ResourceType = "Microsoft.KeyVault/"
200-
$invalidResourceType = "Scope '/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/zzzzlastgroupzz/providers/Microsoft.KeyVault/zzzzlastgroupzz' should have even number of parts."
227+
$invalidResourceType = "Scope '/subscriptions/"+$subscription.Id+"/resourceGroups/"+$resource.ResourceGroupName+"/providers/Microsoft.KeyVault/"+$resource.ResourceGroupName+"' should have even number of parts."
201228
Assert-Throws { &$cmdName `
202229
-ObjectId $groups[0].Id.Guid `
203230
-RoleDefinitionName $definitionName `
@@ -213,7 +240,7 @@ Tests verifies creation and deletion of a RoleAssignments for Service principal
213240
function Test-RaByServicePrincipal
214241
{
215242
# Setup
216-
$definitionName = 'Reader'
243+
$definitionName = 'Contributor'
217244
$servicePrincipals = Get-AzureRmADServicePrincipal | Select-Object -Last 1 -Wait
218245
$subscription = Get-AzureRmSubscription
219246
$resourceGroups = Get-AzureRmResourceGroup | Select-Object -Last 1 -Wait

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests/RaByServicePrincipal.json

Lines changed: 364 additions & 269 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests/RaDeleteByPSRoleAssignment.json

Lines changed: 2726 additions & 0 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests/RaValidateInputParameters.json

Lines changed: 1472 additions & 39 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources/RoleAssignments/RemoveAzureRoleAssignmentCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,19 @@ public class RemoveAzureRoleAssignmentCommand : ResourcesBaseCmdlet
150150
[Parameter(Mandatory = false)]
151151
public SwitchParameter PassThru { get; set; }
152152

153+
[ValidateNotNullOrEmpty]
154+
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet.RoleAssignment, HelpMessage = "Role Assignment.")]
155+
public PSRoleAssignment InputObject { get; set; }
156+
153157
public override void ExecuteCmdlet()
154158
{
155159
IEnumerable<PSRoleAssignment> roleAssignments = null;
160+
if (InputObject != null) {
161+
Scope = InputObject.Scope;
162+
ObjectId = InputObject.ObjectId;
163+
RoleDefinitionName = InputObject.RoleDefinitionName;
164+
}
165+
156166
FilterRoleAssignmentsOptions options = new FilterRoleAssignmentsOptions()
157167
{
158168
Scope = Scope,

src/ResourceManager/Resources/Commands.Resources/help/Remove-AzureRmRoleAssignment.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ Remove-AzureRmRoleAssignment -ServicePrincipalName <String> [-Scope <String>] -R
8181
[-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]
8282
```
8383

84+
### RoleAssignmentParameterSet
85+
```
86+
Remove-AzureRmRoleAssignment [-InputObject] <PSRoleAssignment> [<CommonParameters>]
87+
```
88+
8489
## DESCRIPTION
8590
Use the Remove-AzureRmRoleAssignment commandlet to revoke access to any principal at given scope and given role.
8691

@@ -117,6 +122,14 @@ PS C:\> Remove-AzureRmRoleAssignment -ObjectId 36f81fc3-b00f-48cd-8218-3879f51ff
117122
Removes the role assignment to the group principal identified by the ObjectId and assigned to the Reader role.
118123
Defaults to using the current subscription as the scope to find the assignment to be deleted.
119124

125+
### -------------------------- Example 3 --------------------------
126+
```
127+
PS C:\> $roleassignment = Get-AzureRmRoleAssignment |Select-Object -First 1 -Wait
128+
PS C:\> Remove-AzureRmRoleAssignment -InputObject $roleassignment
129+
```
130+
131+
Removes the first role assignment object which is fetched from the Get-AzureRmRoleAssignment commandlet.
132+
120133
## PARAMETERS
121134

122135
### -ObjectId
@@ -299,6 +312,21 @@ Accept pipeline input: True (ByPropertyName)
299312
Accept wildcard characters: False
300313
```
301314
315+
### -InputObject
316+
Role Assignment object.
317+
318+
```yaml
319+
Type: PSRoleAssignment
320+
Parameter Sets: RoleAssignmentParameterSet
321+
Aliases:
322+
323+
Required: True
324+
Position: 0
325+
Default value: None
326+
Accept pipeline input: True
327+
Accept wildcard characters: False
328+
```
329+
302330
### -Confirm
303331
Prompts you for confirmation before running the cmdlet.
304332

0 commit comments

Comments
 (0)