Skip to content
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

[Resources] Template Specs: Adding tag assignment/viewing support #13619

Merged
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 @@ -21,6 +21,7 @@
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.IO;
using System.Management.Automation;

Expand Down Expand Up @@ -98,6 +99,13 @@ public class NewAzTemplateSpec : TemplateSpecCmdletBase
[LocationCompleter("Microsoft.Resources/templateSpecs")]
public string Location { get; set; }

[Alias("Tags")]
[Parameter(
Mandatory = false,
HelpMessage = "Hashtable of tags for the new template spec resource(s).")]
[ValidateNotNull]
public Hashtable Tag { get; set; }

[Parameter(
ParameterSetName = FromJsonStringParameterSet,
Mandatory = true,
Expand Down Expand Up @@ -204,7 +212,9 @@ public override void ExecuteCmdlet()
packagedTemplate,
templateSpecDescription: Description,
templateSpecDisplayName: DisplayName,
versionDescription: VersionDescription
versionDescription: VersionDescription,
templateSpecTags:Tag, // Note: Only applied if template spec doesn't exist
versionTags: Tag
);

WriteObject(templateSpecVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.IO;
using System.Management.Automation;

Expand Down Expand Up @@ -120,6 +121,13 @@ public class SetAzTemplateSpec : TemplateSpecCmdletBase
[LocationCompleter("Microsoft.Resources/templateSpecs")]
public string Location { get; set; }

[Alias("Tags")]
[Parameter(
Mandatory = false,
HelpMessage = "Hashtable of tags for the template spec and/or version")]
[ValidateNotNull]
public Hashtable Tag { get; set; }

[Parameter(Mandatory = true, ParameterSetName = UpdateVersionByNameFromJsonParameterSet, ValueFromPipelineByPropertyName = true,
HelpMessage = "The Azure Resource Manager template JSON.")]
[Parameter(Mandatory = true, ParameterSetName = UpdateVersionByIdFromJsonParameterSet, ValueFromPipelineByPropertyName = true,
Expand Down Expand Up @@ -245,7 +253,9 @@ public override void ExecuteCmdlet()
packagedTemplate,
templateSpecDescription: Description,
templateSpecDisplayName: DisplayName,
versionDescription: VersionDescription
versionDescription: VersionDescription,
templateSpecTags: Tag, // Note: Only applied if template spec doesn't exist
versionTags: Tag
);

WriteObject(templateSpecVersion);
Expand All @@ -264,7 +274,8 @@ public override void ExecuteCmdlet()
Name,
Location,
templateSpecDescription: Description,
templateSpecDisplayName: DisplayName
templateSpecDisplayName: DisplayName,
tags: Tag
);

// As the root template spec is a seperate resource type, it won't contain version
Expand Down
51 changes: 46 additions & 5 deletions src/Resources/ResourceManager/SdkClient/TemplateSpecsSdkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
Expand Down Expand Up @@ -181,14 +183,18 @@ public PSTemplateSpec CreateOrUpdateTemplateSpecVersion(
PackagedTemplate packagedTemplate,
string templateSpecDisplayName = null,
string templateSpecDescription = null,
string versionDescription = null)
string versionDescription = null,
Hashtable templateSpecTags = null,
Hashtable versionTags = null)
{
var templateSpecModel = this.CreateOrUpdateTemplateSpecInternal(
resourceGroupName,
templateSpecName,
location,
templateSpecDisplayName,
templateSpecDescription
templateSpecDescription,
tags: templateSpecTags,
onlyApplyTagsOnCreate: true // Don't update tags if the template spec already exists
);

var existingTemplateSpecVersion = this.GetAzureSdkTemplateSpecVersion(
Expand All @@ -206,6 +212,31 @@ public PSTemplateSpec CreateOrUpdateTemplateSpecVersion(
Description = versionDescription ?? existingTemplateSpecVersion?.Description
};

// Handle our conditional tagging:
// ------------------------------------------

if (versionTags != null)
{
// Explicit version tags provided. Use them:
templateSpecVersionModel.Tags =
TagsConversionHelper.CreateTagDictionary(versionTags, true);
}
else if (existingTemplateSpecVersion != null)
{
// No tags were provided. The template spec version already exists
// ... keep the existing version's tags:
templateSpecVersionModel.Tags = existingTemplateSpecVersion.Tags;
}
else
{
// No tags were provided. The template spec version does not already exist
// ... inherit the tags present on the underlying template spec:
templateSpecVersionModel.Tags = templateSpecModel.Tags;
}

// Perform the actual version create/update:
// ------------------------------------------

templateSpecVersionModel = TemplateSpecsClient.TemplateSpecVersions.CreateOrUpdate(
resourceGroupName,
templateSpecName,
Expand All @@ -221,14 +252,17 @@ public PSTemplateSpec CreateOrUpdateTemplateSpec(
string templateSpecName,
string location,
string templateSpecDisplayName = null,
string templateSpecDescription = null)
string templateSpecDescription = null,
Hashtable tags = null)
{
var sdkTemplateSpecModel = this.CreateOrUpdateTemplateSpecInternal(
resourceGroupName,
templateSpecName,
location,
templateSpecDisplayName,
templateSpecDescription
templateSpecDescription,
tags,
onlyApplyTagsOnCreate: false // Apply tags on updates too
);

return new PSTemplateSpec(sdkTemplateSpecModel);
Expand Down Expand Up @@ -285,7 +319,9 @@ protected TemplateSpec CreateOrUpdateTemplateSpecInternal(
string templateSpecName,
string location,
string templateSpecDisplayName = null,
string templateSpecDescription = null)
string templateSpecDescription = null,
Hashtable tags = null,
bool onlyApplyTagsOnCreate = false)
{
var existingTemplateSpec = this.GetAzureSdkTemplateSpec(
resourceGroupName,
Expand Down Expand Up @@ -314,6 +350,11 @@ protected TemplateSpec CreateOrUpdateTemplateSpecInternal(
Tags = existingTemplateSpec?.Tags
};

if ((tags != null) && (existingTemplateSpec == null || !onlyApplyTagsOnCreate))
{
templateSpecModel.Tags = TagsConversionHelper.CreateTagDictionary(tags, true);
}

templateSpecModel = TemplateSpecsClient.TemplateSpecs.CreateOrUpdate(
resourceGroupName,
templateSpecName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ internal PSTemplateSpec(TemplateSpec templateSpec,
this.LastModifiedTime = templateSpec.SystemData.LastModifiedAt;
this.Description = templateSpec.Description;
this.DisplayName = templateSpec.DisplayName;
this.Tags = templateSpec.Tags;
this.Tags = templateSpec.Tags == null
? new Dictionary<string, string>()
: new Dictionary<string, string>(templateSpec.Tags);

this.Versions = versionModels?
.Select(v => PSTemplateSpecVersion.FromAzureSDKTemplateSpecVersion(v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ internal static PSTemplateSpecVersion FromAzureSDKTemplateSpecVersion(
LastModifiedTime = templateSpecVersion.SystemData.LastModifiedAt,
Name = templateSpecVersion.Name,
Description = templateSpecVersion.Description,
Tags = templateSpecVersion.Tags,
Tags = templateSpecVersion.Tags == null
? new Dictionary<string, string>()
: new Dictionary<string, string>(templateSpecVersion.Tags),
// Note: Cast is redundant, but present for clarity reasons:
Template = ((JToken)templateSpecVersion.Template).ToString()
};
Expand Down
64 changes: 64 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/TemplateSpecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,69 @@ public void TestTemplateSpecRemoval()
{
TestRunner.RunTestScript("Test-RemoveTemplateSpec");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NewTemplateSpec_Tags_AppliesTagsToBothTemplateSpecAndVersionIfNotExist()
{
TestRunner.RunTestScript("Test-NewAppliesTagsToBothTemplateSpecAndVersionIfNotExist");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NewTemplateSpec_Tags_AppliesTagsToOnlyVersionIfTemplateSpecExists()
{
TestRunner.RunTestScript("Test-NewAppliesTagsToOnlyVersionIfTemplateSpecExists");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NewTemplateSpec_Tags_AppliesInheritedTagsIfNoTagsSuppliedAndSpecExists()
{
TestRunner.RunTestScript("Test-NewAppliesInheritedTagsIfNoTagsSuppliedAndSpecExists");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NewTemplateSpec_Tags_RemovesTagsFromExistingVersionIfTagsExplicityEmpty()
{
TestRunner.RunTestScript("Test-NewRemovesTagsFromExistingVersionIfTagsExplicitlyEmpty");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetTemplateSpec_Tags_AppliesTagsToBothTemplateSpecAndVersionIfNotExist()
{
TestRunner.RunTestScript("Test-SetAppliesTagsToBothTemplateSpecAndVersionIfNotExist");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetTemplateSpec_Tags_AppliesTagsOnlyToVersionIfVersionSpecified()
{
TestRunner.RunTestScript("Test-SetAppliesTagsOnlyToVersionIfVersionSpecified");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetTemplateSpec_Tags_AppliesTagsToTemplateSpecIfNoVersionSpecified()
{
TestRunner.RunTestScript("Test-SetAppliesTagsToTemplateSpecIfNoVersionSpecified");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetTemplateSpec_Tags_AppliesInheritedTagsIfNewVersionAndNoTagsProvidedAndSpecExists()
{
TestRunner.RunTestScript("Test-SetAppliesInheritedTagsIfNewVersionAndNoTagsProvidedAndSpecExists");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetTemplateSpec_Tags_RemovesTagsIfTagsExplicitlyEmpty()
{
TestRunner.RunTestScript("Test-SetRemovesTagsIfTagsExplicitlyEmpty");
}

}
}
Loading