Skip to content

Commit e43a1f0

Browse files
committed
Add policy assignment cmdlets
1 parent cc8f3d0 commit e43a1f0

13 files changed

+701
-33
lines changed

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Commands.ResourceManager.Cmdlets.csproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@
111111
<Compile Include="Entities\Locks\LockLevel.cs" />
112112
<Compile Include="Entities\Locks\LockProperties.cs" />
113113
<Compile Include="Entities\Operations\AzureAsyncOperationResource.cs" />
114-
<Compile Include="Entities\Policy\PolicyDefinition.cs" />
114+
<Compile Include="Entities\Policy\PolicyAssignment.cs" />
115+
<Compile Include="Entities\Policy\PolicyAssignmentProperties.cs" />
115116
<Compile Include="Entities\Policy\PolicyDefinitionProperties.cs" />
116-
<Compile Include="Entities\Policy\PolicyRule.cs" />
117+
<Compile Include="Entities\Policy\PolicyDefinition.cs" />
117118
<Compile Include="Entities\Providers\ResourceProviderDefinition.cs" />
118119
<Compile Include="Entities\Providers\ResourceTypeDefinition.cs" />
119120
<Compile Include="Entities\ResourceGroup\ResourceBatchMoveParameters.cs" />
@@ -139,10 +140,15 @@
139140
<Compile Include="Implementation\InvokeAzureResourceActionCmdlet.cs" />
140141
<Compile Include="Implementation\MoveAzureResourceCmdlet.cs" />
141142
<Compile Include="Implementation\NewAzureResourceLockCmdlet.cs" />
143+
<Compile Include="Implementation\Policy\GetAzurePolicyAssignment.cs" />
142144
<Compile Include="Implementation\Policy\GetAzurePolicyDefinition.cs" />
145+
<Compile Include="Implementation\Policy\NewAzurePolicyAssignment.cs" />
143146
<Compile Include="Implementation\Policy\NewAzurePolicyDefinition.cs" />
144-
<Compile Include="Implementation\Policy\PolicyDefinitionCmdletBase.cs" />
147+
<Compile Include="Implementation\Policy\PolicyAssignmentCmdletBase.cs" />
148+
<Compile Include="Implementation\Policy\RemoveAzurePolicyAssignment.cs" />
145149
<Compile Include="Implementation\Policy\RemoveAzurePolicyDefinition.cs" />
150+
<Compile Include="Implementation\Policy\PolicyDefinitionCmdletBase.cs" />
151+
<Compile Include="Implementation\Policy\SetAzurePolicyAssignment.cs" />
146152
<Compile Include="Implementation\Policy\SetAzurePolicyDefinition.cs" />
147153
<Compile Include="Implementation\RemoveAzureResourceCmdlet.cs" />
148154
<Compile Include="Implementation\RemoveAzureResourceLockCmdlet.cs" />

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Components/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public static class Constants
7474
/// </summary>
7575
public static readonly string MicrosoftAuthorizationPolicyDefinitionType = Constants.MicrosoftAuthorizationNamespace + "/policydefinitions";
7676

77+
/// <summary>
78+
/// The policy assignment resource type.
79+
/// </summary>
80+
public static readonly string MicrosoftAuthorizationPolicyAssignmentType = Constants.MicrosoftAuthorizationNamespace + "/policyassignments";
81+
7782
/// <summary>
7883
/// The type name of the generic resource.
7984
/// </summary>

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Entities/Policy/PolicyRule.cs renamed to src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Entities/Policy/PolicyAssignment.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
1717
using Newtonsoft.Json;
1818

1919
/// <summary>
20-
/// The policy rule object.
20+
/// The policy assignment object.
2121
/// </summary>
22-
public class PolicyRule
22+
public class PolicyAssignment
2323
{
2424
/// <summary>
25-
/// The policy rule
25+
/// The policy assignment properties.
2626
/// </summary>
27-
[JsonProperty(Required = Required.Always)]
28-
public string Rule { get; set; }
27+
[JsonProperty(Required = Required.Default)]
28+
public PolicyAssignmentProperties Properties { get; set; }
2929
}
3030
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
16+
{
17+
using Newtonsoft.Json;
18+
19+
/// <summary>
20+
/// The policy assignment properties.
21+
/// </summary>
22+
public class PolicyAssignmentProperties
23+
{
24+
/// <summary>
25+
/// The scope.
26+
/// </summary>
27+
[JsonProperty(Required = Required.Always)]
28+
public string Scope { get; set; }
29+
30+
/// <summary>
31+
/// The display name.
32+
/// </summary>
33+
[JsonProperty(Required = Required.Default)]
34+
public string DisplayName { get; set; }
35+
36+
/// <summary>
37+
/// The policy definition id.
38+
/// </summary>
39+
[JsonProperty(Required = Required.Always)]
40+
public string PolicyDefinitionId { get; set; }
41+
}
42+
}

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Entities/Policy/PolicyDefinitionProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public class PolicyDefinitionProperties
3737
/// The policy rule.
3838
/// </summary>
3939
[JsonProperty(Required = Required.Always)]
40-
public PolicyRule PolicyRule { get; set; }
40+
public string PolicyRule { get; set; }
4141
}
4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
16+
{
17+
using System.Management.Automation;
18+
using System.Threading.Tasks;
19+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
20+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
21+
using Newtonsoft.Json.Linq;
22+
23+
/// <summary>
24+
/// Gets the policy assignment.
25+
/// </summary>
26+
[Cmdlet(VerbsCommon.Get, "AzureRMPolicyAssignment", DefaultParameterSetName = GetAzurePolicyAssignmentCmdlet.ParameterlessSet), OutputType(typeof(PSObject))]
27+
public class GetAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
28+
{
29+
/// <summary>
30+
/// The policy Id parameter set.
31+
/// </summary>
32+
internal const string PolicyAssignmentIdParameterSet = "The policy assignment Id parameter set.";
33+
34+
/// <summary>
35+
/// The policy name parameter set.
36+
/// </summary>
37+
internal const string PolicyAssignmentNameParameterSet = "The policy assignment name parameter set.";
38+
39+
/// <summary>
40+
/// The list all policy parameter set.
41+
/// </summary>
42+
internal const string ParameterlessSet = "The list all policy assignments parameter set.";
43+
44+
/// <summary>
45+
/// Gets or sets the policy assignment name parameter.
46+
/// </summary>
47+
[Parameter(ParameterSetName = GetAzurePolicyAssignmentCmdlet.PolicyAssignmentNameParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The policy assignment name.")]
48+
[ValidateNotNullOrEmpty]
49+
public string Name { get; set; }
50+
51+
/// <summary>
52+
/// Gets or sets the policy assignment scope parameter.
53+
/// </summary>
54+
[Parameter(ParameterSetName = GetAzurePolicyAssignmentCmdlet.PolicyAssignmentNameParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The policy assignment name.")]
55+
[ValidateNotNullOrEmpty]
56+
public string Scope { get; set; }
57+
58+
/// <summary>
59+
/// Gets or sets the policy assignment id parameter
60+
/// </summary>
61+
[Alias("ResourceId")]
62+
[Parameter(ParameterSetName = GetAzurePolicyAssignmentCmdlet.PolicyAssignmentIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The fully qualified policy assignment Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}")]
63+
[ValidateNotNullOrEmpty]
64+
public string Id { get; set; }
65+
66+
/// <summary>
67+
/// Gets or sets the policy assignment policy definition id parameter
68+
/// </summary>
69+
[Parameter(ParameterSetName = GetAzurePolicyAssignmentCmdlet.PolicyAssignmentIdParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The fully qualified policy assignment Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}")]
70+
[Parameter(ParameterSetName = GetAzurePolicyAssignmentCmdlet.PolicyAssignmentNameParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The fully qualified policy assignment Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}")]
71+
[ValidateNotNullOrEmpty]
72+
public string PolicyDefinitionId { get; set; }
73+
74+
/// <summary>
75+
/// Executes the cmdlet.
76+
/// </summary>
77+
protected override void OnProcessRecord()
78+
{
79+
base.OnProcessRecord();
80+
81+
this.RunCmdlet();
82+
}
83+
84+
/// <summary>
85+
/// Contains the cmdlet's execution logic.
86+
/// </summary>
87+
private void RunCmdlet()
88+
{
89+
PaginatedResponseHelper.ForEach(
90+
getFirstPage: () => this.GetResources(),
91+
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
92+
cancellationToken: this.CancellationToken,
93+
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects(resources), enumerateCollection: true));
94+
}
95+
96+
/// <summary>
97+
/// Queries the ARM cache and returns the cached resource that match the query specified.
98+
/// </summary>
99+
private async Task<ResponseWithContinuation<JObject[]>> GetResources()
100+
{
101+
string resourceId = this.Id ?? this.GetResourceId();
102+
103+
var apiVersion = await this
104+
.DetermineApiVersion(resourceId: resourceId)
105+
.ConfigureAwait(continueOnCapturedContext: false);
106+
107+
if (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
108+
{
109+
var resource = await this
110+
.GetResourcesClient()
111+
.GetResource<JObject>(
112+
resourceId: resourceId,
113+
apiVersion: apiVersion,
114+
cancellationToken: this.CancellationToken.Value,
115+
odataQuery: null)
116+
.ConfigureAwait(continueOnCapturedContext: false);
117+
ResponseWithContinuation<JObject[]> retVal;
118+
return resource.TryConvertTo(out retVal) && retVal.Value != null
119+
? retVal
120+
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
121+
}
122+
else if(!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(this.Name))//If only scope is given, list assignments call
123+
{
124+
string filter = "$filter=atScope()";
125+
return await this
126+
.GetResourcesClient()
127+
.ListObjectColleciton<JObject>(
128+
resourceCollectionId: resourceId,
129+
apiVersion: apiVersion,
130+
cancellationToken: this.CancellationToken.Value,
131+
odataQuery: filter)
132+
.ConfigureAwait(continueOnCapturedContext: false);
133+
}
134+
else
135+
{
136+
string filter = string.IsNullOrEmpty(this.PolicyDefinitionId)
137+
? null
138+
: string.Format("$filter=policydefinitionid eq '{0}'", this.PolicyDefinitionId);
139+
140+
return await this
141+
.GetResourcesClient()
142+
.ListObjectColleciton<JObject>(
143+
resourceCollectionId: resourceId,
144+
apiVersion: apiVersion,
145+
cancellationToken: this.CancellationToken.Value,
146+
odataQuery: filter)
147+
.ConfigureAwait(continueOnCapturedContext: false);
148+
}
149+
}
150+
151+
/// <summary>
152+
/// Gets the resource Id
153+
/// </summary>
154+
private string GetResourceId()
155+
{
156+
var subscriptionId = DefaultContext.Subscription.Id;
157+
if(string.IsNullOrEmpty(this.Name) && string.IsNullOrEmpty(this.Scope))
158+
{
159+
return string.Format("/subscriptions/{0}/providers/{1}",
160+
subscriptionId.ToString(),
161+
Constants.MicrosoftAuthorizationPolicyAssignmentType);
162+
}
163+
else if(string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
164+
{
165+
return ResourceIdUtility.GetResourceId(
166+
resourceId: this.Scope,
167+
extensionResourceType: Constants.MicrosoftAuthorizationPolicyAssignmentType,
168+
extensionResourceName: null);
169+
}
170+
return ResourceIdUtility.GetResourceId(
171+
resourceId: this.Scope,
172+
extensionResourceType: Constants.MicrosoftAuthorizationPolicyAssignmentType,
173+
extensionResourceName: this.Name);
174+
}
175+
}
176+
}

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/Policy/GetAzurePolicyDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
8989
.DetermineApiVersion(resourceId: resourceId)
9090
.ConfigureAwait(continueOnCapturedContext: false);
9191

92-
if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceGroupName(resourceId)))
92+
if (!string.IsNullOrEmpty(this.Name))
9393
{
9494
var resource = await this
9595
.GetResourcesClient()

0 commit comments

Comments
 (0)