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 assignment.
25+ /// </summary>
26+ [ Cmdlet ( VerbsCommon . Get , "AzureRmPolicyAssignment" , DefaultParameterSetName = GetAzurePolicyAssignmentCmdlet . ParameterlessSet ) , OutputType ( typeof ( PSObject ) ) ]
27+ public class GetAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
28+ {
29+ /// <summary>
30+ /// The policy Id parameter set.
31+ /// </summary>
32+ internal const string PolicyAssignmentIdParameterSet = "The policy assignment Id parameter set." ;
33+
34+ /// <summary>
35+ /// The policy name parameter set.
36+ /// </summary>
37+ internal const string PolicyAssignmentNameParameterSet = "The policy assignment name parameter set." ;
38+
39+ /// <summary>
40+ /// The list all policy parameter set.
41+ /// </summary>
42+ internal const string ParameterlessSet = "The list all policy assignments parameter set." ;
43+
44+ /// <summary>
45+ /// Gets or sets the policy assignment name parameter.
46+ /// </summary>
47+ [ Parameter ( ParameterSetName = GetAzurePolicyAssignmentCmdlet . PolicyAssignmentNameParameterSet , Mandatory = false , ValueFromPipelineByPropertyName = true , HelpMessage = "The policy assignment name." ) ]
48+ [ ValidateNotNullOrEmpty ]
49+ public string Name { get ; set ; }
50+
51+ /// <summary>
52+ /// Gets or sets the policy assignment scope parameter.
53+ /// </summary>
54+ [ Parameter ( ParameterSetName = GetAzurePolicyAssignmentCmdlet . PolicyAssignmentNameParameterSet , Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "The policy assignment name." ) ]
55+ [ ValidateNotNullOrEmpty ]
56+ public string Scope { get ; set ; }
57+
58+ /// <summary>
59+ /// Gets or sets the policy assignment id parameter
60+ /// </summary>
61+ [ Alias ( "ResourceId" ) ]
62+ [ Parameter ( ParameterSetName = GetAzurePolicyAssignmentCmdlet . PolicyAssignmentIdParameterSet , Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "The fully qualified policy assignment Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}" ) ]
63+ [ ValidateNotNullOrEmpty ]
64+ public string Id { get ; set ; }
65+
66+ /// <summary>
67+ /// Gets or sets the policy assignment policy definition id parameter
68+ /// </summary>
69+ [ Parameter ( ParameterSetName = GetAzurePolicyAssignmentCmdlet . PolicyAssignmentIdParameterSet , Mandatory = false , ValueFromPipelineByPropertyName = true , HelpMessage = "The fully qualified policy assignment Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}" ) ]
70+ [ Parameter ( ParameterSetName = GetAzurePolicyAssignmentCmdlet . PolicyAssignmentNameParameterSet , Mandatory = false , ValueFromPipelineByPropertyName = true , HelpMessage = "The fully qualified policy assignment Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}" ) ]
71+ [ ValidateNotNullOrEmpty ]
72+ public string PolicyDefinitionId { get ; set ; }
73+
74+ /// <summary>
75+ /// Executes the cmdlet.
76+ /// </summary>
77+ protected override void OnProcessRecord ( )
78+ {
79+ base . OnProcessRecord ( ) ;
80+
81+ this . RunCmdlet ( ) ;
82+ }
83+
84+ /// <summary>
85+ /// Contains the cmdlet's execution logic.
86+ /// </summary>
87+ private void RunCmdlet ( )
88+ {
89+ PaginatedResponseHelper . ForEach (
90+ getFirstPage : ( ) => this . GetResources ( ) ,
91+ getNextPage : nextLink => this . GetNextLink < JObject > ( nextLink ) ,
92+ cancellationToken : this . CancellationToken ,
93+ action : resources => this . WriteObject ( sendToPipeline : this . GetOutputObjects ( resources ) , enumerateCollection : true ) ) ;
94+ }
95+
96+ /// <summary>
97+ /// Queries the ARM cache and returns the cached resource that match the query specified.
98+ /// </summary>
99+ private async Task < ResponseWithContinuation < JObject [ ] > > GetResources ( )
100+ {
101+ string resourceId = this . Id ?? this . GetResourceId ( ) ;
102+
103+ var apiVersion = await this
104+ . DetermineApiVersion ( resourceId : resourceId )
105+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
106+
107+ if ( IsResourceGet ( resourceId ) )
108+ {
109+ var resource = await this
110+ . GetResourcesClient ( )
111+ . GetResource < JObject > (
112+ resourceId : resourceId ,
113+ apiVersion : apiVersion ,
114+ cancellationToken : this . CancellationToken . Value ,
115+ odataQuery : null )
116+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
117+ ResponseWithContinuation < JObject [ ] > retVal ;
118+ return resource . TryConvertTo ( out retVal ) && retVal . Value != null
119+ ? retVal
120+ : new ResponseWithContinuation < JObject [ ] > { Value = resource . AsArray ( ) } ;
121+ }
122+ else if ( IsScopeLevelList ( resourceId ) ) //If only scope is given, list assignments call
123+ {
124+ string filter = "$filter=atScope()" ;
125+ return await this
126+ . GetResourcesClient ( )
127+ . ListObjectColleciton < JObject > (
128+ resourceCollectionId : resourceId ,
129+ apiVersion : apiVersion ,
130+ cancellationToken : this . CancellationToken . Value ,
131+ odataQuery : filter )
132+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
133+ }
134+ else
135+ {
136+ string filter = string . IsNullOrEmpty ( this . PolicyDefinitionId )
137+ ? null
138+ : string . Format ( "$filter=policydefinitionid eq '{0}'" , this . PolicyDefinitionId ) ;
139+
140+ return await this
141+ . GetResourcesClient ( )
142+ . ListObjectColleciton < JObject > (
143+ resourceCollectionId : resourceId ,
144+ apiVersion : apiVersion ,
145+ cancellationToken : this . CancellationToken . Value ,
146+ odataQuery : filter )
147+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
148+ }
149+ }
150+
151+ /// <summary>
152+ /// Returns true if it is scope level policy assignment list call
153+ /// </summary>
154+ private bool IsScopeLevelList ( string resourceId )
155+ {
156+ return ( ! string . IsNullOrEmpty ( this . Scope ) && string . IsNullOrEmpty ( this . Name ) )
157+ || ( ! string . IsNullOrEmpty ( this . Scope ) && string . IsNullOrEmpty ( ResourceIdUtility . GetResourceName ( resourceId ) ) ) ;
158+ }
159+
160+ /// <summary>
161+ /// Returns true if it is a single policy assignment get
162+ /// </summary>
163+ /// <param name="resourceId"></param>
164+ private bool IsResourceGet ( string resourceId )
165+ {
166+ return ( ! string . IsNullOrEmpty ( this . Name ) && ! string . IsNullOrEmpty ( this . Scope ) )
167+ || ! string . IsNullOrEmpty ( ResourceIdUtility . GetResourceName ( resourceId ) ) ;
168+ }
169+
170+ /// <summary>
171+ /// Gets the resource Id
172+ /// </summary>
173+ private string GetResourceId ( )
174+ {
175+ var subscriptionId = DefaultContext . Subscription . Id ;
176+ if ( string . IsNullOrEmpty ( this . Name ) && string . IsNullOrEmpty ( this . Scope ) )
177+ {
178+ return string . Format ( "/subscriptions/{0}/providers/{1}" ,
179+ subscriptionId . ToString ( ) ,
180+ Constants . MicrosoftAuthorizationPolicyAssignmentType ) ;
181+ }
182+ else if ( string . IsNullOrEmpty ( this . Name ) && ! string . IsNullOrEmpty ( this . Scope ) )
183+ {
184+ return ResourceIdUtility . GetResourceId (
185+ resourceId : this . Scope ,
186+ extensionResourceType : Constants . MicrosoftAuthorizationPolicyAssignmentType ,
187+ extensionResourceName : null ) ;
188+ }
189+ return ResourceIdUtility . GetResourceId (
190+ resourceId : this . Scope ,
191+ extensionResourceType : Constants . MicrosoftAuthorizationPolicyAssignmentType ,
192+ extensionResourceName : this . Name ) ;
193+ }
194+ }
195+ }
0 commit comments