Skip to content

Commit 5278d3e

Browse files
mentat9wyunchi-ms
andauthored
Change policy cmdlets to use strongly-typed objects (#11681)
* Breaking changes for policy cmdlets to support typed pipeline objects * Update help files to correspond to typed parameters * Add breaking change exceptions * Remove unneeded usings Update changelog Co-authored-by: Yunchi Wang <[email protected]>
1 parent c21b196 commit 5278d3e

38 files changed

+3319
-80
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@ public class PolicyDefinitionProperties
5757
/// </summary>
5858
[JsonProperty(Required = Required.Default)]
5959
public string Mode { get; set; }
60+
61+
/// <summary>
62+
/// The policy type.
63+
/// </summary>
64+
[JsonProperty(Required = Required.Default)]
65+
public PolicyType PolicyType { get; set; }
6066
}
6167
}

src/Resources/ResourceManager/Entities/Policy/PolicySetDefinitionProperties.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@ public class PolicySetDefinitionProperties
5757
/// </summary>
5858
[JsonProperty(Required = Required.Default)]
5959
public JArray PolicyDefinitionGroups { get; set; }
60+
61+
/// <summary>
62+
/// The policy type.
63+
/// </summary>
64+
[JsonProperty(Required = Required.Default)]
65+
public PolicyType PolicyType { get; set; }
6066
}
6167
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/// <summary>
18+
/// The policy assignment enforcement mode.
19+
/// </summary>
20+
public enum PolicyType
21+
{
22+
/// <summary>
23+
/// The policy policy [set] definition is custom
24+
/// </summary>
25+
Custom,
26+
27+
/// <summary>
28+
/// The policy policy [set] definition is built in
29+
/// </summary>
30+
BuiltIn,
31+
32+
/// <summary>
33+
/// The policy policy definition is static
34+
/// </summary>
35+
Static
36+
}
37+
}

src/Resources/ResourceManager/Extensions/JsonExtensions.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions
2323
using System.Collections.Generic;
2424
using System.Globalization;
2525
using System.IO;
26+
using System.Management.Automation;
2627

2728
/// <summary>
2829
/// <c>JSON</c> extensions
@@ -134,6 +135,50 @@ public static JToken ToJToken(this object obj)
134135
return null;
135136
}
136137

138+
if (obj is PSObject psObject)
139+
{
140+
var jObject = new JObject();
141+
if (psObject.BaseObject is object[] psArray)
142+
{
143+
var jArray = new JArray();
144+
foreach (var item in psArray)
145+
{
146+
jArray.Add(item.ToJToken());
147+
}
148+
149+
return jArray;
150+
}
151+
152+
foreach (var property in psObject.Properties)
153+
{
154+
jObject.Add(new JProperty(property.Name, property.Value.ToJToken()));
155+
}
156+
157+
return jObject;
158+
}
159+
160+
if (obj is PSMemberInfoCollection<PSPropertyInfo> psCollection)
161+
{
162+
var jObject = new JObject();
163+
foreach (var member in psCollection)
164+
{
165+
jObject.Add(new JProperty(member.Name, member.Value.ToJToken()));
166+
}
167+
168+
return jObject;
169+
}
170+
171+
if (obj is object[] objArray)
172+
{
173+
var jArray = new JArray();
174+
foreach (var item in objArray)
175+
{
176+
jArray.Add(item.ToJToken());
177+
}
178+
179+
return jArray;
180+
}
181+
137182
return JToken.FromObject(obj, JsonExtensions.JsonObjectTypeSerializer);
138183
}
139184

src/Resources/ResourceManager/Implementation/Policy/GetAzurePolicyAssignment.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1919
using Microsoft.Azure.Commands.ResourceManager.Common;
20-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2120

2221
using Newtonsoft.Json.Linq;
2322
using Policy;
@@ -27,8 +26,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2726
/// <summary>
2827
/// Gets the policy assignment.
2928
/// </summary>
30-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyAssignment")]
31-
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PSObject))]
29+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PsPolicyAssignment))]
3230
public class GetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
3331
{
3432
/// <summary>
@@ -87,7 +85,7 @@ private void RunCmdlet()
8785
getFirstPage: () => this.GetResources(),
8886
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
8987
cancellationToken: this.CancellationToken,
90-
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects("PolicyAssignmentId", resources), enumerateCollection: true));
88+
action: resources => this.WriteObject(sendToPipeline: this.GetOutputPolicyAssignments(resources), enumerateCollection: true));
9189
}
9290

9391
/// <summary>

src/Resources/ResourceManager/Implementation/Policy/GetAzurePolicyDefinition.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2020
using Microsoft.Azure.Commands.ResourceManager.Common;
21-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2221

2322
using Newtonsoft.Json.Linq;
2423
using Policy;
@@ -29,8 +28,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2928
/// <summary>
3029
/// Gets the policy definition.
3130
/// </summary>
32-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyDefinition")]
33-
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PSObject))]
31+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PsPolicyDefinition))]
3432
public class GetAzurePolicyDefinitionCmdlet : PolicyCmdletBase
3533
{
3634
/// <summary>
@@ -100,7 +98,7 @@ private void RunCmdlet()
10098
getFirstPage: () => this.GetResources(listFilter),
10199
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
102100
cancellationToken: this.CancellationToken,
103-
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputObjects("PolicyDefinitionId", listFilter, resources), enumerateCollection: true));
101+
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputPolicyDefinitions(listFilter, resources), enumerateCollection: true));
104102
}
105103

106104
/// <summary>

src/Resources/ResourceManager/Implementation/Policy/GetAzurePolicySetDefinition.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2020
using Microsoft.Azure.Commands.ResourceManager.Common;
21-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2221

2322
using Newtonsoft.Json.Linq;
2423
using Policy;
@@ -29,8 +28,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2928
/// <summary>
3029
/// Gets the policy set definition.
3130
/// </summary>
32-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicySetDefinition")]
33-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PSObject))]
31+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PsPolicySetDefinition))]
3432
public class GetAzurePolicySetDefinitionCmdlet : PolicyCmdletBase
3533
{
3634
/// <summary>
@@ -100,7 +98,7 @@ private void RunCmdlet()
10098
getFirstPage: () => this.GetResources(listFilter),
10199
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
102100
cancellationToken: this.CancellationToken,
103-
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputObjects("PolicySetDefinitionId", listFilter, resources), enumerateCollection: true));
101+
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputPolicySetDefinitions(listFilter, resources), enumerateCollection: true));
104102
}
105103

106104
/// <summary>

src/Resources/ResourceManager/Implementation/Policy/NewAzurePolicyAssignment.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
3131
/// <summary>
3232
/// Creates a policy assignment.
3333
/// </summary>
34-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyAssignment")]
35-
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PSObject))]
34+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PsPolicyAssignment))]
3635
public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParameters
3736
{
3837
private readonly RuntimeDefinedParameterDictionary dynamicParameters = new RuntimeDefinedParameterDictionary();
@@ -80,7 +79,7 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParamete
8079
[Parameter(ParameterSetName = PolicyCmdletBase.DefaultParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
8180
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
8281
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
83-
public PSObject PolicyDefinition { get; set; }
82+
public PsPolicyDefinition PolicyDefinition { get; set; }
8483

8584
/// <summary>
8685
/// Gets or sets the policy assignment policy set definition parameter.
@@ -90,7 +89,7 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParamete
9089
[Parameter(ParameterSetName = PolicyCmdletBase.DefaultParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
9190
[Parameter(ParameterSetName = PolicyCmdletBase.PolicySetParameterObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
9291
[Parameter(ParameterSetName = PolicyCmdletBase.PolicySetParameterStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
93-
public PSObject PolicySetDefinition { get; set; }
92+
public PsPolicySetDefinition PolicySetDefinition { get; set; }
9493

9594
/// <summary>
9695
/// Gets or sets the policy assignment policy parameter object.
@@ -145,12 +144,12 @@ protected override void OnProcessRecord()
145144
throw new PSInvalidOperationException("Only one of PolicyDefinition or PolicySetDefinition can be specified, not both.");
146145
}
147146

148-
if (this.PolicyDefinition != null && this.PolicyDefinition.Properties["policyDefinitionId"] == null)
147+
if (this.PolicyDefinition != null && this.PolicyDefinition.PolicyDefinitionId == null)
149148
{
150149
throw new PSInvalidOperationException("The supplied PolicyDefinition object is invalid.");
151150
}
152151

153-
if (this.PolicySetDefinition != null && this.PolicySetDefinition.Properties["policySetDefinitionId"] == null)
152+
if (this.PolicySetDefinition != null && this.PolicySetDefinition.PolicySetDefinitionId == null)
154153
{
155154
throw new PSInvalidOperationException("The supplied PolicySetDefinition object is invalid.");
156155
}
@@ -178,7 +177,7 @@ protected override void OnProcessRecord()
178177
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
179178
.WaitOnOperation(operationResult: operationResult);
180179

181-
this.WriteObject(this.GetOutputObjects("PolicyAssignmentId", JObject.Parse(result)), enumerateCollection: true);
180+
this.WriteObject(this.GetOutputPolicyAssignments(JObject.Parse(result)), enumerateCollection: true);
182181
}
183182

184183
/// <summary>
@@ -213,11 +212,11 @@ private JToken GetResource()
213212

214213
if (this.PolicyDefinition != null)
215214
{
216-
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicyDefinition.Properties["policyDefinitionId"].Value.ToString();
215+
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicyDefinition.PolicyDefinitionId;
217216
}
218217
else if (this.PolicySetDefinition != null)
219218
{
220-
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicySetDefinition.Properties["policySetDefinitionId"].Value.ToString();
219+
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicySetDefinition.PolicySetDefinitionId;
221220
}
222221

223222
return policyassignmentObject.ToJToken();
@@ -228,11 +227,11 @@ object IDynamicParameters.GetDynamicParameters()
228227
PSObject parameters = null;
229228
if (this.PolicyDefinition != null)
230229
{
231-
parameters = this.PolicyDefinition.GetPSObjectProperty("Properties.parameters") as PSObject;
230+
parameters = this.PolicyDefinition.Properties.Parameters;
232231
}
233232
else if (this.PolicySetDefinition != null)
234233
{
235-
parameters = this.PolicySetDefinition.GetPSObjectProperty("Properties.parameters") as PSObject;
234+
parameters = this.PolicySetDefinition.Properties.Parameters;
236235
}
237236

238237
if (parameters != null)

src/Resources/ResourceManager/Implementation/Policy/NewAzurePolicyDefinition.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2020
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
21-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2221

2322
using Newtonsoft.Json.Linq;
2423
using Policy;
@@ -28,8 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2827
/// <summary>
2928
/// Creates the new policy definition.
3029
/// </summary>
31-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyDefinition")]
32-
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PSObject))]
30+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PsPolicyDefinition))]
3331
public class NewAzurePolicyDefinitionCmdlet : PolicyCmdletBase
3432
{
3533
/// <summary>
@@ -124,8 +122,7 @@ protected override void OnProcessRecord()
124122
var activity = string.Format("PUT {0}", managementUri.PathAndQuery);
125123
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
126124
.WaitOnOperation(operationResult: operationResult);
127-
128-
this.WriteObject(this.GetOutputObjects("PolicyDefinitionId", JObject.Parse(result)), enumerateCollection: true);
125+
this.WriteObject(this.GetOutputPolicyDefinitions(JObject.Parse(result)), enumerateCollection: true);
129126
}
130127

131128
/// <summary>

src/Resources/ResourceManager/Implementation/Policy/NewAzurePolicySetDefinition.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
20-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2120

2221
using Newtonsoft.Json.Linq;
2322
using Policy;
@@ -27,8 +26,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2726
/// <summary>
2827
/// Creates the policy set definition.
2928
/// </summary>
30-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicySetDefinition")]
31-
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSObject))]
29+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PsPolicySetDefinition))]
3230
public class NewAzurePolicySetDefinitionCmdlet : PolicyCmdletBase
3331
{
3432
/// <summary>
@@ -126,7 +124,7 @@ protected override void OnProcessRecord()
126124
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
127125
.WaitOnOperation(operationResult: operationResult);
128126

129-
this.WriteObject(this.GetOutputObjects("PolicySetDefinitionId", JObject.Parse(result)), enumerateCollection: true);
127+
this.WriteObject(this.GetOutputPolicySetDefinitions(JObject.Parse(result)), enumerateCollection: true);
130128
}
131129
}
132130

0 commit comments

Comments
 (0)