Skip to content

Commit a969d47

Browse files
committed
Merge pull request Azure#907 from vivsriaus/Policy
Add policy definition cmdlets
2 parents 6fe39fd + 9a9a1c6 commit a969d47

File tree

10 files changed

+733
-0
lines changed

10 files changed

+733
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
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" />
115+
<Compile Include="Entities\Policy\PolicyDefinitionProperties.cs" />
116+
<Compile Include="Entities\Policy\PolicyRule.cs" />
114117
<Compile Include="Entities\Providers\ResourceProviderDefinition.cs" />
115118
<Compile Include="Entities\Providers\ResourceTypeDefinition.cs" />
116119
<Compile Include="Entities\ResourceGroup\ResourceBatchMoveParameters.cs" />
@@ -136,6 +139,11 @@
136139
<Compile Include="Implementation\InvokeAzureResourceActionCmdlet.cs" />
137140
<Compile Include="Implementation\MoveAzureResourceCmdlet.cs" />
138141
<Compile Include="Implementation\NewAzureResourceLockCmdlet.cs" />
142+
<Compile Include="Implementation\Policy\GetAzurePolicyDefinition.cs" />
143+
<Compile Include="Implementation\Policy\NewAzurePolicyDefinition.cs" />
144+
<Compile Include="Implementation\Policy\PolicyDefinitionCmdletBase.cs" />
145+
<Compile Include="Implementation\Policy\RemoveAzurePolicyDefinition.cs" />
146+
<Compile Include="Implementation\Policy\SetAzurePolicyDefinition.cs" />
139147
<Compile Include="Implementation\RemoveAzureResourceCmdlet.cs" />
140148
<Compile Include="Implementation\RemoveAzureResourceLockCmdlet.cs" />
141149
<Compile Include="Implementation\ResourceLockManagementCmdletBase.cs" />

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public static class Constants
2424
/// </summary>
2525
public static readonly string MicrosoftResourceNamesapce = "Microsoft.Resources";
2626

27+
/// <summary>
28+
/// The <c>Microsoft.Authorization</c> namespace.
29+
/// </summary>
30+
public static readonly string MicrosoftAuthorizationNamespace = "Microsoft.Authorization";
31+
2732
/// <summary>
2833
/// The string literal <c>ResourceGroups</c>
2934
/// </summary>
@@ -64,6 +69,11 @@ public static class Constants
6469
/// </summary>
6570
public static readonly string MicrosoftResourcesDeploymentOperationsType = Constants.MicrosoftResourceNamesapce + "/deployments/operations";
6671

72+
/// <summary>
73+
/// The policy definition resource type.
74+
/// </summary>
75+
public static readonly string MicrosoftAuthorizationPolicyDefinitionType = Constants.MicrosoftAuthorizationNamespace + "/policydefinitions";
76+
6777
/// <summary>
6878
/// The type name of the generic resource.
6979
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 definition object.
21+
/// </summary>
22+
public class PolicyDefinition
23+
{
24+
/// <summary>
25+
/// The policy definition properties.
26+
/// </summary>
27+
[JsonProperty(Required = Required.Default)]
28+
public PolicyDefinitionProperties Properties { get; set; }
29+
}
30+
}
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 definition properties.
21+
/// </summary>
22+
public class PolicyDefinitionProperties
23+
{
24+
/// <summary>
25+
/// The description.
26+
/// </summary>
27+
[JsonProperty(Required = Required.Default)]
28+
public string Description { 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 rule.
38+
/// </summary>
39+
[JsonProperty(Required = Required.Always)]
40+
public PolicyRule PolicyRule { get; set; }
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 rule object.
21+
/// </summary>
22+
public class PolicyRule
23+
{
24+
/// <summary>
25+
/// The policy rule
26+
/// </summary>
27+
[JsonProperty(Required = Required.Always)]
28+
public string Rule { get; set; }
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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 definition.
25+
/// </summary>
26+
[Cmdlet(VerbsCommon.Get, "AzureRMPolicyDefinition", DefaultParameterSetName = GetAzurePolicyDefinitionCmdlet.ParameterlessSet), OutputType(typeof(PSObject))]
27+
public class GetAzurePolicyDefinitionCmdlet : PolicyDefinitionCmdletBase
28+
{
29+
/// <summary>
30+
/// The policy Id parameter set.
31+
/// </summary>
32+
internal const string PolicyDefinitionIdParameterSet = "The policy definition Id parameter set.";
33+
34+
/// <summary>
35+
/// The policy name parameter set.
36+
/// </summary>
37+
internal const string PolicyDefinitionNameParameterSet = "The policy definition name parameter set.";
38+
39+
/// <summary>
40+
/// The list all policy parameter set.
41+
/// </summary>
42+
internal const string ParameterlessSet = "The list all policy definitions parameter set.";
43+
44+
/// <summary>
45+
/// Gets or sets the policy definition name parameter.
46+
/// </summary>
47+
[Parameter(ParameterSetName = GetAzurePolicyDefinitionCmdlet.PolicyDefinitionNameParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The policy definition name.")]
48+
[ValidateNotNullOrEmpty]
49+
public string Name { get; set; }
50+
51+
/// <summary>
52+
/// Gets or sets the policy definition id parameter
53+
/// </summary>
54+
[Alias("ResourceId")]
55+
[Parameter(ParameterSetName = GetAzurePolicyDefinitionCmdlet.PolicyDefinitionIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The fully qualified policy definition Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}")]
56+
[ValidateNotNullOrEmpty]
57+
public string Id { get; set; }
58+
59+
/// <summary>
60+
/// Executes the cmdlet.
61+
/// </summary>
62+
protected override void OnProcessRecord()
63+
{
64+
base.OnProcessRecord();
65+
66+
this.RunCmdlet();
67+
}
68+
69+
/// <summary>
70+
/// Contains the cmdlet's execution logic.
71+
/// </summary>
72+
private void RunCmdlet()
73+
{
74+
PaginatedResponseHelper.ForEach(
75+
getFirstPage: () => this.GetResources(),
76+
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
77+
cancellationToken: this.CancellationToken,
78+
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects(resources), enumerateCollection: true));
79+
}
80+
81+
/// <summary>
82+
/// Queries the ARM cache and returns the cached resource that match the query specified.
83+
/// </summary>
84+
private async Task<ResponseWithContinuation<JObject[]>> GetResources()
85+
{
86+
string resourceId = this.Id ?? this.GetResourceId();
87+
88+
var apiVersion = await this
89+
.DetermineApiVersion(resourceId: resourceId)
90+
.ConfigureAwait(continueOnCapturedContext: false);
91+
92+
if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceGroupName(resourceId)))
93+
{
94+
var resource = await this
95+
.GetResourcesClient()
96+
.GetResource<JObject>(
97+
resourceId: resourceId,
98+
apiVersion: apiVersion,
99+
cancellationToken: this.CancellationToken.Value)
100+
.ConfigureAwait(continueOnCapturedContext: false);
101+
ResponseWithContinuation<JObject[]> retVal;
102+
return resource.TryConvertTo(out retVal) && retVal.Value != null
103+
? retVal
104+
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
105+
}
106+
else
107+
{
108+
return await this
109+
.GetResourcesClient()
110+
.ListObjectColleciton<JObject>(
111+
resourceCollectionId: resourceId,
112+
apiVersion: apiVersion,
113+
cancellationToken: this.CancellationToken.Value)
114+
.ConfigureAwait(continueOnCapturedContext: false);
115+
}
116+
}
117+
118+
/// <summary>
119+
/// Gets the resource Id
120+
/// </summary>
121+
private string GetResourceId()
122+
{
123+
var subscriptionId = DefaultContext.Subscription.Id;
124+
if(string.IsNullOrEmpty(this.Name))
125+
{
126+
return string.Format("/subscriptions/{0}/providers/{1}",
127+
subscriptionId.ToString(),
128+
Constants.MicrosoftAuthorizationPolicyDefinitionType);
129+
}
130+
else
131+
{
132+
return string.Format("/subscriptions/{0}/providers/{1}/{2}",
133+
subscriptionId.ToString(),
134+
Constants.MicrosoftAuthorizationPolicyDefinitionType,
135+
this.Name);
136+
}
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)