-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* save * add roleassignment * support role assignment * revert sln change * updat tests * fix * pr fb * regen * Add back env var support for subs, PR fb * regen * fix tests * regen
- Loading branch information
1 parent
983d180
commit 3a83b56
Showing
41 changed files
with
569 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
sdk/provisioning/Azure.Provisioning/src/authorization/AuthorizationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Azure.Provisioning.Authorization | ||
{ | ||
/// <summary> | ||
/// Extension methods for authorization. | ||
/// </summary> | ||
public static class AuthorizationExtensions | ||
{ | ||
/// <summary> | ||
/// Assigns a role to the resource. | ||
/// </summary> | ||
/// <param name="resource">The resource.</param> | ||
/// <param name="roleDefinition">The role definition.</param> | ||
/// <param name="principalId">The principal ID.</param> | ||
public static RoleAssignment AssignRole(this Resource resource, RoleDefinition roleDefinition, Guid? principalId = default) | ||
{ | ||
return new RoleAssignment(resource, roleDefinition, principalId); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
sdk/provisioning/Azure.Provisioning/src/authorization/RoleAssignment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Core; | ||
using Azure.ResourceManager.Authorization; | ||
using Azure.ResourceManager.Authorization.Models; | ||
|
||
namespace Azure.Provisioning.Authorization | ||
{ | ||
/// <summary> | ||
/// Role assignment resource. | ||
/// </summary> | ||
public class RoleAssignment : Resource<RoleAssignmentData> | ||
{ | ||
private static readonly ResourceType ResourceType = "Microsoft.Resources/roleAssignments"; | ||
private static readonly ResourceType RoleDefinitionResourceType = "Microsoft.Authorization/roleDefinitions"; | ||
|
||
private const string SubscriptionResourceIdFunction = "subscriptionResourceId"; | ||
|
||
internal RoleAssignment( | ||
Resource resource, | ||
RoleDefinition roleDefinition, | ||
Guid? principalId = default) | ||
: base( | ||
resource.Scope, | ||
resource, | ||
resource.Name, | ||
ResourceType, | ||
"2022-04-01", | ||
(name) => ArmAuthorizationModelFactory.RoleAssignmentData( | ||
name: name, | ||
principalId: principalId)) | ||
{ | ||
if (resource.Scope.Configuration?.UseInteractiveMode != true && principalId == null) | ||
{ | ||
throw new InvalidOperationException("PrincipalId must be specified when not in interactive mode."); | ||
} | ||
|
||
if (principalId == null) | ||
{ | ||
AssignParameter(data => data.PrincipalId, new Parameter("principalId")); | ||
} | ||
|
||
AssignProperty( | ||
data => data.Name, | ||
$"guid('{resource.Name}', {(principalId == null ? "principalId" : "'" + principalId + "'")}," + | ||
$" {SubscriptionResourceIdFunction}({(resource.Scope.Configuration?.UseInteractiveMode != true ? "'" + Id.SubscriptionId + "', ": string.Empty)}" + | ||
$"'{RoleDefinitionResourceType}', '{roleDefinition}'))"); | ||
|
||
AssignProperty( | ||
data => data.RoleDefinitionId, | ||
$"{SubscriptionResourceIdFunction}({(resource.Scope.Configuration?.UseInteractiveMode != true ? "'"+ Id.SubscriptionId + "', ": string.Empty)}" + | ||
$"'{RoleDefinitionResourceType}', '{roleDefinition}')"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override bool NeedsScope() => true; | ||
|
||
/// <inheritdoc /> | ||
protected override bool NeedsParent() => false; | ||
} | ||
} |
Oops, something went wrong.