Skip to content

Commit 315c876

Browse files
committed
Merge branch 'build' of https://github.com/Azure/azure-powershell-pr into crp2
2 parents 32f15a7 + 782922e commit 315c876

File tree

24 files changed

+257
-32
lines changed

24 files changed

+257
-32
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.1-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.1-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.1-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.1-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: 3 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.1-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>
@@ -246,6 +247,7 @@
246247
<Content Include="ScenarioTests\AuthorizationTests.ps1">
247248
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
248249
</Content>
250+
<None Include="Resources\NewRoleDefinition.json" />
249251
<None Include="Resources\RoleDefinition.json" />
250252
<None Include="ScenarioTests\ActiveDirectoryTests.ps1">
251253
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
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+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ namespace Microsoft.Azure.Commands.Resources.Test.ScenarioTests
2020
{
2121
public class RoleDefinitionTests
2222
{
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+
2331
[Fact(Skip="Not implemented")]
24-
[Trait(Category.AcceptanceType, Category.CheckIn)]
2532
public void RdNegativeScenarios()
2633
{
2734
ResourcesController.NewInstance.RunPsTest("Test-RdNegativeScenarios");
2835
}
2936

3037
[Fact(Skip = "Not implemented")]
31-
[Trait(Category.AcceptanceType, Category.CheckIn)]
3238
public void RdPositiveScenarios()
3339
{
3440
ResourcesController.NewInstance.RunPsTest("Test-RDPositiveScenarios");

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
1414

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+
1552
<#
1653
.SYNOPSIS
1754
Tests verify negative scenarios for RoleDefinitions
@@ -34,7 +71,7 @@ function Test-RdNegativeScenarios
3471
$rdNull = Get-AzureRoleDefinition -Name $rdName
3572

3673
# Create a role definition
37-
# $rd = New-AzureRoleDefinition -InputFile .Resources\RoleDefinition.json
74+
$rd = New-AzureRoleDefinition -InputFile .Resources\RoleDefinition.json
3875

3976
# Role Defintion not provided.
4077
$roleDefNotProvided = "Role definition not provided."
@@ -59,7 +96,7 @@ function Test-RDPositiveScenarios
5996
{
6097
# Create a role definition with Name rdNamme.
6198
$rdName = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
62-
# $rd = New-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json
99+
$rd = New-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json
63100
$rd = Get-AzureRoleDefinition -Name $rdName
64101

65102
# Update the role definition with name $rdName that was created in the step above.
@@ -78,6 +115,6 @@ function Test-RDPositiveScenarios
78115
$readRd = Get-AzureRoleDefinition -Name $rd.Name
79116
Assert-Null $readRd
80117

81-
# $rdReCreated = New-AzureRoleDefinition -Role $rd
118+
$rdReCreated = New-AzureRoleDefinition -Role $rd
82119
$rdReDeleted = Get-AzureRoleDefinition -Name $rd.Name | Remove-AzureRoleDefinition -Force
83-
}
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.1-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.1-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="RoleDefinitions\RemoveAzureRoleDefinitionCommand.cs" />
202209
<Compile Include="RoleDefinitions\SetAzureRoleDefinitionCommand.cs" />
203210
<Compile Include="Templates\TestAzureResourceGroupTemplateCommand.cs" />
@@ -215,11 +222,6 @@
215222
<Compile Include="Models.ResourceGroups\SetResourceMode.cs" />
216223
<Compile Include="Models.ResourceGroups\TemplateFile.cs" />
217224
<Compile Include="Models.ResourceGroups\TemplateFileParameterV1.cs" />
218-
<Compile Include="Properties\Resources.Designer.cs">
219-
<AutoGen>True</AutoGen>
220-
<DesignTime>True</DesignTime>
221-
<DependentUpon>Resources.resx</DependentUpon>
222-
</Compile>
223225
<Compile Include="Models.ResourceGroups\ResourceClient.ResourceManager.cs" />
224226
<Compile Include="Properties\AssemblyInfo.cs" />
225227
<Compile Include="ResourceGroupDeployments\StopAzureResourceGroupDeploymentCommand.cs" />
@@ -268,8 +270,8 @@
268270
<ItemGroup>
269271
<EmbeddedResource Include="Properties\Resources.resx">
270272
<Generator>ResXFileCodeGenerator</Generator>
271-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
272273
<SubType>Designer</SubType>
274+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
273275
</EmbeddedResource>
274276
</ItemGroup>
275277
<ItemGroup />

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,55 @@ public PSRoleDefinition GetRoleRoleDefinition(string name)
198198
return role;
199199
}
200200

201+
public PSRoleDefinition CreateRoleDefinition(PSRoleDefinition roleDefinition)
202+
{
203+
AuthorizationClient.ValidateRoleDefinition(roleDefinition);
204+
205+
Guid newRoleDefinitionId = Guid.NewGuid();
206+
RoleDefinitionCreateOrUpdateParameters parameters = new RoleDefinitionCreateOrUpdateParameters()
207+
{
208+
RoleDefinition = new RoleDefinition()
209+
{
210+
Name = newRoleDefinitionId,
211+
Properties = new RoleDefinitionProperties()
212+
{
213+
AssignableScopes = roleDefinition.AssignableScopes,
214+
Description = roleDefinition.Description,
215+
Permissions = new List<Permission>()
216+
{
217+
new Permission()
218+
{
219+
Actions = roleDefinition.Actions,
220+
NotActions = roleDefinition.NotActions
221+
}
222+
},
223+
RoleName = roleDefinition.Name,
224+
Type = "CustomRole"
225+
}
226+
}
227+
};
228+
229+
return AuthorizationManagementClient.RoleDefinitions.CreateOrUpdate(newRoleDefinitionId, parameters).RoleDefinition.ToPSRoleDefinition();
230+
}
231+
232+
private static void ValidateRoleDefinition(PSRoleDefinition roleDefinition)
233+
{
234+
if (string.IsNullOrWhiteSpace(roleDefinition.Name))
235+
{
236+
throw new ArgumentException(ProjectResources.InvalidRoleDefinitionName);
237+
}
238+
239+
if (roleDefinition.AssignableScopes == null || !roleDefinition.AssignableScopes.Any())
240+
{
241+
throw new ArgumentException(ProjectResources.InvalidAssignableScopes);
242+
}
243+
244+
if (roleDefinition.Actions == null || !roleDefinition.Actions.Any())
245+
{
246+
throw new ArgumentException(ProjectResources.InvalidActions);
247+
}
248+
}
249+
201250
/// <summary>
202251
/// Updates a role definiton.
203252
/// </summary>
@@ -214,6 +263,8 @@ public PSRoleDefinition UpdateRoleDefinition(PSRoleDefinition role)
214263
roleDefinition.Name = role.Name ?? roleDefinition.Name;
215264
roleDefinition.Actions = role.Actions ?? roleDefinition.Actions;
216265
roleDefinition.NotActions = role.NotActions ?? roleDefinition.NotActions;
266+
roleDefinition.AssignableScopes = role.AssignableScopes ?? roleDefinition.AssignableScopes;
267+
roleDefinition.Description = role.Description ?? roleDefinition.Description;
217268

218269
// TODO: confirm with ARM on what exception will be thrown when the last segment of the roleDefinition's ID is not a GUID.
219270
// This will be done after their API is designed.
@@ -242,7 +293,9 @@ public PSRoleDefinition UpdateRoleDefinition(PSRoleDefinition role)
242293
Actions = roleDefinition.Actions,
243294
NotActions = roleDefinition.NotActions
244295
}
245-
}
296+
},
297+
AssignableScopes = roleDefinition.AssignableScopes,
298+
Description = roleDefinition.Description
246299
}
247300
}
248301
}).RoleDefinition.ToPSRoleDefinition();

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ public class PSRoleDefinition
2222

2323
public string Id { get; set; }
2424

25+
public bool IsCustom { get; set; }
26+
27+
public string Description { get; set; }
28+
2529
public List<string> Actions { get; set; }
2630

2731
public List<string> NotActions { get; set; }
32+
33+
public List<string> AssignableScopes { get; set; }
2834
}
2935
}

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

Lines changed: 27 additions & 0 deletions
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,19 @@
282282
<data name="RegisterProviderFeatureMessage" xml:space="preserve">
283283
<value>Registering provider feature ...</value>
284284
</data>
285+
<data name="InvalidRoleDefinitionName" xml:space="preserve">
286+
<value>RoleDefinition Name is invalid</value>
287+
</data>
285288
<data name="RoleDefinitionWithIdNotFound" xml:space="preserve">
286289
<value>Cannot find role definition with id '{0}'.</value>
287290
</data>
288291
<data name="RemoveRoleDefinition" xml:space="preserve">
289292
<value>Are you sure you want to remove role definition with id '{0}'.</value>
290293
</data>
294+
<data name="InvalidActions" xml:space="preserve">
295+
<value>Invalid value for Actions</value>
296+
</data>
297+
<data name="InvalidAssignableScopes" xml:space="preserve">
298+
<value>Invalid value for AssignableScopes</value>
299+
</data>
291300
</root>

0 commit comments

Comments
 (0)