Skip to content

Commit 0fc57cd

Browse files
authored
Merge pull request Azure#9441 from mentat9/master
Add parameters to Set-AzPolicyAssignment, fixes
2 parents 2c37179 + f65721d commit 0fc57cd

File tree

47 files changed

+19062
-5052
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+19062
-5052
lines changed

src/PolicyInsights/PolicyInsights/Common/ParameterHelpMessages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class ParameterHelpMessages
3030
public const string PolicyAssignmentName = "Policy assignment name.";
3131
public const string PolicyAssignmentId = "Policy assignment ID. E.g. '/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyAssignments/{assignmentName}'.";
3232
public const string PolicyDefinitionReferenceId = "Gets the policy definition reference ID of the individual definition that is being remediated. Required when the policy assignment assigns a policy set definition.";
33-
public const string Top = "Maximum number of records to return.";
33+
public const string Top = "Maximum number of records to return. If not provided, the maximum number of records returned is determined by the Azure Policy service (currently 1000).";
3434
public const string OrderBy = "Ordering expression using OData notation. One or more comma-separated column names with an optional 'desc' (the default) or 'asc'.";
3535
public const string Select = "Select expression using OData notation. One or more comma-separated column names. Limits the columns on each record to just those requested.";
3636
public const string From = "ISO 8601 formatted timestamp specifying the start time of the interval to query. When not specified, defaults to 'To' parameter value minus 1 day.";

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ private bool IsMatch(string input, string match)
115115

116116
private IEnumerable<Provider> GetAllProviders()
117117
{
118-
return this.ResourceManagerSdkClient.ResourceManagementClient.Providers.List(expand: "resourceTypes/aliases");
118+
var returnList = new List<Provider>();
119+
var tempResult = this.ResourceManagerSdkClient.ResourceManagementClient.Providers.List(expand: "resourceTypes/aliases");
120+
returnList.AddRange(tempResult);
121+
122+
while (!string.IsNullOrWhiteSpace(tempResult.NextPageLink))
123+
{
124+
tempResult = this.ResourceManagerSdkClient.ResourceManagementClient.Providers.ListNext(tempResult.NextPageLink);
125+
returnList.AddRange(tempResult);
126+
}
127+
128+
return returnList;
119129
}
120130

121131
private IEnumerable<Provider> GetMatchingProviders(IEnumerable<Provider> input, string namespaceMatch, string resourceTypeMatch)

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17-
using Commands.Common.Authentication.Abstractions;
1817
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1918
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
2019
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
21-
using Microsoft.WindowsAzure.Commands.Common;
22-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
23-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2420
using Newtonsoft.Json.Linq;
2521
using Policy;
2622
using System;
2723
using System.Collections;
28-
using System.Linq;
2924
using System.Management.Automation;
3025
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
3126
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
@@ -198,7 +193,7 @@ private JToken GetResource()
198193
Scope = this.Scope,
199194
NotScopes = this.NotScope ?? null,
200195
Metadata = this.Metadata == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Metadata).ToString()),
201-
Parameters = this.GetParameters()
196+
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject)
202197
}
203198
};
204199

@@ -260,32 +255,5 @@ object IDynamicParameters.GetDynamicParameters()
260255
this.RegisterDynamicParameters(this.dynamicParameters);
261256
return this.dynamicParameters;
262257
}
263-
264-
private JObject GetParameters()
265-
{
266-
// Load parameters from local file or literal
267-
if (this.PolicyParameter != null)
268-
{
269-
string policyParameterFilePath = this.TryResolvePath(this.PolicyParameter);
270-
return FileUtilities.DataStore.FileExists(policyParameterFilePath)
271-
? JObject.Parse(FileUtilities.DataStore.ReadFileAsText(policyParameterFilePath))
272-
: JObject.Parse(this.PolicyParameter);
273-
}
274-
275-
// Load from PS object
276-
if (this.PolicyParameterObject != null)
277-
{
278-
return this.PolicyParameterObject.ToJObjectWithValue();
279-
}
280-
281-
// Load dynamic parameters
282-
var parameters = PowerShellUtilities.GetUsedDynamicParameters(AsJobDynamicParameters, MyInvocation);
283-
if (parameters.Count() > 0)
284-
{
285-
return MyInvocation.BoundParameters.ToJObjectWithValue(parameters.Select(p => p.Name));
286-
}
287-
288-
return null;
289-
}
290258
}
291259
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2121
using Newtonsoft.Json.Linq;
2222
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
2323
using System;
24+
using System.Collections;
2425
using System.IO;
2526
using System.Linq;
2627
using System.Management.Automation;
@@ -54,6 +55,10 @@ public enum ListFilter
5455
protected const string PolicyParameterStringParameterSet = "PolicyParameterStringParameterSet";
5556
protected const string PolicySetParameterObjectParameterSet = "PolicySetParameterObjectParameterSet";
5657
protected const string PolicySetParameterStringParameterSet = "PolicySetParameterStringParameterSet";
58+
protected const string PolicyParameterNameObjectParameterSet = "PolicyParameterNameObjectParameterSet";
59+
protected const string PolicyParameterNameStringParameterSet = "PolicyParameterNameStringParameterSet";
60+
protected const string PolicyParameterIdObjectParameterSet = "PolicyParameterIdObjectParameterSet";
61+
protected const string PolicyParameterIdStringParameterSet = "PolicyParameterIdStringParameterSet";
5762

5863
/// <summary>
5964
/// Converts the resource object to specified resource type object.
@@ -253,5 +258,38 @@ protected async Task<JObject> GetExistingResource(string resourceId, string apiV
253258
cancellationToken: this.CancellationToken.Value)
254259
.ConfigureAwait(continueOnCapturedContext: false);
255260
}
261+
262+
/// <summary>
263+
/// Resolve given input parameters into JSON parameter object for put
264+
/// </summary>
265+
/// <param name="policyParameter"></param>
266+
/// <param name="policyParameterObject"></param>
267+
/// <returns></returns>
268+
protected JObject GetParameters(string policyParameter, Hashtable policyParameterObject)
269+
{
270+
// Load parameters from local file or literal
271+
if (policyParameter != null)
272+
{
273+
string policyParameterFilePath = this.TryResolvePath(policyParameter);
274+
return FileUtilities.DataStore.FileExists(policyParameterFilePath)
275+
? JObject.Parse(FileUtilities.DataStore.ReadFileAsText(policyParameterFilePath))
276+
: JObject.Parse(policyParameter);
277+
}
278+
279+
// Load from PS object
280+
if (policyParameterObject != null)
281+
{
282+
return policyParameterObject.ToJObjectWithValue();
283+
}
284+
285+
// Load dynamic parameters
286+
var parameters = PowerShellUtilities.GetUsedDynamicParameters(AsJobDynamicParameters, MyInvocation);
287+
if (parameters.Count() > 0)
288+
{
289+
return MyInvocation.BoundParameters.ToJObjectWithValue(parameters.Select(p => p.Name));
290+
}
291+
292+
return null;
293+
}
256294
}
257295
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static class PolicyHelpStrings
4646
public const string SetPolicyAssignmentDisplayNameHelp = "The display name of the updated policy assignment";
4747
public const string SetPolicyAssignmentDescriptionHelp = "The description of the updated policy assignment";
4848
public const string SetPolicyAssignmentMetadataHelp = "The updated metadata for the policy assignment. This can either be a path to a file name containing the metadata, or the metadata as a string.";
49+
public const string SetPolicyAssignmentPolicyParameterObjectHelp = "The new policy parameters object for the policy assignment.";
50+
public const string SetPolicyParameterHelp = "The new policy parameters file path or string for the policy assignment.";
4951
public const string SetPolicyAssignmentSkuHelp = "A hash table which specifies sku properties. This parameter is deprecated and ignored.";
5052
public const string PolicyAssignmentAssignIdentityHelp = "Generate and assign an Azure Active Directory Identity for this policy assignment. The identity will be used when executing deployments for 'deployIfNotExists' policies. Location is required when assigning an identity.";
5153
public const string PolicyAssignmentLocationHelp = "The location of the policy assignment's resource identity. This is required when the -AssignIdentity switch is used.";

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
16-
1715
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1816
{
1917
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2018
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
2119
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
22-
using Microsoft.WindowsAzure.Commands.Common;
2320
using Newtonsoft.Json.Linq;
2421
using Policy;
2522
using System.Collections;
@@ -37,13 +34,17 @@ public class SetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
3734
/// Gets or sets the policy assignment name parameter.
3835
/// </summary>
3936
[Parameter(ParameterSetName = PolicyCmdletBase.NameParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentNameHelp)]
37+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterNameObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentPolicyParameterObjectHelp)]
38+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterNameStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyParameterHelp)]
4039
[ValidateNotNullOrEmpty]
4140
public string Name { get; set; }
4241

4342
/// <summary>
4443
/// Gets or sets the policy assignment scope parameter.
4544
/// </summary>
4645
[Parameter(ParameterSetName = PolicyCmdletBase.NameParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentScopeHelp)]
46+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterNameObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentPolicyParameterObjectHelp)]
47+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterNameStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyParameterHelp)]
4748
[ValidateNotNullOrEmpty]
4849
public string Scope { get; set; }
4950

@@ -59,6 +60,8 @@ public class SetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
5960
/// </summary>
6061
[Alias("ResourceId")]
6162
[Parameter(ParameterSetName = PolicyCmdletBase.IdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentIdHelp)]
63+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterIdObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentPolicyParameterObjectHelp)]
64+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterIdStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyParameterHelp)]
6265
[ValidateNotNullOrEmpty]
6366
public string Id { get; set; }
6467

@@ -83,6 +86,21 @@ public class SetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
8386
[ValidateNotNullOrEmpty]
8487
public string Metadata { get; set; }
8588

89+
/// <summary>
90+
/// Gets or sets the policy assignment policy parameter object.
91+
/// </summary>
92+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterNameObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentPolicyParameterObjectHelp)]
93+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterIdObjectParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentPolicyParameterObjectHelp)]
94+
public Hashtable PolicyParameterObject { get; set; }
95+
96+
/// <summary>
97+
/// Gets or sets the policy assignment policy parameter file path or policy parameter string.
98+
/// </summary>
99+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterNameStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyParameterHelp)]
100+
[Parameter(ParameterSetName = PolicyCmdletBase.PolicyParameterIdStringParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyParameterHelp)]
101+
[ValidateNotNullOrEmpty]
102+
public string PolicyParameter { get; set; }
103+
86104
/// <summary>
87105
/// Gets or sets a flag indicating whether a system assigned identity should be added to the policy assignment.
88106
/// </summary>
@@ -149,7 +167,7 @@ private JToken GetResource(string resourceId, string apiVersion)
149167
NotScopes = this.NotScope ?? resource.Properties["NotScopes"]?.ToString().Split(','),
150168
PolicyDefinitionId = resource.Properties["policyDefinitionId"].ToString(),
151169
Metadata = string.IsNullOrEmpty(this.Metadata) ? null : JObject.Parse(metaDataJson),
152-
Parameters = (JObject)resource.Properties["parameters"]
170+
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject) ?? (JObject)resource.Properties["parameters"]
153171
}
154172
};
155173

0 commit comments

Comments
 (0)