17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
19
using System . Management . Automation ;
20
+ using System . Net ;
20
21
using Microsoft . Azure . Commands . Common . Authentication ;
21
22
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
22
23
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Components ;
23
24
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Utilities ;
24
25
using Microsoft . Azure . Management . ResourceManager ;
26
+ using Microsoft . Azure . Management . ResourceManager . Models ;
25
27
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
26
28
using Newtonsoft . Json . Linq ;
27
29
@@ -48,6 +50,7 @@ public abstract class ResourceWithParameterCmdletBase : ResourceManagerCmdletBas
48
50
protected const string TemplateSpecResourceIdParameterSetName = "ByTemplateSpecResourceId" ;
49
51
protected const string TemplateSpecResourceIdParameterFileParameterSetName = "ByTemplateSpecResourceIdAndParams" ;
50
52
protected const string TemplateSpecResourceIdParameterUriParameterSetName = "ByTemplateSpecResourceIdAndParamsUri" ;
53
+ protected const string TemplateSpecResourceIdParameterObjectParameterSetName = "ByTemplateSpecResourceIdAndParamsObject" ;
51
54
52
55
protected RuntimeDefinedParameterDictionary dynamicParameters ;
53
56
@@ -74,6 +77,8 @@ protected ResourceWithParameterCmdletBase()
74
77
Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "A hash table which represents the parameters." ) ]
75
78
[ Parameter ( ParameterSetName = TemplateUriParameterObjectParameterSetName ,
76
79
Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "A hash table which represents the parameters." ) ]
80
+ [ Parameter ( ParameterSetName = TemplateSpecResourceIdParameterObjectParameterSetName ,
81
+ Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "A hash table which represents the parameters." ) ]
77
82
public Hashtable TemplateParameterObject { get ; set ; }
78
83
79
84
[ Parameter ( ParameterSetName = TemplateObjectParameterFileParameterSetName ,
@@ -137,6 +142,8 @@ protected ResourceWithParameterCmdletBase()
137
142
Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "Resource ID of the templateSpec to be deployed." ) ]
138
143
[ Parameter ( ParameterSetName = TemplateSpecResourceIdParameterFileParameterSetName ,
139
144
Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "Resource ID of the templateSpec to be deployed." ) ]
145
+ [ Parameter ( ParameterSetName = TemplateSpecResourceIdParameterObjectParameterSetName ,
146
+ Mandatory = true , ValueFromPipelineByPropertyName = true , HelpMessage = "Resource ID of the templateSpec to be deployed." ) ]
140
147
[ ValidateNotNullOrEmpty ]
141
148
public string TemplateSpecId { get ; set ; }
142
149
@@ -261,19 +268,32 @@ public virtual object GetDynamicParameters()
261
268
// context. Force the client to use that subscription:
262
269
TemplateSpecsClient . SubscriptionId = resourceIdentifier . Subscription ;
263
270
}
271
+ JObject templateObj = ( JObject ) null ;
272
+ try
273
+ {
274
+ var templateSpecVersion = TemplateSpecsClient . TemplateSpecVersions . Get (
275
+ ResourceIdUtility . GetResourceGroupName ( templateSpecId ) ,
276
+ ResourceIdUtility . GetResourceName ( templateSpecId ) . Split ( '/' ) [ 0 ] ,
277
+ resourceIdentifier . ResourceName ) ;
264
278
265
- var templateSpecVersion = TemplateSpecsClient . TemplateSpecVersions . Get (
266
- ResourceIdUtility . GetResourceGroupName ( templateSpecId ) ,
267
- ResourceIdUtility . GetResourceName ( templateSpecId ) . Split ( '/' ) [ 0 ] ,
268
- resourceIdentifier . ResourceName ) ;
269
-
270
- if ( ! ( templateSpecVersion . Template is JObject ) )
279
+ if ( ! ( templateSpecVersion . Template is JObject ) )
280
+ {
281
+ throw new InvalidOperationException ( "Unexpected type." ) ; // Sanity check
282
+ }
283
+ templateObj = ( JObject ) templateSpecVersion . Template ;
284
+ }
285
+ catch ( TemplateSpecsErrorException e )
271
286
{
272
- throw new InvalidOperationException ( "Unexpected type." ) ; // Sanity check
287
+ //If the templateSpec resourceID is pointing to a non existant resource
288
+ if ( e . Response . StatusCode . Equals ( HttpStatusCode . NotFound ) )
289
+ {
290
+ //By returning null, we are introducing parity in the way templateURI and templateSpecId are validated. Gives a cleaner error message in line with the error message for invalid templateURI
291
+ return null ;
292
+ }
293
+ //Throw for any other error that is not due to a 404 for the template resource.
294
+ throw ;
273
295
}
274
296
275
- JObject templateObj = ( JObject ) templateSpecVersion . Template ;
276
-
277
297
if ( string . IsNullOrEmpty ( TemplateParameterUri ) )
278
298
{
279
299
dynamicParameters = TemplateUtility . GetTemplateParametersFromFile (
0 commit comments