Skip to content

Commit 81e86e6

Browse files
authored
Fixed invalid ts deployment error message (#14236)
* Fixed invalid ts deployment error message and added support for -TemplateParameterObject in TS deployments * Update help files for new parameter set * review fixes
1 parent c1a7cbe commit 81e86e6

File tree

8 files changed

+354
-4410
lines changed

8 files changed

+354
-4410
lines changed

src/Resources/ResourceManager/Implementation/CmdletBase/ResourceWithParameterCmdletBase.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using System.Management.Automation;
20+
using System.Net;
2021
using Microsoft.Azure.Commands.Common.Authentication;
2122
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2223
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2324
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
2425
using Microsoft.Azure.Management.ResourceManager;
26+
using Microsoft.Azure.Management.ResourceManager.Models;
2527
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2628
using Newtonsoft.Json.Linq;
2729

@@ -48,6 +50,7 @@ public abstract class ResourceWithParameterCmdletBase : ResourceManagerCmdletBas
4850
protected const string TemplateSpecResourceIdParameterSetName = "ByTemplateSpecResourceId";
4951
protected const string TemplateSpecResourceIdParameterFileParameterSetName = "ByTemplateSpecResourceIdAndParams";
5052
protected const string TemplateSpecResourceIdParameterUriParameterSetName = "ByTemplateSpecResourceIdAndParamsUri";
53+
protected const string TemplateSpecResourceIdParameterObjectParameterSetName = "ByTemplateSpecResourceIdAndParamsObject";
5154

5255
protected RuntimeDefinedParameterDictionary dynamicParameters;
5356

@@ -74,6 +77,8 @@ protected ResourceWithParameterCmdletBase()
7477
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "A hash table which represents the parameters.")]
7578
[Parameter(ParameterSetName = TemplateUriParameterObjectParameterSetName,
7679
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.")]
7782
public Hashtable TemplateParameterObject { get; set; }
7883

7984
[Parameter(ParameterSetName = TemplateObjectParameterFileParameterSetName,
@@ -137,6 +142,8 @@ protected ResourceWithParameterCmdletBase()
137142
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Resource ID of the templateSpec to be deployed.")]
138143
[Parameter(ParameterSetName = TemplateSpecResourceIdParameterFileParameterSetName,
139144
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.")]
140147
[ValidateNotNullOrEmpty]
141148
public string TemplateSpecId { get; set; }
142149

@@ -261,19 +268,32 @@ public virtual object GetDynamicParameters()
261268
// context. Force the client to use that subscription:
262269
TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription;
263270
}
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);
264278

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)
271286
{
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;
273295
}
274296

275-
JObject templateObj = (JObject)templateSpecVersion.Template;
276-
277297
if (string.IsNullOrEmpty(TemplateParameterUri))
278298
{
279299
dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(

src/Resources/Resources.Test/ScenarioTests/DeploymentTests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ function Test-NewFailedSubscriptionDeploymentFromTemplateSpec
199199
$sampleTemplateJson = Get-Content -Raw -Path "subscription_level_template.json"
200200
$basicCreatedTemplateSpec = New-AzTemplateSpec -ResourceGroupName $rgname -Name $rname -Location $rgLocation -Version "v1" -TemplateJson $sampleTemplateJson
201201

202-
$resourceId = $basicCreatedTemplateSpec.Id + "/versions/v1"
202+
$resourceId = $basicCreatedTemplateSpec.Id + "/versions/v2"
203203

204204
#Create deployment
205205
try {
206206
$deployment = New-AzSubscriptionDeployment -Name $rname -TemplateSpecId $resourceId -TemplateParameterFile "subscription_level_parameters.json" -Location $rglocation
207207
}
208208
Catch {
209-
Assert-True { $Error[0].Contains("ResourceNotFound")}
209+
Assert-true { $Error[0].exception.message.Contains("InvalidTemplateSpec") }
210210
}
211211

212212
}

0 commit comments

Comments
 (0)