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 ;
18
+ using System . Collections . Generic ;
19
+ using System . Linq ;
20
+ using System . Management . Automation ;
21
+
22
+ using Microsoft . Azure . Commands . ResourceManager . Common ;
23
+ using Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters ;
24
+ using Microsoft . Azure . Management . ResourceManager ;
25
+ using Microsoft . Azure . Management . ResourceManager . Models ;
26
+
27
+ /// <summary>
28
+ /// Get an existing resource.
29
+ /// </summary>
30
+ [ Cmdlet ( VerbsCommon . Get , AzureRMConstants . AzureRMPrefix + "PolicyAlias" ) , OutputType ( typeof ( PsResourceProviderAlias ) ) ]
31
+ public class GetAzurePolicyAlias : ResourceManagerCmdletBase
32
+ {
33
+ /// <summary>
34
+ /// Gets or sets the provider namespace match string
35
+ /// </summary>
36
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Limits the output to items whose namespace matches this value." ) ]
37
+ [ Alias ( "Name" , "Namespace" ) ]
38
+ [ ValidateNotNullOrEmpty ]
39
+ public string NamespaceMatch { get ; set ; } = string . Empty ;
40
+
41
+ /// <summary>
42
+ /// Gets or sets the resource type match string
43
+ /// </summary>
44
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Limits the output to items whose resource type matches this value." ) ]
45
+ [ Alias ( "ResourceType" , "Resource" ) ]
46
+ [ ValidateNotNullOrEmpty ]
47
+ public string ResourceTypeMatch { get ; set ; } = string . Empty ;
48
+
49
+ /// <summary>
50
+ /// Gets or sets the alias match string
51
+ /// </summary>
52
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Includes in the output items with aliases whose name matches this value." ) ]
53
+ [ Alias ( "Alias" ) ]
54
+ [ ValidateNotNullOrEmpty ]
55
+ public string AliasMatch { get ; set ; } = string . Empty ;
56
+
57
+ /// <summary>
58
+ /// Gets or sets the alias path match string
59
+ /// </summary>
60
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Includes in the output items with aliases containing a path that matches this value." ) ]
61
+ [ Alias ( "Path" ) ]
62
+ [ ValidateNotNullOrEmpty ]
63
+ public string PathMatch { get ; set ; } = string . Empty ;
64
+
65
+ /// <summary>
66
+ /// Gets or sets the api version match string
67
+ /// </summary>
68
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Includes in the output items whose resource types or aliases have a matching api version." ) ]
69
+ [ ValidateNotNullOrEmpty ]
70
+ public string ApiVersionMatch { get ; set ; } = string . Empty ;
71
+
72
+ /// <summary>
73
+ /// Gets or sets the alias match string
74
+ /// </summary>
75
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Includes in the output items whose resource types have a matching location." ) ]
76
+ [ Alias ( "Location" ) ]
77
+ [ ValidateNotNullOrEmpty ]
78
+ [ LocationCompleter ( "Microsoft.Resources/resourceGroups" ) ]
79
+ public string LocationMatch { get ; set ; } = string . Empty ;
80
+
81
+ /// <summary>
82
+ /// Gets or sets list available flag
83
+ /// </summary>
84
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = false , HelpMessage = "Includes in the output matching items with and without aliases." ) ]
85
+ [ Alias ( "ShowAll" ) ]
86
+ [ ValidateNotNullOrEmpty ]
87
+ public SwitchParameter ListAvailable { get ; set ; }
88
+
89
+ /// <summary>
90
+ /// Executes the cmdlet
91
+ /// </summary>
92
+ public override void ExecuteCmdlet ( )
93
+ {
94
+ // remove leading and trailing whitespace
95
+ this . NamespaceMatch = this . NamespaceMatch ? . Trim ( ) ?? string . Empty ;
96
+ this . ResourceTypeMatch = this . ResourceTypeMatch ? . Trim ( ) ?? string . Empty ;
97
+ this . AliasMatch = this . AliasMatch ? . Trim ( ) ?? string . Empty ;
98
+ this . PathMatch = this . PathMatch ? . Trim ( ) ?? String . Empty ;
99
+ this . ApiVersionMatch = this . ApiVersionMatch ? . Trim ( ) ?? string . Empty ;
100
+ this . LocationMatch = this . LocationMatch ? . Trim ( ) ?? string . Empty ;
101
+
102
+ var resourceTypes = this . GetProviderResourceTypes ( this . ListAvailable , this . NamespaceMatch , this . ResourceTypeMatch , this . AliasMatch , this . PathMatch , this . ApiVersionMatch , this . LocationMatch ) ;
103
+ this . WriteObject ( resourceTypes , enumerateCollection : true ) ;
104
+ }
105
+
106
+ private bool IsStringMatch ( string input , string match )
107
+ {
108
+ return input . IndexOf ( match , StringComparison . OrdinalIgnoreCase ) >= 0 ;
109
+ }
110
+
111
+ private bool IsMatch ( string input , string match )
112
+ {
113
+ return string . IsNullOrEmpty ( match ) || this . IsStringMatch ( input , match ) ;
114
+ }
115
+
116
+ private IEnumerable < Provider > GetAllProviders ( )
117
+ {
118
+ return this . ResourceManagerSdkClient . ResourceManagementClient . Providers . List ( expand : "resourceTypes/aliases" ) ;
119
+ }
120
+
121
+ private IEnumerable < Provider > GetMatchingProviders ( IEnumerable < Provider > input , string namespaceMatch , string resourceTypeMatch )
122
+ {
123
+ // Filter the list of all providers to what matches on namespace and resource type
124
+ return input . Where ( item => this . IsMatch ( item . NamespaceProperty , namespaceMatch ) && item . ResourceTypes . Any ( r => IsMatch ( r . ResourceType , resourceTypeMatch ) ) ) ;
125
+ }
126
+
127
+ private bool FilterFunction ( ProviderResourceType providerResourceType , bool listAvailable , string resourceTypesMatch , string aliasMatch , string pathMatch , string apiVersionMatch , string locationMatch )
128
+ {
129
+ return
130
+ // if resource type match was provided, the resource type name must match
131
+ ( string . IsNullOrEmpty ( resourceTypesMatch ) || this . IsStringMatch ( providerResourceType . ResourceType , resourceTypesMatch ) ) &&
132
+
133
+ // include everything remaining if list available switch is provided
134
+ ( listAvailable ||
135
+
136
+ // otherwise just those with aliases that match the rest of the parameters
137
+ providerResourceType . Aliases . Coalesce ( ) . Any ( ) &&
138
+
139
+ // if no match strings provided, include everything
140
+ ( string . IsNullOrEmpty ( locationMatch ) && string . IsNullOrEmpty ( aliasMatch ) && string . IsNullOrEmpty ( pathMatch ) && string . IsNullOrEmpty ( apiVersionMatch ) ||
141
+
142
+ // if location match was provided, include those with matching location
143
+ ! string . IsNullOrEmpty ( locationMatch ) && providerResourceType . Locations . Coalesce ( ) . Any ( l => this . IsStringMatch ( l , locationMatch ) ) ||
144
+
145
+ // if API match was provided, include those with matching resource type API version
146
+ ! string . IsNullOrEmpty ( apiVersionMatch ) && providerResourceType . ApiVersions . Coalesce ( ) . Any ( v => this . IsStringMatch ( v , apiVersionMatch ) ) ||
147
+
148
+ // if alias match was provided, include those with matching alias name
149
+ ! string . IsNullOrEmpty ( aliasMatch ) && providerResourceType . Aliases . Coalesce ( ) . Any ( a => this . IsStringMatch ( a . Name , aliasMatch ) ) ||
150
+
151
+ // if alias path match was provided, includes those with matching path
152
+ ! string . IsNullOrEmpty ( pathMatch ) && providerResourceType . Aliases . Coalesce ( ) . Any ( a => a . Paths . Any ( p => this . IsStringMatch ( p . Path , pathMatch ) ) ) ||
153
+
154
+ // if API version match was provided, also include those with matching alias API version
155
+ ! string . IsNullOrEmpty ( apiVersionMatch ) && providerResourceType . Aliases . Coalesce ( ) . Any ( a => a . Paths . Coalesce ( ) . Any ( p => p . ApiVersions . Coalesce ( ) . Any ( v => this . IsStringMatch ( v , apiVersionMatch ) ) ) ) ) ) ;
156
+ }
157
+
158
+ private IEnumerable < PsResourceProviderAlias > GetProviderResourceTypes ( bool listAvailable , string namespaceMatch , string resourceTypeMatch , string aliasMatch , string pathMatch , string apiVersionMatch , string locationMatch )
159
+ {
160
+ var allProviders = this . GetAllProviders ( ) ;
161
+ var providers = this . GetMatchingProviders ( allProviders , namespaceMatch , resourceTypeMatch ) ;
162
+ var rv = new List < PsResourceProviderAlias > ( ) ;
163
+ foreach ( var provider in providers )
164
+ {
165
+ var match = provider . ResourceTypes . Where ( r => this . FilterFunction ( r , listAvailable , resourceTypeMatch , aliasMatch , pathMatch , apiVersionMatch , locationMatch ) ) ;
166
+ rv . AddRange ( match . Select ( t => new PsResourceProviderAlias { Aliases = t . Aliases , ApiVersions = t . ApiVersions , Locations = t . Locations , Namespace = provider . NamespaceProperty , ResourceType = t . ResourceType } ) ) ;
167
+ }
168
+
169
+ return rv ;
170
+ }
171
+ }
172
+ }
0 commit comments