Skip to content

Commit dd85df9

Browse files
committed
Merge pull request Azure#1175 from shuagarw/DocumentationUpdatesAndFixes
Role commands documentation updates and some bug fixes
2 parents 25f5fe3 + 709f18a commit dd85df9

13 files changed

+1754
-748
lines changed

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Compile Include="TestMockSupport.cs" />
174174
<Compile Include="PSAzureAccount.cs" />
175175
<Compile Include="Properties\AssemblyInfo.cs" />
176+
<Compile Include="ValidateGuidNotEmpty.cs" />
176177
</ItemGroup>
177178
<ItemGroup>
178179
<EmbeddedResource Include="Properties\Resources.resx">
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Management.Automation;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.WindowsAzure.Commands.Common
9+
{
10+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
11+
public class ValidateGuidNotEmptyAttribute : ValidateArgumentsAttribute
12+
{
13+
protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)
14+
{
15+
if (arguments == null)
16+
{
17+
throw new ValidationMetadataException("Specify a parameter of type 'System.Guid' and try again.");
18+
}
19+
20+
Guid param = (Guid)arguments;
21+
if (param == Guid.Empty)
22+
{
23+
throw new ValidationMetadataException("Specify a non empty value of type 'System.Guid' and try again.");
24+
}
25+
}
26+
}
27+
}

src/ResourceManager/Resources/Commands.Resources/Microsoft.Azure.Commands.Resources.dll-Help.xml

Lines changed: 1650 additions & 714 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources/Models.Authorization/AuthorizationClient.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parame
134134
Guid roleAssignmentId = RoleAssignmentNames.Count == 0 ? Guid.NewGuid() : RoleAssignmentNames.Dequeue();
135135
string roleDefinitionId = !string.IsNullOrEmpty(parameters.RoleDefinitionName)
136136
? AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(subscriptionId, GetRoleRoleDefinition(parameters.RoleDefinitionName).Id)
137-
: parameters.RoleDefinitionId;
137+
: AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(subscriptionId, parameters.RoleDefinitionId);
138138

139139
RoleAssignmentCreateParameters createParameters = new RoleAssignmentCreateParameters
140140
{
@@ -189,7 +189,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
189189

190190
result.AddRange(AuthorizationManagementClient.RoleAssignments.List(parameters)
191191
.RoleAssignments
192-
.FilterRoleAssignmentsOnRoleId(options.RoleDefinitionId)
192+
.FilterRoleAssignmentsOnRoleId(AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(currentSubscription, options.RoleDefinitionId))
193193
.ToPSRoleAssignments(this, ActiveDirectoryClient, options.ExcludeAssignmentsForDeletedPrincipals));
194194

195195
// Filter out by scope
@@ -205,13 +205,15 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
205205

206206
result.AddRange(AuthorizationManagementClient.RoleAssignments.ListForScope(options.Scope, parameters)
207207
.RoleAssignments
208-
.FilterRoleAssignmentsOnRoleId(options.RoleDefinitionId)
208+
.FilterRoleAssignmentsOnRoleId(AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(currentSubscription, options.RoleDefinitionId))
209209
.ToPSRoleAssignments(this, ActiveDirectoryClient, options.ExcludeAssignmentsForDeletedPrincipals));
210210
}
211211
else
212212
{
213213
result.AddRange(AuthorizationManagementClient.RoleAssignments.List(parameters)
214-
.RoleAssignments.ToPSRoleAssignments(this, ActiveDirectoryClient, options.ExcludeAssignmentsForDeletedPrincipals));
214+
.RoleAssignments
215+
.FilterRoleAssignmentsOnRoleId(AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(currentSubscription, options.RoleDefinitionId))
216+
.ToPSRoleAssignments(this, ActiveDirectoryClient, options.ExcludeAssignmentsForDeletedPrincipals));
215217
}
216218

217219
if (!string.IsNullOrEmpty(options.RoleDefinitionName))
@@ -251,11 +253,11 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
251253
/// </summary>
252254
/// <param name="options">The role assignment filtering options</param>
253255
/// <returns>The deleted role assignments</returns>
254-
public IEnumerable<PSRoleAssignment> RemoveRoleAssignment(FilterRoleAssignmentsOptions options)
256+
public IEnumerable<PSRoleAssignment> RemoveRoleAssignment(FilterRoleAssignmentsOptions options, string subscriptionId)
255257
{
256258
// Match role assignments at exact scope. Ideally, atmost 1 roleAssignment should match the criteria
257259
// but an edge case can have multiple role assignments to the same role or multiple role assignments to different roles, with same name.
258-
IEnumerable<PSRoleAssignment> roleAssignments = FilterRoleAssignments(options, currentSubscription: string.Empty)
260+
IEnumerable<PSRoleAssignment> roleAssignments = FilterRoleAssignments(options, subscriptionId)
259261
.Where(ra => ra.Scope == options.Scope.TrimEnd('/'));
260262

261263
if (roleAssignments == null || !roleAssignments.Any())
@@ -362,6 +364,8 @@ public PSRoleDefinition UpdateRoleDefinition(PSRoleDefinition role, string subsc
362364
roleDefinition.AssignableScopes = role.AssignableScopes ?? roleDefinition.AssignableScopes;
363365
roleDefinition.Description = role.Description ?? roleDefinition.Description;
364366

367+
ValidateRoleDefinition(roleDefinition);
368+
365369
return
366370
AuthorizationManagementClient.RoleDefinitions.CreateOrUpdate(
367371
roleDefinitionId,
@@ -444,6 +448,11 @@ private static void ValidateRoleDefinition(PSRoleDefinition roleDefinition)
444448
throw new ArgumentException(ProjectResources.InvalidRoleDefinitionName);
445449
}
446450

451+
if (string.IsNullOrWhiteSpace(roleDefinition.Description))
452+
{
453+
throw new ArgumentException(ProjectResources.InvalidRoleDefinitionDescription);
454+
}
455+
447456
if (roleDefinition.AssignableScopes == null || !roleDefinition.AssignableScopes.Any())
448457
{
449458
throw new ArgumentException(ProjectResources.InvalidAssignableScopes);

src/ResourceManager/Resources/Commands.Resources/Models.Authorization/AuthorizationHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class AuthorizationHelper
1212

1313
public static string GetRoleDefinitionFullyQualifiedId(string subscriptionId, string roleDefinitionGuid)
1414
{
15+
if(string.IsNullOrEmpty(roleDefinitionGuid))
16+
{
17+
return null;
18+
}
19+
1520
return string.Concat(string.Format(AuthorizationHelper.roleDefinitionIdPrefixFormat, subscriptionId), roleDefinitionGuid);
1621
}
1722
}

src/ResourceManager/Resources/Commands.Resources/Models.Authorization/FilterRoleAssignmentsOptions.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,12 @@ namespace Microsoft.Azure.Commands.Resources.Models.Authorization
1818
{
1919
public class FilterRoleAssignmentsOptions
2020
{
21-
private string roleDefinitionGuid;
22-
2321
public string RoleDefinitionName { get; set; }
2422

25-
public string RoleDefinitionId
26-
{
27-
get
28-
{
29-
if (string.IsNullOrEmpty(roleDefinitionGuid))
30-
{
31-
return null;
32-
}
33-
34-
return AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(this.ResourceIdentifier.Subscription, roleDefinitionGuid);
35-
}
36-
set
37-
{
38-
roleDefinitionGuid = value;
39-
}
40-
}
23+
/// <summary>
24+
/// RoleDefinitionId Guid
25+
/// </summary>
26+
public string RoleDefinitionId { get; set; }
4127

4228
private string scope;
4329

src/ResourceManager/Resources/Commands.Resources/Properties/Resources.Designer.cs

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Resources/Commands.Resources/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,10 @@
333333
<data name="ProviderOperationUnsupportedWildcard" xml:space="preserve">
334334
<value>Wildcard character ? is not supported.</value>
335335
</data>
336+
<data name="InvalidRoleDefinitionDescription" xml:space="preserve">
337+
<value>RoleDefinition Description is invalid.</value>
338+
</data>
339+
<data name="RemoveRoleDefinitionWithName" xml:space="preserve">
340+
<value>Are you sure you want to remove role definition with name '{0}'.</value>
341+
</data>
336342
</root>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Azure.Commands.Resources.Models;
1616
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
1717
using Microsoft.Azure.Commands.Resources.Models.Authorization;
18+
using Microsoft.WindowsAzure.Commands.Common;
1819
using System;
1920
using System.Collections.Generic;
2021
using System.Management.Automation;
@@ -35,7 +36,9 @@ public class GetAzureRoleAssignmentCommand : ResourcesBaseCmdlet
3536
HelpMessage = "The user or group object id.")]
3637
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithObjectId,
3738
HelpMessage = "The user or group object id.")]
38-
[ValidateNotNullOrEmpty]
39+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
40+
HelpMessage = "The user or group object id.")]
41+
[ValidateGuidNotEmpty]
3942
[Alias("Id", "PrincipalId")]
4043
public Guid ObjectId { get; set; }
4144

@@ -150,6 +153,11 @@ public class GetAzureRoleAssignmentCommand : ResourcesBaseCmdlet
150153
[ValidateNotNullOrEmpty]
151154
public string RoleDefinitionName { get; set; }
152155

156+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
157+
HelpMessage = "Role Id the principal is assigned to.")]
158+
[ValidateGuidNotEmpty]
159+
public Guid RoleDefinitionId { get; set; }
160+
153161
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.Scope,
154162
HelpMessage = "Scope of the role assignment. In the format of relative URI.")]
155163
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithObjectId,
@@ -158,6 +166,8 @@ public class GetAzureRoleAssignmentCommand : ResourcesBaseCmdlet
158166
HelpMessage = "Scope of the role assignment. In the format of relative URI.")]
159167
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithSPN,
160168
HelpMessage = "Scope of the role assignment. In the format of relative URI.")]
169+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
170+
HelpMessage = "Scope of the role assignment. In the format of relative URI.")]
161171
[ValidateNotNullOrEmpty]
162172
public string Scope { get; set; }
163173

@@ -207,6 +217,7 @@ protected override void ProcessRecord()
207217
{
208218
Scope = Scope,
209219
RoleDefinitionName = RoleDefinitionName,
220+
RoleDefinitionId = RoleDefinitionId == Guid.Empty ? null : RoleDefinitionId.ToString(),
210221
ADObjectFilter = new ADObjectFilterOptions
211222
{
212223
SignInName = SignInName,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Azure.Commands.Resources.Models;
1616
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
1717
using Microsoft.Azure.Commands.Resources.Models.Authorization;
18+
using Microsoft.WindowsAzure.Commands.Common;
1819
using System;
1920
using System.Management.Automation;
2021

@@ -36,7 +37,7 @@ public class NewAzureRoleAssignmentCommand : ResourcesBaseCmdlet
3637
HelpMessage = "The user or group object id.")]
3738
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
3839
HelpMessage = "The user or group object id.")]
39-
[ValidateNotNullOrEmpty]
40+
[ValidateGuidNotEmpty]
4041
[Alias("Id", "PrincipalId")]
4142
public Guid ObjectId { get; set; }
4243

@@ -140,7 +141,7 @@ public class NewAzureRoleAssignmentCommand : ResourcesBaseCmdlet
140141

141142
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
142143
HelpMessage = "Role Id the principal is assigned to.")]
143-
[ValidateNotNullOrEmpty]
144+
[ValidateGuidNotEmpty]
144145
public Guid RoleDefinitionId { get; set; }
145146

146147
protected override void ProcessRecord()

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Azure.Commands.Resources.Models;
1616
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
1717
using Microsoft.Azure.Commands.Resources.Models.Authorization;
18+
using Microsoft.WindowsAzure.Commands.Common;
1819
using System;
1920
using System.Collections.Generic;
2021
using System.Management.Automation;
@@ -38,7 +39,7 @@ public class RemoveAzureRoleAssignmentCommand : ResourcesBaseCmdlet
3839
HelpMessage = "The user or group object id.")]
3940
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
4041
HelpMessage = "The user or group object id.")]
41-
[ValidateNotNullOrEmpty]
42+
[ValidateGuidNotEmpty]
4243
[Alias("Id", "PrincipalId")]
4344
public Guid ObjectId { get; set; }
4445

@@ -142,7 +143,7 @@ public class RemoveAzureRoleAssignmentCommand : ResourcesBaseCmdlet
142143

143144
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
144145
HelpMessage = "Role Id the principal is assigned to.")]
145-
[ValidateNotNullOrEmpty]
146+
[ValidateGuidNotEmpty]
146147
public Guid RoleDefinitionId { get; set; }
147148

148149
[Parameter(Mandatory = false)]
@@ -184,7 +185,7 @@ protected override void ProcessRecord()
184185
options.RoleDefinitionName ?? RoleDefinitionId.ToString()),
185186
ProjectResources.RemovingRoleAssignment,
186187
null,
187-
() => roleAssignments = PoliciesClient.RemoveRoleAssignment(options));
188+
() => roleAssignments = PoliciesClient.RemoveRoleAssignment(options, DefaultProfile.Context.Subscription.Id.ToString()));
188189

189190
if (PassThru)
190191
{

src/ResourceManager/Resources/Commands.Resources/RoleDefinitions/GetAzureRoleDefinitionCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Azure.Commands.Resources.Models;
1616
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
1717
using Microsoft.Azure.Commands.Resources.Models.Authorization;
18+
using Microsoft.WindowsAzure.Commands.Common;
1819
using System;
1920
using System.Collections.Generic;
2021
using System.Management.Automation;
@@ -34,7 +35,7 @@ public class GetAzureRoleDefinitionCommand : ResourcesBaseCmdlet
3435

3536
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleDefinitionId,
3637
HelpMessage = "Role definition id.")]
37-
[ValidateNotNullOrEmpty]
38+
[ValidateGuidNotEmpty]
3839
public Guid Id { get; set; }
3940

4041
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleDefinitionCustom,

src/ResourceManager/Resources/Commands.Resources/RoleDefinitions/RemoveAzureRoleDefinitionCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.Azure.Commands.Resources.Models.Authorization;
1919
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
2020
using System;
21+
using Microsoft.WindowsAzure.Commands.Common;
2122

2223
namespace Microsoft.Azure.Commands.Resources
2324
{
@@ -29,7 +30,7 @@ public class RemoveAzureRoleDefinitionCommand : ResourcesBaseCmdlet
2930
{
3031
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleDefinitionId,
3132
HelpMessage = "Role definition id")]
32-
[ValidateNotNullOrEmpty]
33+
[ValidateGuidNotEmpty]
3334
public Guid Id { get; set; }
3435

3536
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleDefinitionName,
@@ -47,19 +48,22 @@ protected override void ProcessRecord()
4748
{
4849
PSRoleDefinition roleDefinition = null;
4950
Action action = null;
51+
string confirmMessage = null;
5052

5153
if(Id != Guid.Empty)
5254
{
5355
action = (() => roleDefinition = PoliciesClient.RemoveRoleDefinition(Id, DefaultProfile.Context.Subscription.Id.ToString()));
56+
confirmMessage = string.Format(ProjectResources.RemoveRoleDefinition, Id);
5457
}
5558
else
5659
{
5760
action = (() => roleDefinition = PoliciesClient.RemoveRoleDefinition(Name, DefaultProfile.Context.Subscription.Id.ToString()));
61+
confirmMessage = string.Format(ProjectResources.RemoveRoleDefinitionWithName, Name);
5862
}
5963

6064
ConfirmAction(
6165
Force.IsPresent,
62-
string.Format(ProjectResources.RemoveRoleDefinition, Id),
66+
confirmMessage,
6367
ProjectResources.RemoveRoleDefinition,
6468
Id.ToString(),
6569
action);

0 commit comments

Comments
 (0)