Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Resources/ResourceManager/Components/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ public static class Constants
/// <summary>
/// The default policy definition API version.
/// </summary>
public static readonly string PolicyDefinitionApiVersion = "2019-06-01";
public static readonly string PolicyDefinitionApiVersion = "2019-09-01";

/// <summary>
/// The default policy set definition API version.
/// </summary>
public static readonly string PolicySetDefintionApiVersion = "2019-06-01";
public static readonly string PolicySetDefintionApiVersion = "2019-09-01";

/// <summary>
/// The default policy assignment API version.
/// </summary>
public static readonly string PolicyAssignmentApiVersion = "2019-06-01";
public static readonly string PolicyAssignmentApiVersion = "2019-09-01";

/// <summary>
/// The default providers API version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,11 @@ public class PolicySetDefinitionProperties
/// </summary>
[JsonProperty(Required = Required.Default)]
public JObject Parameters { get; set; }

/// <summary>
/// The policy definition groups.
/// </summary>
[JsonProperty(Required = Required.Default)]
public JArray PolicyDefinitionGroups { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void RunCmdlet()
{
var listFilter = this.GetListFilter(this.Builtin, this.Custom);
PaginatedResponseHelper.ForEach(
getFirstPage: () => this.GetResources(),
getFirstPage: () => this.GetResources(listFilter),
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
cancellationToken: this.CancellationToken,
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputObjects("PolicyDefinitionId", listFilter, resources), enumerateCollection: true));
Expand All @@ -103,9 +103,11 @@ private void RunCmdlet()
/// <summary>
/// Queries the ARM cache and returns the cached resource that match the query specified.
/// </summary>
private async Task<ResponseWithContinuation<JObject[]>> GetResources()
/// <param name="policyTypeFilter">The policy type filter.</param>
private async Task<ResponseWithContinuation<JObject[]>> GetResources(ListFilter policyTypeFilter)
{
string resourceId = this.GetResourceId();
var odataFilter = policyTypeFilter != ListFilter.None ? string.Format(PolicyCmdletBase.PolicyTypeFilterFormat, policyTypeFilter.ToString()) : null;

var apiVersion = string.IsNullOrWhiteSpace(this.ApiVersion) ? Constants.PolicyDefinitionApiVersion : this.ApiVersion;

Expand Down Expand Up @@ -150,7 +152,8 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
.ListObjectColleciton<JObject>(
resourceCollectionId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
cancellationToken: this.CancellationToken.Value,
odataQuery: odataFilter)
.ConfigureAwait(continueOnCapturedContext: false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void RunCmdlet()
{
var listFilter = this.GetListFilter(this.Builtin, this.Custom);
PaginatedResponseHelper.ForEach(
getFirstPage: () => this.GetResources(),
getFirstPage: () => this.GetResources(listFilter),
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
cancellationToken: this.CancellationToken,
action: resources => this.WriteObject(sendToPipeline: this.GetFilteredOutputObjects("PolicySetDefinitionId", listFilter, resources), enumerateCollection: true));
Expand All @@ -103,9 +103,11 @@ private void RunCmdlet()
/// <summary>
/// Queries the ARM cache and returns the cached resource that match the query specified.
/// </summary>
private async Task<ResponseWithContinuation<JObject[]>> GetResources()
/// <param name="policyTypeFilter">The policy type filter.</param>
private async Task<ResponseWithContinuation<JObject[]>> GetResources(ListFilter policyTypeFilter)
{
string resourceId = this.GetResourceId();
var odataFilter = policyTypeFilter != ListFilter.None ? string.Format(PolicyCmdletBase.PolicyTypeFilterFormat, policyTypeFilter.ToString()) : null;

var apiVersion = string.IsNullOrWhiteSpace(this.ApiVersion) ? Constants.PolicySetDefintionApiVersion : this.ApiVersion;

Expand Down Expand Up @@ -146,12 +148,13 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
else
{
return await this
.GetResourcesClient()
.ListObjectColleciton<JObject>(
resourceCollectionId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
.GetResourcesClient()
.ListObjectColleciton<JObject>(
resourceCollectionId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value,
odataQuery: odataFilter)
.ConfigureAwait(continueOnCapturedContext: false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private JToken GetResource()
Description = this.Description ?? null,
Scope = this.Scope,
NotScopes = this.NotScope ?? null,
Metadata = this.Metadata == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Metadata).ToString()),
Metadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)),
EnforcementMode = EnforcementMode ?? PolicyAssignmentEnforcementMode.Default,
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ private JToken GetResource()
{
Description = this.Description ?? null,
DisplayName = this.DisplayName ?? null,
PolicyRule = JObject.Parse(this.GetObjectFromParameter(this.Policy).ToString()),
Metadata = this.Metadata == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Metadata).ToString()),
Parameters = this.Parameter == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Parameter).ToString()),
PolicyRule = this.GetObjectFromParameter(this.Policy, nameof(this.Policy)),
Metadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)),
Parameters = this.Parameter == null ? null : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)),
Mode = string.IsNullOrEmpty(this.Mode) ? PolicyDefinitionMode.All : this.Mode
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public class NewAzurePolicySetDefinitionCmdlet : PolicyCmdletBase
[ValidateNotNullOrEmpty]
public Guid? SubscriptionId { get; set; }

/// <summary>
/// Gets or sets the policy definition groups parameter of the new policy set definition
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicySetDefinitionGroupDefinitionHelp)]
[ValidateNotNullOrEmpty]
public string GroupDefinition { get; set; }

/// <summary>
/// Executes the cmdlet.
/// </summary>
Expand Down Expand Up @@ -140,9 +147,10 @@ private JToken GetResource()
{
Description = this.Description ?? null,
DisplayName = this.DisplayName ?? null,
PolicyDefinitions = JArray.Parse(this.GetObjectFromParameter(this.PolicyDefinition).ToString()),
Metadata = this.Metadata == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Metadata).ToString()),
Parameters = this.Parameter == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Parameter).ToString())
PolicyDefinitions = this.GetArrayFromParameter(this.PolicyDefinition, nameof(this.PolicyDefinition)),
Metadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)),
Parameters = this.Parameter == null ? null : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)),
PolicyDefinitionGroups = this.GroupDefinition == null ? null : this.GetArrayFromParameter(this.GroupDefinition, nameof(this.GroupDefinition))
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public enum ListFilter
protected const string PolicyParameterIdObjectParameterSet = "PolicyParameterIdObjectParameterSet";
protected const string PolicyParameterIdStringParameterSet = "PolicyParameterIdStringParameterSet";

/// <summary>
/// The policy type OData filter format
/// </summary>
protected const string PolicyTypeFilterFormat = "$filter=PolicyType eq '{0}'";

/// <summary>
/// Converts the resource object to specified resource type object.
/// </summary>
Expand Down Expand Up @@ -121,19 +126,66 @@ protected Task<ResponseWithContinuation<TType[]>> GetNextLink<TType>(string next
}

/// <summary>
/// Gets the JToken object from parameter
/// Gets a JObject from a parameter value
/// </summary>
/// <param name="parameter">The parameter value.</param>
/// <param name="parameterName">The name of the parameter.</param>
/// <returns></returns>
protected JObject GetObjectFromParameter(string parameter, string parameterName)
{
var result = this.GetTokenFromParameter(parameter) as JObject;
if (result == null)
{
throw new PSArgumentException(string.Format(ProjectResources.JsonObjectExpected, parameterName), parameterName);
}

return result;
}

/// <summary>
/// Gets a JArray from a parameter value
/// </summary>
protected JToken GetObjectFromParameter(string parameter)
/// <param name="parameter">The parameter value.</param>
/// <param name="parameterName">The name of the parameter.</param>
/// <returns></returns>
protected JArray GetArrayFromParameter(string parameter, string parameterName)
{
var result = this.GetTokenFromParameter(parameter) as JArray;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use an is-pattern here (and above in GetObjectFromParameter()):

if (this.GetTokenFromParameter(parameter) is JArray result)
{
  return result;
}

throw new PSArgumentException(string.Format(ProjectResources.JsonArrayExpected, parameterName), parameterName);

if (result == null)
{
throw new PSArgumentException(string.Format(ProjectResources.JsonArrayExpected, parameterName), parameterName);
}

return result;
}

/// <summary>
/// Gets the resource Id from the supplied PowerShell parameters.
/// </summary>
protected string MakePolicyAssignmentId(string scope, string resourceName)
{
return ResourceIdUtility.GetResourceId(
resourceId: scope,
extensionResourceType: Constants.MicrosoftAuthorizationPolicyAssignmentType,
extensionResourceName: resourceName);
}

/// <summary>
/// Gets the JToken from parameter
/// </summary>
/// <param name="parameter">The parameter to be parsed.</param>
private JToken GetTokenFromParameter(string parameter)
{
Uri outUri = null;

if (Uri.TryCreate(parameter, UriKind.Absolute, out outUri))
{
if(outUri.Scheme == Uri.UriSchemeFile)
if (outUri.Scheme == Uri.UriSchemeFile)
{
string filePath = this.TryResolvePath(parameter);
if(File.Exists(filePath))
if (File.Exists(filePath))
{
return JToken.FromObject(FileUtilities.DataStore.ReadFileAsText(filePath));
return JToken.Parse(FileUtilities.DataStore.ReadFileAsText(filePath));
}
else
{
Expand All @@ -151,31 +203,21 @@ protected JToken GetObjectFromParameter(string parameter)
else
{
string contents = GeneralUtilities.DownloadFile(outUri.AbsoluteUri);
if(string.IsNullOrEmpty(contents))
if (string.IsNullOrEmpty(contents))
{
throw new PSInvalidOperationException(string.Format(ProjectResources.InvalidUriContent, parameter));
}
return JToken.FromObject(contents);

return JToken.Parse(contents);
}
}

//for non absolute file paths
string path = this.TryResolvePath(parameter);

return File.Exists(path)
? JToken.FromObject(FileUtilities.DataStore.ReadFileAsText(path))
: JToken.FromObject(parameter);
}

/// <summary>
/// Gets the resource Id from the supplied PowerShell parameters.
/// </summary>
protected string MakePolicyAssignmentId(string scope, string resourceName)
{
return ResourceIdUtility.GetResourceId(
resourceId: scope,
extensionResourceType: Constants.MicrosoftAuthorizationPolicyAssignmentType,
extensionResourceName: resourceName);
? JToken.Parse(FileUtilities.DataStore.ReadFileAsText(path))
: JToken.Parse(parameter);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ public static class PolicyHelpStrings
public const string NewPolicySetDefinitionDisplayNameHelp = "The display name for the new policy set definition.";
public const string NewPolicySetDefinitionDescriptionHelp = "The description for the new policy set definition.";
public const string NewPolicySetDefinitionMetadataHelp = "The metadata for the new policy set definition. This can either be a path to a file name containing the metadata, or the metadata as a string.";
public const string NewPolicySetDefinitionPolicyDefinitionHelp = "The policy definition for the new policy set definition. This can either be a path to a file name containing the policy definitions, or the policy set definition as a string.";
public const string NewPolicySetDefinitionParametersHelp = "The parameters declaration for the new policy set definition. This can either be a path to a file name or uri containing the parameters declaration, or the parameters declaration as a string.";
public const string NewPolicySetDefinitionPolicyDefinitionHelp = "The policy definitions for the new policy set definition. This can either be a path to a file containing the policy definitions, or the policy set definition as a JSON string.";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch/fixes here.

public const string NewPolicySetDefinitionGroupDefinitionHelp = "The policy definition groups for the new policy set definition. This can either be a path to a file containing the groups, or the groups as a JSON string.";
public const string NewPolicySetDefinitionParametersHelp = "The parameters declaration for the new policy set definition. This can either be a path to a file or uri containing the parameters declaration, or the parameters declaration as a JSON string.";
public const string NewPolicySetDefinitionManagementGroupHelp = "The name of the management group of the new policy set definition.";
public const string NewPolicySetDefinitionSubscriptionIdHelp = "The subscription ID of the new policy set definition.";
public const string RemovePolicySetDefinitionNameHelp = "The policy set definition name to delete.";
Expand All @@ -111,9 +112,10 @@ public static class PolicyHelpStrings
public const string SetPolicySetDefinitionIdHelp = "The fully qualified policy set definition ID to get, including the subscription or management group. e.g. /providers/Microsoft.Management/managementGroups/{managementGroup}/providers/Microsoft.Authorization/policySetDefinitions/{policySetDefinitionName}.";
public const string SetPolicySetDefinitionDisplayNameHelp = "The display name of the updated policy set definition.";
public const string SetPolicySetDefinitionDescriptionHelp = "The description of the updated policy set definition.";
public const string SetPolicySetDefinitionPolicyDefinitionHelp = "The policy definitions of the updated policy set definition. This can either be a path to a file name containing the policy definitions, or the policy definitions as a string.";
public const string SetPolicySetDefinitionMetadataHelp = "The metadata of the updated policy set definition. This can either be a path to a file name containing the metadata, or the metadata as a string.";
public const string SetPolicySetDefinitionParameterHelp = "The parameters declaration of the updated policy set definition. This can either be a path to a file name or uri containing the parameters declaration, or the parameters declaration as a string.";
public const string SetPolicySetDefinitionPolicyDefinitionHelp = "The policy definitions of the updated policy set definition. This can either be a path to a file containing the policy definitions, or the policy definitions as a JSON string.";
public const string SetPolicySetDefinitionGroupDefinitionHelp = "The policy definition groups of the updated policy set definition. This can either be a path to a file containing the groups, or the groups as a JSON string.";
public const string SetPolicySetDefinitionMetadataHelp = "The metadata of the updated policy set definition. This can either be a path to a file containing the metadata, or the metadata as a JSON string.";
public const string SetPolicySetDefinitionParameterHelp = "The parameters declaration of the updated policy set definition. This can either be a path to a file or uri containing the parameters declaration, or the parameters declaration as a JSON string.";
public const string SetPolicySetDefinitionManagementGroupHelp = "The name of the management group of the policy set definition to update.";
public const string SetPolicySetDefinitionSubscriptionIdHelp = "The subscription ID of the policy set definition to update.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private JToken GetResource(string resourceId, string apiVersion)
{
var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource();

var metaDataJson = string.IsNullOrEmpty(this.Metadata) ? resource.Properties["metadata"]?.ToString() : GetObjectFromParameter(this.Metadata).ToString();
var metaDataJson = string.IsNullOrEmpty(this.Metadata) ? resource.Properties["metadata"]?.ToString() : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)).ToString();

PolicyAssignmentEnforcementMode? existingMode = null;
if (Enum.TryParse(resource.Properties["enforcementMode"]?.ToString(), true, out PolicyAssignmentEnforcementMode tempMode))
Expand Down
Loading