|
| 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