Skip to content

Commit 89a2dfc

Browse files
committed
Rebase master round 1 - restore Azure#11184.
1 parent 744ae6d commit 89a2dfc

37 files changed

+3319
-93
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +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;
21-
2220
using Newtonsoft.Json.Linq;
2321
using Policy;
2422
using System.Management.Automation;
@@ -27,8 +25,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2725
/// <summary>
2826
/// Gets the policy assignment.
2927
/// </summary>
30-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyAssignment")]
31-
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PSObject))]
28+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PsPolicyAssignment))]
3229
public class GetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
3330
{
3431
/// <summary>
@@ -87,7 +84,7 @@ private void RunCmdlet()
8784
getFirstPage: () => this.GetResources(),
8885
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
8986
cancellationToken: this.CancellationToken,
90-
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects("PolicyAssignmentId", resources), enumerateCollection: true));
87+
action: resources => this.WriteObject(sendToPipeline: this.GetOutputPolicyAssignments(resources), enumerateCollection: true));
9188
}
9289

9390
/// <summary>

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +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;
22-
2321
using Newtonsoft.Json.Linq;
2422
using Policy;
2523
using System;
@@ -29,8 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2927
/// <summary>
3028
/// Gets the policy definition.
3129
/// </summary>
32-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyDefinition")]
33-
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PSObject))]
30+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PsPolicyDefinition))]
3431
public class GetAzurePolicyDefinitionCmdlet : PolicyCmdletBase
3532
{
3633
/// <summary>
@@ -100,7 +97,7 @@ private void RunCmdlet()
10097
getFirstPage: () => this.GetResources(listFilter),
10198
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
10299
cancellationToken: this.CancellationToken,
103-
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputObjects("PolicyDefinitionId", listFilter, resources), enumerateCollection: true));
100+
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputPolicyDefinitions(listFilter, resources), enumerateCollection: true));
104101
}
105102

106103
/// <summary>

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +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;
22-
2321
using Newtonsoft.Json.Linq;
2422
using Policy;
2523
using System;
@@ -29,8 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2927
/// <summary>
3028
/// Gets the policy set definition.
3129
/// </summary>
32-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicySetDefinition")]
33-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PSObject))]
30+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PsPolicySetDefinition))]
3431
public class GetAzurePolicySetDefinitionCmdlet : PolicyCmdletBase
3532
{
3633
/// <summary>
@@ -100,7 +97,7 @@ private void RunCmdlet()
10097
getFirstPage: () => this.GetResources(listFilter),
10198
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
10299
cancellationToken: this.CancellationToken,
103-
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputObjects("PolicySetDefinitionId", listFilter, resources), enumerateCollection: true));
100+
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputPolicySetDefinitions(listFilter, resources), enumerateCollection: true));
104101
}
105102

106103
/// <summary>

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2222
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2323
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
2424
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
25-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
26-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
27-
25+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2826
using Newtonsoft.Json.Linq;
2927
using Policy;
3028

3129
/// <summary>
3230
/// Creates a policy assignment.
3331
/// </summary>
34-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyAssignment")]
35-
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PSObject))]
32+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyAssignment", DefaultParameterSetName = PolicyCmdletBase.DefaultParameterSet), OutputType(typeof(PsPolicyAssignment))]
3633
public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParameters
3734
{
3835
private readonly RuntimeDefinedParameterDictionary dynamicParameters = new RuntimeDefinedParameterDictionary();
@@ -75,22 +72,20 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParamete
7572
/// <summary>
7673
/// Gets or sets the policy assignment policy definition parameter.
7774
/// </summary>
78-
[CmdletParameterBreakingChange("PolicyDefinition", OldParamaterType = typeof(PSObject), NewParameterTypeName = "PsPolicyDefinition")]
7975
[Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
8076
[Parameter(ParameterSetName = PolicyCmdletBase.DefaultParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
8177
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
8278
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicyDefinitionHelp)]
83-
public PSObject PolicyDefinition { get; set; }
79+
public PsPolicyDefinition PolicyDefinition { get; set; }
8480

8581
/// <summary>
8682
/// Gets or sets the policy assignment policy set definition parameter.
8783
/// </summary>
88-
[CmdletParameterBreakingChange("PolicySetDefinition", OldParamaterType = typeof(PSObject), NewParameterTypeName = "PsPolicySetDefinition")]
8984
[Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
9085
[Parameter(ParameterSetName = PolicyCmdletBase.DefaultParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
9186
[Parameter(ParameterSetName = PolicyCmdletBase.PolicySetParameterObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
9287
[Parameter(ParameterSetName = PolicyCmdletBase.PolicySetParameterStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentPolicySetDefinitionHelp)]
93-
public PSObject PolicySetDefinition { get; set; }
88+
public PsPolicySetDefinition PolicySetDefinition { get; set; }
9489

9590
/// <summary>
9691
/// Gets or sets the policy assignment policy parameter object.
@@ -145,12 +140,12 @@ protected override void OnProcessRecord()
145140
throw new PSInvalidOperationException("Only one of PolicyDefinition or PolicySetDefinition can be specified, not both.");
146141
}
147142

148-
if (this.PolicyDefinition != null && this.PolicyDefinition.Properties["policyDefinitionId"] == null)
143+
if (this.PolicyDefinition != null && this.PolicyDefinition.PolicyDefinitionId == null)
149144
{
150145
throw new PSInvalidOperationException("The supplied PolicyDefinition object is invalid.");
151146
}
152147

153-
if (this.PolicySetDefinition != null && this.PolicySetDefinition.Properties["policySetDefinitionId"] == null)
148+
if (this.PolicySetDefinition != null && this.PolicySetDefinition.PolicySetDefinitionId == null)
154149
{
155150
throw new PSInvalidOperationException("The supplied PolicySetDefinition object is invalid.");
156151
}
@@ -178,7 +173,7 @@ protected override void OnProcessRecord()
178173
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
179174
.WaitOnOperation(operationResult: operationResult);
180175

181-
this.WriteObject(this.GetOutputObjects("PolicyAssignmentId", JObject.Parse(result)), enumerateCollection: true);
176+
this.WriteObject(this.GetOutputPolicyAssignments(JObject.Parse(result)), enumerateCollection: true);
182177
}
183178

184179
/// <summary>
@@ -213,11 +208,11 @@ private JToken GetResource()
213208

214209
if (this.PolicyDefinition != null)
215210
{
216-
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicyDefinition.Properties["policyDefinitionId"].Value.ToString();
211+
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicyDefinition.PolicyDefinitionId;
217212
}
218213
else if (this.PolicySetDefinition != null)
219214
{
220-
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicySetDefinition.Properties["policySetDefinitionId"].Value.ToString();
215+
policyassignmentObject.Properties.PolicyDefinitionId = this.PolicySetDefinition.PolicySetDefinitionId;
221216
}
222217

223218
return policyassignmentObject.ToJToken();
@@ -228,11 +223,11 @@ object IDynamicParameters.GetDynamicParameters()
228223
PSObject parameters = null;
229224
if (this.PolicyDefinition != null)
230225
{
231-
parameters = this.PolicyDefinition.GetPSObjectProperty("Properties.parameters") as PSObject;
226+
parameters = this.PolicyDefinition.Properties.Parameters;
232227
}
233228
else if (this.PolicySetDefinition != null)
234229
{
235-
parameters = this.PolicySetDefinition.GetPSObjectProperty("Properties.parameters") as PSObject;
230+
parameters = this.PolicySetDefinition.Properties.Parameters;
236231
}
237232

238233
if (parameters != null)

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +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;
22-
2321
using Newtonsoft.Json.Linq;
2422
using Policy;
2523
using System;
@@ -28,8 +26,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2826
/// <summary>
2927
/// Creates the new policy definition.
3028
/// </summary>
31-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicyDefinition")]
32-
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PSObject))]
29+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicyDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet), OutputType(typeof(PsPolicyDefinition))]
3330
public class NewAzurePolicyDefinitionCmdlet : PolicyCmdletBase
3431
{
3532
/// <summary>
@@ -124,8 +121,7 @@ protected override void OnProcessRecord()
124121
var activity = string.Format("PUT {0}", managementUri.PathAndQuery);
125122
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
126123
.WaitOnOperation(operationResult: operationResult);
127-
128-
this.WriteObject(this.GetOutputObjects("PolicyDefinitionId", JObject.Parse(result)), enumerateCollection: true);
124+
this.WriteObject(this.GetOutputPolicyDefinitions(JObject.Parse(result)), enumerateCollection: true);
129125
}
130126

131127
/// <summary>

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +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;
21-
2220
using Newtonsoft.Json.Linq;
2321
using Policy;
2422
using System;
@@ -27,8 +25,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2725
/// <summary>
2826
/// Creates the policy set definition.
2927
/// </summary>
30-
[CmdletOutputBreakingChange(typeof(PSObject), ReplacementCmdletOutputTypeName = "PsPolicySetDefinition")]
31-
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSObject))]
28+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PolicySetDefinition", DefaultParameterSetName = PolicyCmdletBase.NameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PsPolicySetDefinition))]
3229
public class NewAzurePolicySetDefinitionCmdlet : PolicyCmdletBase
3330
{
3431
/// <summary>
@@ -126,7 +123,7 @@ protected override void OnProcessRecord()
126123
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
127124
.WaitOnOperation(operationResult: operationResult);
128125

129-
this.WriteObject(this.GetOutputObjects("PolicySetDefinitionId", JObject.Parse(result)), enumerateCollection: true);
126+
this.WriteObject(this.GetOutputPolicySetDefinitions(JObject.Parse(result)), enumerateCollection: true);
130127
}
131128
}
132129

0 commit comments

Comments
 (0)