Skip to content

Fixed invalid ts deployment error message #14236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Net;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json.Linq;

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

protected RuntimeDefinedParameterDictionary dynamicParameters;

Expand All @@ -74,6 +77,8 @@ protected ResourceWithParameterCmdletBase()
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "A hash table which represents the parameters.")]
[Parameter(ParameterSetName = TemplateUriParameterObjectParameterSetName,
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "A hash table which represents the parameters.")]
[Parameter(ParameterSetName = TemplateSpecResourceIdParameterObjectParameterSetName,
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "A hash table which represents the parameters.")]
public Hashtable TemplateParameterObject { get; set; }

[Parameter(ParameterSetName = TemplateObjectParameterFileParameterSetName,
Expand Down Expand Up @@ -137,6 +142,8 @@ protected ResourceWithParameterCmdletBase()
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Resource ID of the templateSpec to be deployed.")]
[Parameter(ParameterSetName = TemplateSpecResourceIdParameterFileParameterSetName,
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Resource ID of the templateSpec to be deployed.")]
[Parameter(ParameterSetName = TemplateSpecResourceIdParameterObjectParameterSetName,
Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Resource ID of the templateSpec to be deployed.")]
[ValidateNotNullOrEmpty]
public string TemplateSpecId { get; set; }

Expand Down Expand Up @@ -261,19 +268,32 @@ public virtual object GetDynamicParameters()
// context. Force the client to use that subscription:
TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription;
}
JObject templateObj = (JObject)null;
try
{
var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get(
ResourceIdUtility.GetResourceGroupName(templateSpecId),
ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0],
resourceIdentifier.ResourceName);

var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get(
ResourceIdUtility.GetResourceGroupName(templateSpecId),
ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0],
resourceIdentifier.ResourceName);

if (!(templateSpecVersion.Template is JObject))
if (!(templateSpecVersion.Template is JObject))
{
throw new InvalidOperationException("Unexpected type."); // Sanity check
}
templateObj = (JObject)templateSpecVersion.Template;
}
catch (TemplateSpecsErrorException e)
{
throw new InvalidOperationException("Unexpected type."); // Sanity check
//If the templateSpec resourceID is pointing to a non existant resource
if(e.Response.StatusCode.Equals(HttpStatusCode.NotFound))
{
//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
return null;
}
//Throw for any other error that is not due to a 404 for the template resource.
throw;
}

JObject templateObj = (JObject)templateSpecVersion.Template;

if (string.IsNullOrEmpty(TemplateParameterUri))
{
dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ function Test-NewFailedSubscriptionDeploymentFromTemplateSpec
$sampleTemplateJson = Get-Content -Raw -Path "subscription_level_template.json"
$basicCreatedTemplateSpec = New-AzTemplateSpec -ResourceGroupName $rgname -Name $rname -Location $rgLocation -Version "v1" -TemplateJson $sampleTemplateJson

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

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

}
Expand Down
Loading