@@ -120,91 +120,59 @@ public static List<ResourceIdentifier> GetResourceIdsFromClient(string resourceT
120120 {
121121 IAzureContext context = AzureRmProfileProvider . Instance ? . Profile ? . DefaultContext ;
122122 IResourceManagementClient client = AzureSession . Instance . ClientFactory . CreateArmClient < ResourceManagementClient > ( context , AzureEnvironment . Endpoint . ResourceManager ) ;
123- Task < IPage < GenericResource > > allProviders = null ;
124123 var odataQuery = new ODataQuery < GenericResourceFilter > ( r => r . ResourceType == resourceType ) ;
125124
126- if ( string . IsNullOrWhiteSpace ( resourceGroupName ) )
127- {
128- allProviders = client . Resources . ListAsync ( odataQuery ) ;
129- }
130- else
131- {
132- allProviders = client . ResourceGroups . ListResourcesAsync ( resourceGroupName , odataQuery ) ;
133- }
125+ var allProviders = string . IsNullOrWhiteSpace ( resourceGroupName )
126+ ? client . Resources . ListAsync ( odataQuery )
127+ : client . ResourceGroups . ListResourcesAsync ( resourceGroupName , odataQuery ) ;
128+
129+ var timeoutDuration = _timeout == - 1 ? TimeSpan . FromMinutes ( 5 ) : TimeSpan . FromSeconds ( _timeout ) ;
130+ var hasNotTimedOut = allProviders . Wait ( timeoutDuration ) ;
131+ var hasResult = allProviders . Result != null ;
132+ var isSuccessful = hasNotTimedOut && hasResult ;
134133
135- List < ResourceIdentifier > ids = new List < ResourceIdentifier > ( ) ;
136- if ( _timeout == - 1 )
137- {
138- allProviders . Wait ( TimeSpan . FromMinutes ( 5 ) ) ;
139- if ( allProviders . Result != null )
140- {
141- allProviders . Result . ToList ( ) . ForEach ( resource => ids . Add ( new ResourceIdentifier ( resource . Id ) ) ) ;
142- }
143- else
144- {
145134#if DEBUG
146- throw new InvalidOperationException ( "Result from client.Providers is null" ) ;
147- #endif
148- }
149- }
150- else if ( allProviders . Wait ( TimeSpan . FromSeconds ( _timeout ) ) )
135+ if ( ! isSuccessful )
151136 {
152- if ( allProviders . Result != null )
153- {
154- allProviders . Result . ToList ( ) . ForEach ( resource => ids . Add ( new ResourceIdentifier ( resource . Id ) ) ) ;
155- }
156- else
157- {
158- #if DEBUG
159- throw new InvalidOperationException ( "Result from client.Providers is null" ) ;
160- #endif
161- }
137+ throw new InvalidOperationException ( ! hasResult ? "Result from client.Providers is null" : Resources . TimeOutForProviderList ) ;
162138 }
163- else
164- {
165- #if DEBUG
166- throw new InvalidOperationException ( Resources . TimeOutForProviderList ) ;
167139#endif
168- }
169140
170- return ids ;
141+ return isSuccessful
142+ ? allProviders . Result . Select ( resource => new ResourceIdentifier ( resource . Id ) ) . ToList ( )
143+ : new List < ResourceIdentifier > ( ) ;
171144 }
172145
173146 public static List < string > FilterByParentResource ( List < ResourceIdentifier > ids , string [ ] parentResources )
174147 {
175- List < string > output = new List < string > ( ) ;
176- foreach ( var resource in ids )
148+ return ids . Where ( resource =>
177149 {
178- var include = true ;
150+ if ( resource . ParentResource == null )
151+ {
152+ return true ;
153+ }
179154
180- if ( resource . ParentResource != null )
155+ var actualParentResources = resource . ParentResource . Split ( '/' ) . Where ( ( pr , i ) => i % 2 != 0 ) . ToArray ( ) ;
156+ var parentResourcesExceptFirst = parentResources . Skip ( 1 ) . ToArray ( ) ;
157+ if ( actualParentResources . Count ( ) != parentResourcesExceptFirst . Count ( ) )
181158 {
182- var actualParentResource = resource . ParentResource . Split ( '/' ) ;
183- if ( actualParentResource . Count ( ) / 2 == parentResources . Count ( ) - 1 )
184- {
185- for ( int i = 0 ; i < actualParentResource . Count ( ) / 2 ; i ++ )
186- {
187- if ( ! string . IsNullOrEmpty ( parentResources [ i + 1 ] ) && ! string . Equals ( actualParentResource [ i * 2 + 1 ] , parentResources [ i + 1 ] , StringComparison . OrdinalIgnoreCase ) )
188- {
189- include = false ;
190- }
191- }
192- }
193- else
194- {
195159#if DEBUG
196- throw new InvalidOperationException ( "Improper number of parent resources were given" ) ;
160+ throw new InvalidOperationException ( "Improper number of parent resources were given" ) ;
161+ #else
162+ return true ;
197163#endif
198- }
199164 }
200165
201- if ( include )
166+ for ( int i = 0 ; i < actualParentResources . Count ( ) ; i ++ )
202167 {
203- output . Add ( resource . ResourceName ) ;
168+ if ( ! string . IsNullOrEmpty ( parentResourcesExceptFirst [ i ] ) && ! string . Equals ( actualParentResources [ i ] , parentResourcesExceptFirst [ i ] , StringComparison . OrdinalIgnoreCase ) )
169+ {
170+ return false ;
171+ }
204172 }
205- }
206173
207- return output ;
174+ return true ;
175+ } ) . Select ( resource => resource . ResourceName ) . ToList ( ) ;
208176 }
209177 }
210178}
0 commit comments