Skip to content

Commit 885844e

Browse files
committed
Merge pull request #1014 from vivsriaus/Policy
Add policy assignments cmdlets
2 parents 77ee674 + 6764c6f commit 885844e

20 files changed

+2126
-28
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Commands.Resources.Rest.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
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\PolicyAssignment.cs" />
115+
<Compile Include="Entities\Policy\PolicyAssignmentProperties.cs" />
114116
<Compile Include="Entities\Policy\PolicyDefinition.cs" />
115117
<Compile Include="Entities\Policy\PolicyDefinitionProperties.cs" />
116118
<Compile Include="Entities\Policy\PolicyRule.cs" />
@@ -140,10 +142,15 @@
140142
<Compile Include="Implementation\InvokeAzureResourceActionCmdlet.cs" />
141143
<Compile Include="Implementation\MoveAzureResourceCmdlet.cs" />
142144
<Compile Include="Implementation\NewAzureResourceLockCmdlet.cs" />
145+
<Compile Include="Implementation\Policy\GetAzurePolicyAssignment.cs" />
143146
<Compile Include="Implementation\Policy\GetAzurePolicyDefinition.cs" />
147+
<Compile Include="Implementation\Policy\NewAzurePolicyAssignment.cs" />
144148
<Compile Include="Implementation\Policy\NewAzurePolicyDefinition.cs" />
149+
<Compile Include="Implementation\Policy\PolicyAssignmentCmdletBase.cs" />
145150
<Compile Include="Implementation\Policy\PolicyDefinitionCmdletBase.cs" />
151+
<Compile Include="Implementation\Policy\RemoveAzurePolicyAssignment.cs" />
146152
<Compile Include="Implementation\Policy\RemoveAzurePolicyDefinition.cs" />
153+
<Compile Include="Implementation\Policy\SetAzurePolicyAssignment.cs" />
147154
<Compile Include="Implementation\Policy\SetAzurePolicyDefinition.cs" />
148155
<Compile Include="Implementation\RemoveAzureResourceCmdlet.cs" />
149156
<Compile Include="Implementation\RemoveAzureResourceLockCmdlet.cs" />

src/ResourceManager/Resources/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 definition 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>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 object.
21+
/// </summary>
22+
public class PolicyAssignment
23+
{
24+
/// <summary>
25+
/// The policy assignment properties.
26+
/// </summary>
27+
[JsonProperty(Required = Required.Default)]
28+
public PolicyAssignmentProperties Properties { get; set; }
29+
30+
/// <summary>
31+
/// The policy assignment name.
32+
/// </summary>
33+
[JsonProperty(Required = Required.Default)]
34+
public string Name { get; set; }
35+
}
36+
}
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/Resources/Commands.ResourceManager/Cmdlets/Entities/Policy/PolicyDefinition.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
2121
/// </summary>
2222
public class PolicyDefinition
2323
{
24+
/// <summary>
25+
/// The policy definition name.
26+
/// </summary>
27+
[JsonProperty(Required = Required.Default)]
28+
public string Name { get; set; }
29+
2430
/// <summary>
2531
/// The policy definition properties.
2632
/// </summary>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
1616
{
1717
using Newtonsoft.Json;
18+
using Newtonsoft.Json.Linq;
1819

1920
/// <summary>
2021
/// The policy definition properties.
@@ -37,6 +38,6 @@ public class PolicyDefinitionProperties
3738
/// The policy rule.
3839
/// </summary>
3940
[JsonProperty(Required = Required.Always)]
40-
public PolicyRule PolicyRule { get; set; }
41+
public JObject PolicyRule { get; set; }
4142
}
4243
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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 (IsResourceGet(resourceId))
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 (IsScopeLevelList(resourceId))//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+
/// Returns true if it is scope level policy assignment list call
153+
/// </summary>
154+
private bool IsScopeLevelList(string resourceId)
155+
{
156+
return (!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(this.Name))
157+
|| (!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)));
158+
}
159+
160+
/// <summary>
161+
/// Returns true if it is a single policy assignment get
162+
/// </summary>
163+
/// <param name="resourceId"></param>
164+
private bool IsResourceGet(string resourceId)
165+
{
166+
return (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
167+
|| !string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId));
168+
}
169+
170+
/// <summary>
171+
/// Gets the resource Id
172+
/// </summary>
173+
private string GetResourceId()
174+
{
175+
var subscriptionId = DefaultContext.Subscription.Id;
176+
if(string.IsNullOrEmpty(this.Name) && string.IsNullOrEmpty(this.Scope))
177+
{
178+
return string.Format("/subscriptions/{0}/providers/{1}",
179+
subscriptionId.ToString(),
180+
Constants.MicrosoftAuthorizationPolicyAssignmentType);
181+
}
182+
else if(string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
183+
{
184+
return ResourceIdUtility.GetResourceId(
185+
resourceId: this.Scope,
186+
extensionResourceType: Constants.MicrosoftAuthorizationPolicyAssignmentType,
187+
extensionResourceName: null);
188+
}
189+
return ResourceIdUtility.GetResourceId(
190+
resourceId: this.Scope,
191+
extensionResourceType: Constants.MicrosoftAuthorizationPolicyAssignmentType,
192+
extensionResourceName: this.Name);
193+
}
194+
}
195+
}

src/ResourceManager/Resources/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(ResourceIdUtility.GetResourceName(resourceId)))
9393
{
9494
var resource = await this
9595
.GetResourcesClient()

0 commit comments

Comments
 (0)