Skip to content

Commit 395a262

Browse files
authored
Merge pull request #7991 from cormacpayne/graph-transfer
Remove Resources dependency on common Graph.RBAC library and use SDK instead
2 parents 9c62a26 + 2bf8d21 commit 395a262

File tree

66 files changed

+1922
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1922
-215
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</PropertyGroup>
2525

2626
<ItemGroup Condition="'$(TargetFramework)' != 'net472'">
27+
<PackageReference Include="Microsoft.Azure.Graph.RBAC" Version="3.5.0-preview" />
2728
<PackageReference Include="Microsoft.Azure.Management.Authorization" Version="2.8.1-preview" />
2829
<PackageReference Include="Microsoft.Azure.Management.ManagementGroups" Version="1.1.1-preview" />
2930
</ItemGroup>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System.Reflection;
16-
using Microsoft.Azure.Graph.RBAC.Version1_6;
17-
using Microsoft.Azure.Graph.RBAC.Version1_6.Models;
16+
using Microsoft.Azure.Graph.RBAC;
17+
using Microsoft.Azure.Graph.RBAC.Models;
1818
using Microsoft.Azure.ServiceManagemenet.Common.Models;
1919
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2020
using Microsoft.WindowsAzure.Commands.ScenarioTest;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function New-AzureRmRoleAssignmentWithId
6666
{
6767
[CmdletBinding()]
6868
param(
69-
[Guid] [Parameter()] [alias("Id", "PrincipalId")] $ObjectId,
69+
[string] [Parameter()] [alias("Id", "PrincipalId")] $ObjectId,
7070
[string] [Parameter()] [alias("Email", "UserPrincipalName")] $SignInName,
7171
[string] [Parameter()] [alias("SPN", "ServicePrincipalName")] $ApplicationId,
7272
[string] [Parameter()] $ResourceGroupName,
@@ -85,7 +85,7 @@ function New-AzureRmRoleAssignmentWithId
8585
$cmdlet.DefaultProfile = $profile
8686
$cmdlet.CommandRuntime = $PSCmdlet.CommandRuntime
8787

88-
if ($ObjectId -ne $null -and $ObjectId -ne [System.Guid]::Empty)
88+
if (-not ([string]::IsNullOrEmpty($ObjectId)))
8989
{
9090
$cmdlet.ObjectId = $ObjectId
9191
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2626
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2727
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
28-
using Microsoft.Azure.Graph.RBAC.Version1_6;
28+
using Microsoft.Azure.Graph.RBAC;
2929
using Microsoft.Azure.Management.Authorization;
3030
using Microsoft.Azure.Management.ManagementGroups;
3131
using Microsoft.Azure.Management.ResourceManager;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// ----------------------------------------------------------------------------------
1414

1515

16-
using Microsoft.Azure.Graph.RBAC.Version1_6;
17-
using Microsoft.Azure.Graph.RBAC.Version1_6.Models;
16+
using Microsoft.Azure.Graph.RBAC;
17+
using Microsoft.Azure.Graph.RBAC.Models;
1818
using Microsoft.Azure.Management.Authorization;
1919
using Microsoft.Azure.Management.ResourceManager;
2020
using Microsoft.Azure.Management.ResourceManager.Models;

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function Test-RaDeleteByPSRoleAssignment
112112

113113
# Test
114114
$newAssignment = New-AzureRmRoleAssignmentWithId `
115-
-ObjectId $users[0].Id.Guid `
115+
-ObjectId $users[0].Id `
116116
-RoleDefinitionName $definitionName `
117117
-Scope $scope `
118118
-RoleAssignmentId c7acc224-7df3-461a-8640-85d7bd15b5da
@@ -140,7 +140,7 @@ function Test-RaByScope
140140

141141
# Test
142142
$newAssignment = New-AzureRmRoleAssignmentWithId `
143-
-ObjectId $users[0].Id.Guid `
143+
-ObjectId $users[0].Id `
144144
-RoleDefinitionName $definitionName `
145145
-Scope $assignmentScope `
146146
-RoleAssignmentId 54e1188f-65ba-4b58-9bc3-a252adedcc7b
@@ -174,7 +174,7 @@ function Test-RaById
174174

175175
# Test
176176
$newAssignment = New-AzureRmRoleAssignmentWithId `
177-
-ObjectId $users[0].Id.Guid `
177+
-ObjectId $users[0].Id `
178178
-RoleDefinitionName $definitionName `
179179
-Scope $assignmentScope `
180180
-RoleAssignmentId 93cb604e-14dc-426b-834e-bf7bb3826cbc
@@ -210,7 +210,7 @@ function Test-RaByResourceGroup
210210

211211
# Test
212212
$newAssignment = New-AzureRmRoleAssignmentWithId `
213-
-ObjectId $users[0].Id.Guid `
213+
-ObjectId $users[0].Id `
214214
-RoleDefinitionName $definitionName `
215215
-ResourceGroupName $resourceGroups[0].ResourceGroupName `
216216
-RoleAssignmentId 8748e3e7-2cc7-41a9-81ed-b704b6d328a5
@@ -243,7 +243,7 @@ function Test-RaByResource
243243

244244
# Test
245245
$newAssignment = New-AzureRmRoleAssignmentWithId `
246-
-ObjectId $groups[0].Id.Guid `
246+
-ObjectId $groups[0].Id `
247247
-RoleDefinitionName $definitionName `
248248
-ResourceGroupName $resource.ResourceGroupName `
249249
-ResourceType $resource.ResourceType `
@@ -280,23 +280,23 @@ function Test-RaValidateInputParameters ($cmdName)
280280
# Check if Scope is valid.
281281
$scope = "/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/Should be 'ResourceGroups'/any group name"
282282
$invalidScope = "Scope '/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/Should be 'ResourceGroups'/any group name' should begin with '/subscriptions/<subid>/resourceGroups'."
283-
Assert-Throws { invoke-expression ($cmdName + " -Scope `"" + $scope + "`" -ObjectId " + $groups[0].Id.Guid + " -RoleDefinitionName " + $definitionName) } $invalidScope
283+
Assert-Throws { invoke-expression ($cmdName + " -Scope `"" + $scope + "`" -ObjectId " + $groups[0].Id + " -RoleDefinitionName " + $definitionName) } $invalidScope
284284

285285
$scope = "/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups"
286286
$invalidScope = "Scope '/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups' should have even number of parts."
287-
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id.Guid -RoleDefinitionName $definitionName } $invalidScope
287+
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id -RoleDefinitionName $definitionName } $invalidScope
288288

289289
$scope = "/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups/"
290290
$invalidScope = "Scope '/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups' should have even number of parts."
291-
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id.Guid -RoleDefinitionName $definitionName } $invalidScope
291+
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id -RoleDefinitionName $definitionName } $invalidScope
292292

293293
$scope = "/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups/groupname/Should be 'Providers'/any provider name"
294294
$invalidScope = "Scope '/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups/groupname/Should be 'Providers'/any provider name' should begin with '/subscriptions/<subid>/resourceGroups/<groupname>/providers'."
295-
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id.Guid -RoleDefinitionName $definitionName } $invalidScope
295+
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id -RoleDefinitionName $definitionName } $invalidScope
296296

297297
$scope = "/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups/groupname/Providers/providername"
298298
$invalidScope = "Scope '/subscriptions/e9ee799d-6ab2-4084-b952-e7c86344bbab/ResourceGroups/groupname/Providers/providername' should have at least one pair of resource type and resource name. e.g. '/subscriptions/<subid>/resourceGroups/<groupname>/providers/<providername>/<resourcetype>/<resourcename>'."
299-
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id.Guid -RoleDefinitionName $definitionName } $invalidScope
299+
Assert-Throws { &$cmdName -Scope $scope -ObjectId $groups[0].Id -RoleDefinitionName $definitionName } $invalidScope
300300

301301
# Check if ResourceType is valid
302302
Assert-AreEqual $resource.ResourceType "Microsoft.Web/sites"
@@ -305,7 +305,7 @@ function Test-RaValidateInputParameters ($cmdName)
305305
$resource.ResourceType = "Microsoft.KeyVault/"
306306
$invalidResourceType = "Scope '/subscriptions/"+$subscription.Id+"/resourceGroups/"+$resource.ResourceGroupName+"/providers/Microsoft.KeyVault/"+$resource.Name+"' should have even number of parts."
307307
Assert-Throws { &$cmdName `
308-
-ObjectId $groups[0].Id.Guid `
308+
-ObjectId $groups[0].Id `
309309
-RoleDefinitionName $definitionName `
310310
-ResourceGroupName $resource.ResourceGroupName `
311311
-ResourceType $resource.ResourceType `
@@ -455,7 +455,7 @@ function Test-RaDeletionByScope
455455

456456
# Test
457457
$newAssignment = New-AzureRmRoleAssignmentWithId `
458-
-ObjectId $users[0].Id.Guid `
458+
-ObjectId $users[0].Id `
459459
-RoleDefinitionName $definitionName `
460460
-Scope $scope `
461461
-RoleAssignmentId 238799bf-1593-45d7-a90d-f3edbceb3bc7
@@ -489,7 +489,7 @@ function Test-RaDeletionByScopeAtRootScope
489489

490490
# Test
491491
$newAssignment = New-AzureRmRoleAssignmentWithId `
492-
-ObjectId $users[0].Id.Guid `
492+
-ObjectId $users[0].Id `
493493
-RoleDefinitionName $definitionName `
494494
-Scope $scope `
495495
-RoleAssignmentId f3c560f8-afaa-4263-b1d7-e34e0ab49fc7
@@ -528,12 +528,12 @@ function Test-RaPropertiesValidation
528528
$rd = Get-AzureRmRoleDefinition -Name "Custom Reader Properties Test"
529529

530530
$newAssignment = New-AzureRmRoleAssignmentWithId `
531-
-ObjectId $users[0].Id.Guid `
531+
-ObjectId $users[0].Id `
532532
-RoleDefinitionName $roleDef.Name `
533533
-Scope $scope `
534534
-RoleAssignmentId 584d33a3-b14d-4eb4-863e-0df67b178389
535535

536-
$assignments = Get-AzureRmRoleAssignment -ObjectId $users[0].Id.Guid
536+
$assignments = Get-AzureRmRoleAssignment -ObjectId $users[0].Id
537537
Assert-NotNull $assignments
538538

539539
foreach ($assignment in $assignments){
@@ -570,7 +570,7 @@ function Test-RaDelegation
570570

571571
# Test
572572
$newAssignment = New-AzureRmRoleAssignmentWithId `
573-
-ObjectId $users[0].Id.Guid `
573+
-ObjectId $users[0].Id `
574574
-RoleDefinitionName $definitionName `
575575
-Scope $assignmentScope `
576576
-AllowDelegation `
@@ -606,18 +606,18 @@ function Test-RaGetByScope
606606

607607
# Test
608608
$newAssignment1 = New-AzureRmRoleAssignmentWithId `
609-
-ObjectId $users[0].Id.Guid `
609+
-ObjectId $users[0].Id `
610610
-RoleDefinitionName $definitionName `
611611
-Scope $scope1 `
612612
-RoleAssignmentId 08fe91d5-b917-4d76-81d7-581ff5a99cab
613613

614614
$newAssignment2 = New-AzureRmRoleAssignmentWithId `
615-
-ObjectId $users[0].Id.Guid `
615+
-ObjectId $users[0].Id `
616616
-RoleDefinitionName $definitionName `
617617
-Scope $scope2 `
618618
-RoleAssignmentId fa1a4d3b-2cca-406b-8956-6b6b32377641
619619

620-
$ras = Get-AzureRmRoleAssignment -ObjectId $users[0].Id.Guid `
620+
$ras = Get-AzureRmRoleAssignment -ObjectId $users[0].Id `
621621
-RoleDefinitionName $definitionName `
622622
-Scope $scope1
623623

@@ -664,7 +664,7 @@ function DeleteRoleAssignment
664664
{
665665
param([Parameter(Mandatory=$true)] [object] $roleAssignment)
666666

667-
Remove-AzureRmRoleAssignment -ObjectId $roleAssignment.ObjectId.Guid `
667+
Remove-AzureRmRoleAssignment -ObjectId $roleAssignment.ObjectId `
668668
-Scope $roleAssignment.Scope `
669669
-RoleDefinitionName $roleAssignment.RoleDefinitionName
670670
}
@@ -677,7 +677,7 @@ function VerifyRoleAssignmentDeleted
677677
{
678678
param([Parameter(Mandatory=$true)] [object] $roleAssignment)
679679

680-
$deletedRoleAssignment = Get-AzureRmRoleAssignment -ObjectId $roleAssignment.ObjectId.Guid `
680+
$deletedRoleAssignment = Get-AzureRmRoleAssignment -ObjectId $roleAssignment.ObjectId `
681681
-Scope $roleAssignment.Scope `
682682
-RoleDefinitionName $roleAssignment.RoleDefinitionName | where {$_.roleAssignmentId -eq $roleAssignment.roleAssignmentId}
683683
Assert-Null $deletedRoleAssignment
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.ResourceManager.Common;
16+
using Microsoft.Azure.Graph.RBAC.Models;
17+
using System;
18+
using System.Management.Automation;
19+
using System.Net;
20+
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
21+
22+
namespace Microsoft.Azure.Commands.ActiveDirectory
23+
{
24+
public abstract class ActiveDirectoryBaseCmdlet : AzureRMCmdlet
25+
{
26+
private ActiveDirectoryClient _activeDirectoryClient;
27+
28+
public ActiveDirectoryClient ActiveDirectoryClient
29+
{
30+
get
31+
{
32+
if (_activeDirectoryClient == null)
33+
{
34+
_activeDirectoryClient = new ActiveDirectoryClient(DefaultProfile.DefaultContext);
35+
}
36+
37+
return _activeDirectoryClient;
38+
}
39+
40+
set { _activeDirectoryClient = value; }
41+
}
42+
43+
/// <summary>
44+
/// Handles graph exceptions thrown by client
45+
/// </summary>
46+
/// <param name="exception"></param>
47+
private void HandleException(Exception exception)
48+
{
49+
Exception targetEx = exception;
50+
string targetErrorId = String.Empty;
51+
ErrorCategory targetErrorCategory = ErrorCategory.NotSpecified;
52+
var graphEx = exception as GraphErrorException;
53+
54+
if (graphEx == null)
55+
{
56+
throw exception;
57+
}
58+
59+
if (graphEx.Body != null && graphEx.Body.Message != null && graphEx.Body.Code != null)
60+
{
61+
WriteDebug(String.Format(ProjectResources.GraphException, graphEx.Body.Code, graphEx.Body.Message));
62+
targetEx = new Exception(graphEx.Body.Message);
63+
targetErrorId = graphEx.Body.Code;
64+
}
65+
else
66+
{
67+
if (graphEx.Response != null && graphEx.Response.StatusCode == HttpStatusCode.NotFound)
68+
{
69+
targetErrorCategory = ErrorCategory.InvalidArgument;
70+
}
71+
else
72+
{
73+
targetErrorCategory = ErrorCategory.InvalidOperation;
74+
}
75+
76+
Exception parsedException = ParseResponse(graphEx);
77+
targetEx = parsedException ?? targetEx;
78+
}
79+
80+
var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null);
81+
WriteError(errorRecord);
82+
}
83+
84+
85+
private Exception ParseResponse(GraphErrorException graphEx)
86+
{
87+
if (graphEx?.Response?.Content != null)
88+
{
89+
int exceptionMessageIndex = graphEx.Response.Content.IndexOf("\"value\":", StringComparison.CurrentCultureIgnoreCase);
90+
if (exceptionMessageIndex > 0)
91+
{
92+
string substring = graphEx.Response.Content.Substring(exceptionMessageIndex + 9);
93+
// the start index is added 9, so as to remove the delimiter \"value\":\
94+
string exceptionDetails = substring.Substring(0, substring.IndexOf("\"}"));
95+
return new Exception(exceptionDetails);
96+
}
97+
}
98+
99+
return null;
100+
}
101+
102+
protected void ExecutionBlock(Action execAction)
103+
{
104+
try
105+
{
106+
execAction();
107+
}
108+
catch (Exception exception)
109+
{
110+
WriteDebug(String.Format(ProjectResources.ExceptionInExecution, exception.GetType()));
111+
HandleException(exception);
112+
}
113+
}
114+
}
115+
}

src/ResourceManager/Resources/Commands.Resources/ActiveDirectory/AddAzureADGroupMemberCommand.cs renamed to src/ResourceManager/Resources/Commands.Resources/ActiveDirectory/Cmdlets/AddAzureADGroupMemberCommand.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16-
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
17-
using Microsoft.Azure.Graph.RBAC.Version1_6.Models;
16+
using Microsoft.Azure.Graph.RBAC.Models;
1817
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1918
using System;
2019
using System.Collections.Generic;
21-
using System.Linq;
2220
using System.Management.Automation;
2321

2422
namespace Microsoft.Azure.Commands.ActiveDirectory
@@ -33,7 +31,7 @@ public class AddAzureADGroupMemberCommand : ActiveDirectoryBaseCmdlet
3331
[Parameter(Mandatory = true, ParameterSetName = ParameterSet.MemberObjectIdWithGroupObject, HelpMessage = "The object id of the member(s) to add to the group.")]
3432
[Parameter(Mandatory = true, ParameterSetName = ParameterSet.MemberObjectIdWithGroupObjectId, HelpMessage = "The object id of the member(s) to add to the group.")]
3533
[ValidateNotNullOrEmpty]
36-
public Guid[] MemberObjectId { get; set; }
34+
public string[] MemberObjectId { get; set; }
3735

3836
[Parameter(Mandatory = true, ParameterSetName = ParameterSet.MemberUPNWithGroupDisplayName, HelpMessage = "The UPN of the member(s) to add to the group.")]
3937
[Parameter(Mandatory = true, ParameterSetName = ParameterSet.MemberUPNWithGroupObject, HelpMessage = "The UPN of the member(s) to add to the group.")]
@@ -43,7 +41,7 @@ public class AddAzureADGroupMemberCommand : ActiveDirectoryBaseCmdlet
4341
[Parameter(Mandatory = true, ParameterSetName = ParameterSet.MemberObjectIdWithGroupObjectId, HelpMessage = "The object id of the group to add the member(s) to.")]
4442
[Parameter(Mandatory = true, ParameterSetName = ParameterSet.MemberUPNWithGroupObjectId, HelpMessage = "The object id of the group to add the member(s) to.")]
4543
[ValidateNotNullOrEmpty]
46-
public Guid TargetGroupObjectId { get; set; }
44+
public string TargetGroupObjectId { get; set; }
4745

4846
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet.MemberObjectIdWithGroupObject, HelpMessage = "The object representation of the group to add the member(s) to.")]
4947
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet.MemberUPNWithGroupObject, HelpMessage = "The object representation of the group to add the member(s) to.")]
@@ -73,7 +71,7 @@ public override void ExecuteCmdlet()
7371

7472
if (this.IsParameterBound(c => c.MemberUserPrincipalName))
7573
{
76-
var memberObjectId = new List<Guid>();
74+
var memberObjectId = new List<string>();
7775
foreach (var memberUPN in MemberUserPrincipalName)
7876
{
7977
memberObjectId.Add(ActiveDirectoryClient.GetObjectIdFromUPN(memberUPN));

0 commit comments

Comments
 (0)