17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
19
using System . Management . Automation ;
20
+ using Microsoft . Azure . Commands . Common . Authentication ;
20
21
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
21
22
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Components ;
22
- using Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkClient ;
23
- using Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkModels ;
24
23
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Utilities ;
24
+ using Microsoft . Azure . Management . ResourceManager ;
25
25
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
26
26
using Newtonsoft . Json . Linq ;
27
27
@@ -59,6 +59,8 @@ public abstract class ResourceWithParameterCmdletBase : ResourceManagerCmdletBas
59
59
60
60
private string templateSpecId ;
61
61
62
+ private ITemplateSpecsClient templateSpecsClient ;
63
+
62
64
protected ResourceWithParameterCmdletBase ( )
63
65
{
64
66
dynamicParameters = new RuntimeDefinedParameterDictionary ( ) ;
@@ -142,30 +144,35 @@ protected ResourceWithParameterCmdletBase()
142
144
"to provide a better error message in the case where not all required parameters are satisfied." ) ]
143
145
public SwitchParameter SkipTemplateParameterPrompt { get ; set ; }
144
146
145
- private TemplateSpecsSdkClient templateSpecsSdkClient ;
146
-
147
147
/// <summary>
148
- /// Gets or sets the Template Specs Azure sdk client wrapper
148
+ /// Gets or sets the Template Specs Azure SDK client
149
149
/// </summary>
150
- public TemplateSpecsSdkClient TemplateSpecsSdkClient
150
+ public ITemplateSpecsClient TemplateSpecsClient
151
151
{
152
152
get
153
153
{
154
- if ( this . templateSpecsSdkClient == null )
154
+ if ( this . templateSpecsClient == null )
155
155
{
156
- this . templateSpecsSdkClient = new TemplateSpecsSdkClient ( DefaultContext ) ;
156
+ this . templateSpecsClient =
157
+ AzureSession . Instance . ClientFactory . CreateArmClient < TemplateSpecsClient > (
158
+ DefaultContext ,
159
+ AzureEnvironment . Endpoint . ResourceManager
160
+ ) ;
157
161
}
158
162
159
- return this . templateSpecsSdkClient ;
163
+ return this . templateSpecsClient ;
160
164
}
161
165
162
- set { this . templateSpecsSdkClient = value ; }
166
+ set { this . templateSpecsClient = value ; }
163
167
}
164
168
165
169
public object GetDynamicParameters ( )
166
170
{
167
171
if ( ! this . IsParameterBound ( c => c . SkipTemplateParameterPrompt ) )
168
172
{
173
+ // Resolve the static parameter names for this cmdlet:
174
+ string [ ] staticParameterNames = this . GetStaticParameterNames ( ) ;
175
+
169
176
if ( TemplateObject != null && TemplateObject != templateObject )
170
177
{
171
178
templateObject = TemplateObject ;
@@ -175,15 +182,15 @@ public object GetDynamicParameters()
175
182
TemplateObject ,
176
183
TemplateParameterObject ,
177
184
this . ResolvePath ( TemplateParameterFile ) ,
178
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
185
+ staticParameterNames ) ;
179
186
}
180
187
else
181
188
{
182
189
dynamicParameters = TemplateUtility . GetTemplateParametersFromFile (
183
190
TemplateObject ,
184
191
TemplateParameterObject ,
185
192
TemplateParameterUri ,
186
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
193
+ staticParameterNames ) ;
187
194
}
188
195
}
189
196
else if ( ! string . IsNullOrEmpty ( TemplateFile ) &&
@@ -196,15 +203,15 @@ public object GetDynamicParameters()
196
203
this . ResolvePath ( TemplateFile ) ,
197
204
TemplateParameterObject ,
198
205
this . ResolvePath ( TemplateParameterFile ) ,
199
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
206
+ staticParameterNames ) ;
200
207
}
201
208
else
202
209
{
203
210
dynamicParameters = TemplateUtility . GetTemplateParametersFromFile (
204
211
this . ResolvePath ( TemplateFile ) ,
205
212
TemplateParameterObject ,
206
213
TemplateParameterUri ,
207
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
214
+ staticParameterNames ) ;
208
215
}
209
216
}
210
217
else if ( ! string . IsNullOrEmpty ( TemplateUri ) &&
@@ -217,15 +224,15 @@ public object GetDynamicParameters()
217
224
TemplateUri ,
218
225
TemplateParameterObject ,
219
226
this . ResolvePath ( TemplateParameterFile ) ,
220
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
227
+ staticParameterNames ) ;
221
228
}
222
229
else
223
230
{
224
231
dynamicParameters = TemplateUtility . GetTemplateParametersFromFile (
225
232
TemplateUri ,
226
233
TemplateParameterObject ,
227
234
TemplateParameterUri ,
228
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
235
+ staticParameterNames ) ;
229
236
}
230
237
}
231
238
else if ( ! string . IsNullOrEmpty ( TemplateSpecId ) &&
@@ -238,28 +245,41 @@ public object GetDynamicParameters()
238
245
throw new PSArgumentException ( "No version found in Resource ID" ) ;
239
246
}
240
247
241
- var templateSpecVersion = TemplateSpecsSdkClient . GetTemplateSpec (
242
- ResourceIdUtility . GetResourceName ( templateSpecId ) . Split ( '/' ) [ 0 ] ,
248
+ if ( ! string . IsNullOrEmpty ( resourceIdentifier . Subscription ) &&
249
+ TemplateSpecsClient . SubscriptionId != resourceIdentifier . Subscription )
250
+ {
251
+ // The template spec is in a different subscription than our default
252
+ // context. Force the client to use that subscription:
253
+ TemplateSpecsClient . SubscriptionId = resourceIdentifier . Subscription ;
254
+ }
255
+
256
+ var templateSpecVersion = TemplateSpecsClient . TemplateSpecVersions . Get (
243
257
ResourceIdUtility . GetResourceGroupName ( templateSpecId ) ,
244
- resourceIdentifier . ResourceName ) . Versions . Single ( ) ;
258
+ ResourceIdUtility . GetResourceName ( templateSpecId ) . Split ( '/' ) [ 0 ] ,
259
+ resourceIdentifier . ResourceName ) ;
260
+
261
+ if ( ! ( templateSpecVersion . Template is JObject ) )
262
+ {
263
+ throw new InvalidOperationException ( "Unexpected type." ) ; // Sanity check
264
+ }
245
265
246
- var templateObj = JObject . Parse ( templateSpecVersion . Template ) ;
266
+ JObject templateObj = ( JObject ) templateSpecVersion . Template ;
247
267
248
268
if ( string . IsNullOrEmpty ( TemplateParameterUri ) )
249
269
{
250
270
dynamicParameters = TemplateUtility . GetTemplateParametersFromFile (
251
271
templateObj ,
252
272
TemplateParameterObject ,
253
273
this . ResolvePath ( TemplateParameterFile ) ,
254
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
274
+ staticParameterNames ) ;
255
275
}
256
276
else
257
277
{
258
278
dynamicParameters = TemplateUtility . GetTemplateParametersFromFile (
259
279
templateObj ,
260
280
TemplateParameterObject ,
261
281
TemplateParameterUri ,
262
- MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ) ;
282
+ staticParameterNames ) ;
263
283
}
264
284
}
265
285
}
@@ -345,5 +365,39 @@ protected string GetDeploymentDebugLogLevel(string deploymentDebugLogLevel)
345
365
346
366
return debugSetting ;
347
367
}
368
+
369
+ /// <summary>
370
+ /// Gets the names of the static parameters defined for this cmdlet.
371
+ /// </summary>
372
+ protected string [ ] GetStaticParameterNames ( )
373
+ {
374
+ if ( MyInvocation . MyCommand != null )
375
+ {
376
+ // We're running inside the shell... parameter information will already
377
+ // be resolved for us:
378
+ return MyInvocation . MyCommand . Parameters . Keys . ToArray ( ) ;
379
+ }
380
+
381
+ // This invocation is internal (e.g: through a unit test), fallback to
382
+ // collecting the command/parameter info explicitly from our current type:
383
+
384
+ CmdletAttribute cmdletAttribute = ( CmdletAttribute ) this . GetType ( )
385
+ . GetCustomAttributes ( typeof ( CmdletAttribute ) , true )
386
+ . FirstOrDefault ( ) ;
387
+
388
+ if ( cmdletAttribute == null )
389
+ {
390
+ throw new InvalidOperationException (
391
+ $ "Expected type '{ this . GetType ( ) . Name } ' to have CmdletAttribute."
392
+ ) ;
393
+ }
394
+
395
+ // The command name we provide for the temporary CmdletInfo isn't consumed
396
+ // anywhere other than instantiation, but let's resolve it anyway:
397
+ string commandName = $ "{ cmdletAttribute . VerbName } -{ cmdletAttribute . NounName } ";
398
+
399
+ CmdletInfo cmdletInfo = new CmdletInfo ( commandName , this . GetType ( ) ) ;
400
+ return cmdletInfo . Parameters . Keys . ToArray ( ) ;
401
+ }
348
402
}
349
403
}
0 commit comments