Skip to content

Commit 0e49447

Browse files
authored
[Az.Resources] Fix unexplicable error message when subscription and scope are both null. (#22730)
* Fix unexplicate error message when subscription and scope are both null * Delete unnecessary namespace * Delete unnecessary namespace * Update ChangeLog * Resolve comments * Resolve Comments
1 parent 3b6bef5 commit 0e49447

File tree

9 files changed

+50
-10
lines changed

9 files changed

+50
-10
lines changed

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed unexplicable error message when subscription and scope are neither provided in `Get-AzRoleDefinition`. [#22716]
2223

2324
## Version 6.10.0
2425
* Added breaking change warnings for Azure Policy cmdlets.

src/Resources/Resources/Models.ResourceGroups/ResourcesBaseCmdlet.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace Microsoft.Azure.Commands.Resources.Models
1717
{
1818
using Authorization;
1919
using ResourceManager.Common;
20+
using System.Management.Automation;
21+
using System;
2022

2123
/// <summary>
2224
/// Base class for all resources cmdlets
@@ -114,5 +116,10 @@ public virtual string DetermineParameterSetName()
114116
{
115117
return ParameterSetName;
116118
}
119+
120+
protected void WriteTerminatingError(string message, params object[] args)
121+
{
122+
ThrowTerminatingError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
123+
}
117124
}
118125
}

src/Resources/Resources/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/Resources/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,7 @@ This means the role assignment was not able to be created. Please assign a role
463463
<data name="SuccessfullRARemove" xml:space="preserve">
464464
<value>Succesfully removed role assignment for AD object '{0}' on scope '{1}' with role definition '{2}'</value>
465465
</data>
466+
<data name="ScopeAndSubscriptionNeitherProvided" xml:space="preserve">
467+
<value>No subscription was found in the default profile and no scope was specified. Either specify a scope or use a tenant with a subscription to run the command.</value>
468+
</data>
466469
</root>

src/Resources/Resources/RoleAssignments/GetAzureRoleAssignmentCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System;
2222
using System.Collections.Generic;
2323
using System.Management.Automation;
24+
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
2425

2526
namespace Microsoft.Azure.Commands.Resources
2627
{
@@ -245,6 +246,11 @@ public override void ExecuteCmdlet()
245246
IncludeClassicAdministrators = IncludeClassicAdministrators.IsPresent,
246247
};
247248

249+
if (options.Scope == null && options.ResourceIdentifier.Subscription == null)
250+
{
251+
WriteTerminatingError(ProjectResources.ScopeAndSubscriptionNeitherProvided);
252+
}
253+
248254
AuthorizationClient.ValidateScope(options.Scope, true);
249255

250256
List<PSRoleAssignment> ra = PoliciesClient.FilterRoleAssignments(options, DefaultProfile.DefaultContext.Subscription.Id);

src/Resources/Resources/RoleAssignments/NewAzureRoleAssignmentCommand.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System;
2525
using System.IO;
2626
using System.Management.Automation;
27+
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
2728

2829
namespace Microsoft.Azure.Commands.Resources
2930
{
@@ -323,14 +324,19 @@ public override void ExecuteCmdlet()
323324
ResourceGroupName = ResourceGroupName,
324325
ResourceName = ResourceName,
325326
ResourceType = ResourceType,
326-
Subscription = DefaultProfile.DefaultContext.Subscription != null ? DefaultProfile.DefaultContext.Subscription.Id : "",
327+
Subscription = DefaultProfile.DefaultContext.Subscription?.Id?.ToString(),
327328
},
328329
CanDelegate = AllowDelegation.IsPresent ? true : false,
329330
Description = Description,
330331
Condition = Condition,
331332
ConditionVersion = ConditionVersion,
332333
};
333334

335+
if (parameters.Scope == null && parameters.ResourceIdentifier.Subscription == null)
336+
{
337+
WriteTerminatingError(ProjectResources.ScopeAndSubscriptionNeitherProvided);
338+
}
339+
334340
AuthorizationClient.ValidateScope(parameters.Scope, true);
335341

336342
WriteObject(PoliciesClient.CreateRoleAssignment(parameters, RoleAssignmentId));

src/Resources/Resources/RoleAssignments/RemoveAzureRoleAssignmentCommand.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,19 @@ public override void ExecuteCmdlet()
188188
ResourceGroupName = ResourceGroupName,
189189
ResourceName = ResourceName,
190190
ResourceType = ResourceType,
191-
Subscription = DefaultProfile.DefaultContext.Subscription.Id
191+
Subscription = DefaultProfile.DefaultContext.Subscription?.Id?.ToString()
192192
},
193193
// we should never expand principal groups in the Delete scenario
194194
ExpandPrincipalGroups = false,
195195
// never include classic administrators in the Delete scenario
196196
IncludeClassicAdministrators = false
197197
};
198198

199+
if (options.Scope == null && options.ResourceIdentifier.Subscription == null)
200+
{
201+
WriteTerminatingError(ProjectResources.ScopeAndSubscriptionNeitherProvided);
202+
}
203+
199204
AuthorizationClient.ValidateScope(options.Scope, true);
200205
ConfirmAction(
201206
string.Format(ProjectResources.RemovingRoleAssignment, ObjectId, Scope, RoleDefinitionName),

src/Resources/Resources/RoleDefinitions/GetAzureRoleDefinitionCommand.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.Generic;
2222
using System.Linq;
2323
using System.Management.Automation;
24+
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
2425

2526
namespace Microsoft.Azure.Commands.Resources
2627
{
@@ -61,15 +62,15 @@ public override void ExecuteCmdlet()
6162
Scope = Scope,
6263
ResourceIdentifier = new ResourceIdentifier
6364
{
64-
Subscription = DefaultProfile.DefaultContext.Subscription.Id?.ToString()
65+
Subscription = DefaultProfile.DefaultContext.Subscription?.Id?.ToString()
6566
},
6667
RoleDefinitionId = Id,
6768
RoleDefinitionName = Name,
6869
};
6970

7071
if (options.Scope == null && options.ResourceIdentifier.Subscription == null)
7172
{
72-
WriteTerminatingError("No subscription was found in the default profile and no scope was specified. Either specify a scope or use a tenant with a subscription to run the command.");
73+
WriteTerminatingError(ProjectResources.ScopeAndSubscriptionNeitherProvided);
7374
}
7475

7576
AuthorizationClient.ValidateScope(options.Scope, true);
@@ -87,10 +88,7 @@ public override void ExecuteCmdlet()
8788
WriteObject(filteredRoleDefinitions, enumerateCollection: true);
8889
}
8990
}
90-
91-
private void WriteTerminatingError(string message, params object[] args)
92-
{
93-
ThrowTerminatingError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
94-
}
91+
92+
9593
}
9694
}

src/Resources/Resources/RoleDefinitions/RemoveAzureRoleDefinitionCommand.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ public override void ExecuteCmdlet()
8989
Scope = Scope,
9090
ResourceIdentifier = new ResourceIdentifier
9191
{
92-
Subscription = DefaultProfile.DefaultContext.Subscription.Id.ToString()
92+
Subscription = DefaultProfile.DefaultContext.Subscription?.Id?.ToString()
9393
}
9494
};
9595

96+
if (options.Scope == null && options.ResourceIdentifier.Subscription == null)
97+
{
98+
WriteTerminatingError(ProjectResources.ScopeAndSubscriptionNeitherProvided);
99+
}
100+
96101
AuthorizationClient.ValidateScope(options.Scope, true);
97102

98103
ConfirmAction(

0 commit comments

Comments
 (0)