Skip to content

Commit 4d135b5

Browse files
author
rakashya
committed
Merge branch 'build' of https://github.com/Azure/azure-powershell-pr into RevertChanges
Conflicts: src/ResourceManager/Batch/Commands.Batch.Test/Commands.Batch.Test.csproj src/ResourceManager/Batch/Commands.Batch.Test/packages.config src/ResourceManager/DataFactories/Commands.DataFactories.Test/Commands.DataFactories.Test.csproj src/ResourceManager/DataFactories/Commands.DataFactories.Test/packages.config src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/RoleDefinitionTests.cs src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/RoleDefinitionTests.ps1 src/ResourceManager/Resources/Commands.Resources.Test/packages.config src/ResourceManager/Resources/Commands.Resources/Commands.Resources.csproj src/ResourceManager/Resources/Commands.Resources/Models.Authorization/AuthorizationClient.cs src/ResourceManager/Resources/Commands.Resources/Properties/Resources.resx src/ResourceManager/Resources/Commands.Resources/packages.config src/ResourceManager/StreamAnalytics/Commands.StreamAnalytics.Test/Commands.StreamAnalytics.Test.csproj src/ResourceManager/StreamAnalytics/Commands.StreamAnalytics.Test/packages.config src/ResourceManager/Websites/Commands.Websites.Test/Commands.Websites.Test.csproj src/ResourceManager/Websites/Commands.Websites.Test/packages.config src/ResourceManager/Websites/Commands.Websites/Commands.Websites.csproj src/ResourceManager/Websites/Commands.Websites/packages.config
2 parents 27d563c + 782922e commit 4d135b5

File tree

25 files changed

+382
-25
lines changed

25 files changed

+382
-25
lines changed

src/ResourceManager/Batch/Commands.Batch.Test/Commands.Batch.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.2-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
6161
</Reference>
6262
<Reference Include="Microsoft.Azure.Management.Authorization">
63-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
63+
<SpecificVersion>False</SpecificVersion>
64+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6465
</Reference>
6566
<Reference Include="Microsoft.Azure.Management.Batch">
6667
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Batch.1.3.0-preview\lib\net40\Microsoft.Azure.Management.Batch.dll</HintPath>

src/ResourceManager/Batch/Commands.Batch.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<package id="Microsoft.Azure.Common.Authentication" version="1.0.22-preview" targetFramework="net45" />
77
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
88
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
9-
<package id="Microsoft.Azure.Management.Authorization" version="0.18.0-preview" targetFramework="net45" />
9+
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Batch" version="1.3.0-preview" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Test.Framework" version="1.0.5571.32271-prerelease" targetFramework="net45" />

src/ResourceManager/DataFactories/Commands.DataFactories.Test/Commands.DataFactories.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</Reference>
6161
<Reference Include="Microsoft.Azure.Management.Authorization">
6262
<SpecificVersion>False</SpecificVersion>
63-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
63+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6464
</Reference>
6565
<Reference Include="Microsoft.Azure.Management.DataFactories">
6666
<HintPath>..\..\..\packages\Microsoft.Azure.Management.DataFactories.0.15.6-preview\lib\net40\Microsoft.Azure.Management.DataFactories.dll</HintPath>

src/ResourceManager/DataFactories/Commands.DataFactories.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<package id="Microsoft.Azure.Common.Authentication" version="1.0.22-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
8-
<package id="Microsoft.Azure.Management.Authorization" version="0.18.0-preview" targetFramework="net45" />
8+
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.DataFactories" version="0.15.6-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Test.Framework" version="1.0.5571.32271-prerelease" targetFramework="net45" />

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
6565
</Reference>
6666
<Reference Include="Microsoft.Azure.Management.Authorization">
67-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
67+
<SpecificVersion>False</SpecificVersion>
68+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6869
</Reference>
6970
<Reference Include="Microsoft.Azure.ResourceManager">
7071
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -173,6 +174,7 @@
173174
<Compile Include="ScenarioTests\ResourceGroupTests.cs" />
174175
<Compile Include="ScenarioTests\ResourceTests.cs" />
175176
<Compile Include="ScenarioTests\RoleAssignmentTests.cs" />
177+
<Compile Include="ScenarioTests\RoleDefinitionTests.cs" />
176178
<Compile Include="Templates\TestAzureResourceGroupTemplateCommandTests.cs" />
177179
<Compile Include="Templates\SaveAzureResourceGroupGalleryTemplateCommandTests.cs" />
178180
<Compile Include="Templates\GetAzureResourceGroupGalleryTemplateCommandTests.cs" />
@@ -245,6 +247,9 @@
245247
<Content Include="ScenarioTests\AuthorizationTests.ps1">
246248
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
247249
</Content>
250+
<None Include="Resources\NewRoleDefinition.json">
251+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
252+
</None>
248253
<None Include="ScenarioTests\ActiveDirectoryTests.ps1">
249254
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
250255
</None>
@@ -266,6 +271,9 @@
266271
<None Include="ScenarioTests\RoleAssignmentTests.ps1">
267272
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
268273
</None>
274+
<None Include="ScenarioTests\RoleDefinitionTests.ps1">
275+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
276+
</None>
269277
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestGetADUserWithMail.json">
270278
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
271279
</None>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Name": "CustomRole Test Role",
3+
"Description": "Test role",
4+
"Actions": [
5+
"Microsoft.Authorization/*/read",
6+
"Microsoft.Support/*"
7+
],
8+
"NotActions": [],
9+
"AssignableScopes": ["Scope1" , "Scope2"]
10+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
19+
namespace Microsoft.Azure.Commands.Resources.Test.ScenarioTests
20+
{
21+
public class RoleDefinitionTests
22+
{
23+
// TODO: Add [Trait(Category.AcceptanceType, Category.CheckIn)] attribute for each test once it gets implemented fully
24+
25+
[Fact(Skip = "Not implemented")]
26+
public void RoleDefinitionCreateTests()
27+
{
28+
ResourcesController.NewInstance.RunPsTest("Test-RoleDefinitionCreateTests");
29+
}
30+
31+
[Fact(Skip="Not implemented")]
32+
public void RdNegativeScenarios()
33+
{
34+
ResourcesController.NewInstance.RunPsTest("Test-RdNegativeScenarios");
35+
}
36+
37+
[Fact(Skip = "Not implemented")]
38+
public void RdPositiveScenarios()
39+
{
40+
ResourcesController.NewInstance.RunPsTest("Test-RDPositiveScenarios");
41+
}
42+
}
43+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
<#
16+
.SYNOPSIS
17+
Tests verify scenarios for RoleDefinitions creation.
18+
#>
19+
function Test-RoleDefinitionCreateTests
20+
{
21+
# Basic positive case - read from file
22+
$rdName = 'CustomRole Test Role'
23+
New-AzureRoleDefinition -InputFile .\Resources\NewRoleDefinition.json
24+
$rd = Get-AzureRoleDefinition -Name $rdName
25+
Assert-NotNull $rd
26+
Assert-AreEqual "Test role" $rd.Description
27+
Assert-AreEqual $true $rd.IsCustom
28+
Assert-NotNull $rd.Actions
29+
Assert-AreEqual "Microsoft.Authorization/*/read" $rd.Actions[0]
30+
Assert-AreEqual "Microsoft.Support/*" $rd.Actions[1]
31+
Assert-NotNull $rd.AssignableScopes
32+
# The below scopes may need to be changed to actual scope values like /subscriptions/.... to satisfy the ARM access checks for PUT requests
33+
Assert-AreEqual "Scope1" $rd.AssignableScopes[0]
34+
Assert-AreEqual "Scope2" $rd.AssignableScopes[1]
35+
36+
# Basic positive case - read from object
37+
$roleDef = Get-AzureRoleDefinition -Name "Virtual Machine Contributor"
38+
$roleDef.Id = $null
39+
$roleDef.Name = "Virtual machine restarter"
40+
$roleDef.Actions.Add("Microsoft.ClassicCompute/virtualMachines/restart/action")
41+
$roleDef.Description = "Can monitor and restart virtual machines"
42+
43+
New-AzureRoleDefinition -Role $roleDef
44+
$addedRoleDef = Get-AzureRoleDefinition -Name "Virtual machine restarter"
45+
46+
Assert-AreEqual $roleDef.Actions $addedRoleDef.Actions
47+
Assert-AreEqual $roleDef.Description $addedRoleDef.Description
48+
Assert-AreEqual $roleDef.AssignableScopes $addedRoleDef.AssignableScopes
49+
Assert-AreEqual $true $roleDef.IsCustom
50+
}
51+
52+
<#
53+
.SYNOPSIS
54+
Tests verify negative scenarios for RoleDefinitions
55+
#>
56+
function Test-RdNegativeScenarios
57+
{
58+
# Does not throw when getting a non-existing role assignment
59+
$rdName = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
60+
$badRdNameResult = Get-AzureRoleDefinition -Name $rdName
61+
Assert-Null $badRdNameResult
62+
63+
$rdId = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
64+
$badIdException = "Can not find role definition with id " + $rdId + "."
65+
66+
# Throws on trying to update the a role that does not exist
67+
Assert-Throws { Set-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json } $badIdException
68+
69+
# Get a null role definition
70+
$rdName = 'nonExisting role'
71+
$rdNull = Get-AzureRoleDefinition -Name $rdName
72+
73+
# Create a role definition
74+
$rd = New-AzureRoleDefinition -InputFile .Resources\RoleDefinition.json
75+
76+
# Role Defintion not provided.
77+
$roleDefNotProvided = "Role definition not provided."
78+
Assert-Throws { Set-AzureRoleDefinition } $roleDefNotProvided
79+
Assert-Throws { Set-AzureRoleDefinition -InputFile "" } $roleDefNotProvided
80+
Assert-Throws { Set-AzureRoleDefinition -Role $rdNull } $roleDefNotProvided
81+
Assert-Throws { Set-AzureRoleDefinition -InputFile "" -Role $rdNull } $roleDefNotProvided
82+
83+
# Provide role definition using either InputFile or Role, not both.
84+
$doNotProvideBothParams = "Provide role definition using either InputFile or Role, not both."
85+
Assert-Throws { Set-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json -Role $rd } $doNotProvideBothParams
86+
87+
# Throws on trying to delete a role that does not exist
88+
Assert-Throws { Remove-AzureRoleDefinition -Id $rdId -Force} $badIdException
89+
}
90+
91+
<#
92+
.SYNOPSIS
93+
Tests verify positive scenarios for RoleDefinitions.
94+
#>
95+
function Test-RDPositiveScenarios
96+
{
97+
# Create a role definition with Name rdNamme.
98+
$rdName = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
99+
$rd = New-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json
100+
$rd = Get-AzureRoleDefinition -Name $rdName
101+
102+
# Update the role definition with name $rdName that was created in the step above.
103+
$newActions = {'Microsoft.Authorization/*/read'}
104+
$rd.Actions = $newActions
105+
$updatedRd = Set-AzureRoleDefinition -RoleDefinition $rd
106+
107+
Assert-AreEqual $rd.Name $updatedRd.Name
108+
Assert-AreEqual $newActions $updatedRd.Actions
109+
110+
# delete the role definition
111+
$deletedRd = Remove-AzureRoleDefinition -Id $rd.Id -Force
112+
Assert-AreEqual $rd.Name $deletedRd.Name
113+
114+
# try to read the deleted role definition
115+
$readRd = Get-AzureRoleDefinition -Name $rd.Name
116+
Assert-Null $readRd
117+
118+
$rdReCreated = New-AzureRoleDefinition -Role $rd
119+
$rdReDeleted = Get-AzureRoleDefinition -Name $rd.Name | Remove-AzureRoleDefinition -Force
120+
}

src/ResourceManager/Resources/Commands.Resources.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
9-
<package id="Microsoft.Azure.Management.Authorization" version="0.18.0-preview" targetFramework="net45" />
9+
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Test.Framework" version="1.0.5571.32271-prerelease" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5571.32271-prerelease" targetFramework="net45" />

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
<SpecificVersion>False</SpecificVersion>
6969
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
7070
</Reference>
71-
<Reference Include="Microsoft.Azure.Management.Authorization">
72-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
71+
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
72+
<SpecificVersion>False</SpecificVersion>
73+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
7374
</Reference>
7475
<Reference Include="Microsoft.Azure.ResourceManager">
7576
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -132,6 +133,11 @@
132133
<Compile Include="ActiveDirectory\RemoveAzureADServicePrincipalCommand.cs" />
133134
<Compile Include="ActiveDirectory\NewAzureADServicePrincipalCommand.cs" />
134135
<Compile Include="Models.ResourceGroups\PSResourceProviderOperation.cs" />
136+
<Compile Include="Properties\Resources.Designer.cs">
137+
<AutoGen>True</AutoGen>
138+
<DesignTime>True</DesignTime>
139+
<DependentUpon>Resources.resx</DependentUpon>
140+
</Compile>
135141
<Compile Include="ProviderFeatures\AzureProviderFeatureCmdletsBase.cs" />
136142
<Compile Include="ProviderFeatures\GetAzureProviderFeatureCmdlet.cs" />
137143
<Compile Include="Models.ActiveDirectory\ActiveDirectoryBaseCmdlet.cs" />
@@ -198,6 +204,7 @@
198204
<Compile Include="RoleAssignments\GetAzureRoleAssignmentCommand.cs" />
199205
<Compile Include="RoleAssignments\NewAzureRoleAssignmentCommand.cs" />
200206
<Compile Include="RoleDefinitions\GetAzureRoleDefinitionCommand.cs" />
207+
<Compile Include="RoleDefinitions\NewAzureRoleDefinitionCommand.cs" />
201208
<Compile Include="Templates\TestAzureResourceGroupTemplateCommand.cs" />
202209
<Compile Include="Templates\SaveAzureResourceGroupGalleryTemplateCommand.cs" />
203210
<Compile Include="Templates\GetAzureResourceGroupGalleryTemplateCommand.cs" />
@@ -213,11 +220,6 @@
213220
<Compile Include="Models.ResourceGroups\SetResourceMode.cs" />
214221
<Compile Include="Models.ResourceGroups\TemplateFile.cs" />
215222
<Compile Include="Models.ResourceGroups\TemplateFileParameterV1.cs" />
216-
<Compile Include="Properties\Resources.Designer.cs">
217-
<AutoGen>True</AutoGen>
218-
<DesignTime>True</DesignTime>
219-
<DependentUpon>Resources.resx</DependentUpon>
220-
</Compile>
221223
<Compile Include="Models.ResourceGroups\ResourceClient.ResourceManager.cs" />
222224
<Compile Include="Properties\AssemblyInfo.cs" />
223225
<Compile Include="ResourceGroupDeployments\StopAzureResourceGroupDeploymentCommand.cs" />
@@ -266,8 +268,8 @@
266268
<ItemGroup>
267269
<EmbeddedResource Include="Properties\Resources.resx">
268270
<Generator>ResXFileCodeGenerator</Generator>
269-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
270271
<SubType>Designer</SubType>
272+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
271273
</EmbeddedResource>
272274
</ItemGroup>
273275
<ItemGroup />

src/ResourceManager/Resources/Commands.Resources/Models.ActiveDirectory/ParameterSet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ internal static class ParameterSet
6767
public const string ApplicationWithKeyCredential = "ApplicationWithKeyCredentialParameterSet";
6868

6969
public const string Empty = "EmptyParameterSet";
70+
71+
public const string InputFile = "InputFileParameterSet";
72+
73+
public const string RoleDefinition = "RoleDefinitionParameterSet";
7074
}
7175
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,54 @@ public PSRoleDefinition GetRoleRoleDefinition(string name)
178178

179179
return role;
180180
}
181+
182+
public PSRoleDefinition CreateRoleDefinition(PSRoleDefinition roleDefinition)
183+
{
184+
AuthorizationClient.ValidateRoleDefinition(roleDefinition);
185+
186+
Guid newRoleDefinitionId = Guid.NewGuid();
187+
RoleDefinitionCreateOrUpdateParameters parameters = new RoleDefinitionCreateOrUpdateParameters()
188+
{
189+
RoleDefinition = new RoleDefinition()
190+
{
191+
Name = newRoleDefinitionId,
192+
Properties = new RoleDefinitionProperties()
193+
{
194+
AssignableScopes = roleDefinition.AssignableScopes,
195+
Description = roleDefinition.Description,
196+
Permissions = new List<Permission>()
197+
{
198+
new Permission()
199+
{
200+
Actions = roleDefinition.Actions,
201+
NotActions = roleDefinition.NotActions
202+
}
203+
},
204+
RoleName = roleDefinition.Name,
205+
Type = "CustomRole"
206+
}
207+
}
208+
};
209+
210+
return AuthorizationManagementClient.RoleDefinitions.CreateOrUpdate(newRoleDefinitionId, parameters).RoleDefinition.ToPSRoleDefinition();
211+
}
212+
213+
private static void ValidateRoleDefinition(PSRoleDefinition roleDefinition)
214+
{
215+
if (string.IsNullOrWhiteSpace(roleDefinition.Name))
216+
{
217+
throw new ArgumentException(ProjectResources.InvalidRoleDefinitionName);
218+
}
219+
220+
if (roleDefinition.AssignableScopes == null || !roleDefinition.AssignableScopes.Any())
221+
{
222+
throw new ArgumentException(ProjectResources.InvalidAssignableScopes);
223+
}
224+
225+
if (roleDefinition.Actions == null || !roleDefinition.Actions.Any())
226+
{
227+
throw new ArgumentException(ProjectResources.InvalidActions);
228+
}
229+
}
181230
}
182231
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public static PSRoleDefinition ToPSRoleDefinition(this RoleDefinition role)
3232
Name = role.Properties.RoleName,
3333
Actions = new List<string>(role.Properties.Permissions.SelectMany(r => r.Actions)),
3434
NotActions = new List<string>(role.Properties.Permissions.SelectMany(r => r.NotActions)),
35-
Id = role.Id
35+
Id = role.Id,
36+
AssignableScopes = role.Properties.AssignableScopes.ToList(),
37+
Description = role.Properties.Description,
38+
IsCustom = role.Properties.Type == "CustomRole" ? true : false
3639
};
3740
}
3841

0 commit comments

Comments
 (0)