Skip to content

Commit

Permalink
Merge pull request #43 from chhaj5236/feature/support_tags
Browse files Browse the repository at this point in the history
support adding tags to image
  • Loading branch information
chhaj5236 authored Sep 17, 2018
2 parents b5cb1c9 + 47b80a8 commit 6ef2e98
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.3.0 (Unreleased)

## 1.2.2 (September 16, 2018)

- Support adding tags to image ([#43](https://github.com/alibaba/packer-provider/pull/43))

## 1.2.1 (September 11, 2018)

IMPROVEMENTS:
Expand Down
3 changes: 3 additions & 0 deletions ecs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
AlicloudImageName: b.config.AlicloudImageName,
},
&stepCreateAlicloudImage{},
&stepCreateTags{
Tags: b.config.AlicloudImageTags,
},
&stepRegionCopyAlicloudImage{
AlicloudImageDestinationRegions: b.config.AlicloudImageDestinationRegions,
AlicloudImageDestinationNames: b.config.AlicloudImageDestinationNames,
Expand Down
81 changes: 81 additions & 0 deletions ecs/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ func TestBuilderAcc_forceDeleteSnapshot(t *testing.T) {
})
}

func TestBuilderAcc_imageTags(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Builder: &Builder{},
Template: testBuilderAccImageTags,
Check: checkImageTags(),
})

}

func checkSnapshotsDeleted(snapshotIds []string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error {
// Verify the snapshots are gone
Expand Down Expand Up @@ -207,6 +219,58 @@ func checkRegionCopy(regions []string) builderT.TestCheckFunc {
}
}

func checkImageTags() builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error {
if len(artifacts) > 1 {
return fmt.Errorf("more than 1 artifact")
}

// Get the actual *Artifact pointer so we can access the AMIs directly
artifactRaw := artifacts[0]
artifact, ok := artifactRaw.(*Artifact)
if !ok {
return fmt.Errorf("unknown artifact: %#v", artifactRaw)
}

// describe the image, get block devices with a snapshot
client, _ := testAliyunClient()
tags, _, err := client.DescribeTags(
&ecs.DescribeTagsArgs{
RegionId: "cn-beijing",
ResourceType: ecs.TagResourceImage,
ResourceId: artifact.AlicloudImages["cn-beijing"],
})

if err != nil {
return fmt.Errorf("Error retrieving Image Attributes for ECS Image Artifact (%#v) "+
"in ECS Image Tags Test: %s", artifact, err)
}

failed := false
if len(tags) != 2 {
failed = true
}

if !failed {
for i := 0; i < len(tags); i++ {
if tags[i].TagKey == "TagKey1" && tags[i].TagValue != "TagValue1" {
failed = true
} else if tags[i].TagKey == "TagKey2" && tags[i].TagValue != "TagValue2" {
failed = true
} else if tags[i].TagKey != "TagKey1" && tags[i].TagKey != "TagKey2" {
failed = true
}
}
}

if failed {
return fmt.Errorf("tags is not correctly set %#v", tags)
}

return nil
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("ALICLOUD_ACCESS_KEY"); v == "" {
t.Fatal("ALICLOUD_ACCESS_KEY must be set for acceptance tests")
Expand Down Expand Up @@ -306,6 +370,23 @@ const testBuilderAccSharing = `
}
`

const testBuilderAccImageTags = `
{ "builders": [{
"type": "test",
"region": "cn-beijing",
"instance_type": "ecs.n1.tiny",
"source_image":"ubuntu_16_0402_64_20G_alibase_20180409.vhd",
"ssh_username": "ubuntu",
"io_optimized":"true",
"ssh_username":"root",
"image_name": "packer-test_{{timestamp}}",
"tags": {
"TagKey1": "TagValue1",
"TagKey2": "TagValue2"
}
}]
}`

func buildForceDeregisterConfig(val, name string) string {
return fmt.Sprintf(testBuilderAccForceDelete, val, name)
}
Expand Down
23 changes: 12 additions & 11 deletions ecs/image_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ type AlicloudDiskDevices struct {
}

type AlicloudImageConfig struct {
AlicloudImageName string `mapstructure:"image_name"`
AlicloudImageVersion string `mapstructure:"image_version"`
AlicloudImageDescription string `mapstructure:"image_description"`
AlicloudImageShareAccounts []string `mapstructure:"image_share_account"`
AlicloudImageUNShareAccounts []string `mapstructure:"image_unshare_account"`
AlicloudImageDestinationRegions []string `mapstructure:"image_copy_regions"`
AlicloudImageDestinationNames []string `mapstructure:"image_copy_names"`
AlicloudImageForceDelete bool `mapstructure:"image_force_delete"`
AlicloudImageForceDeleteSnapshots bool `mapstructure:"image_force_delete_snapshots"`
AlicloudImageForceDeleteInstances bool `mapstructure:"image_force_delete_instances"`
AlicloudImageSkipRegionValidation bool `mapstructure:"skip_region_validation"`
AlicloudImageName string `mapstructure:"image_name"`
AlicloudImageVersion string `mapstructure:"image_version"`
AlicloudImageDescription string `mapstructure:"image_description"`
AlicloudImageShareAccounts []string `mapstructure:"image_share_account"`
AlicloudImageUNShareAccounts []string `mapstructure:"image_unshare_account"`
AlicloudImageDestinationRegions []string `mapstructure:"image_copy_regions"`
AlicloudImageDestinationNames []string `mapstructure:"image_copy_names"`
AlicloudImageForceDelete bool `mapstructure:"image_force_delete"`
AlicloudImageForceDeleteSnapshots bool `mapstructure:"image_force_delete_snapshots"`
AlicloudImageForceDeleteInstances bool `mapstructure:"image_force_delete_instances"`
AlicloudImageSkipRegionValidation bool `mapstructure:"skip_region_validation"`
AlicloudImageTags map[string]string `mapstructure:"tags"`
AlicloudDiskDevices `mapstructure:",squash"`
}

Expand Down
20 changes: 20 additions & 0 deletions ecs/image_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ func TestAMIConfigPrepare_regions(t *testing.T) {

}

func TestECSImageConfigPrepare_imageTags(t *testing.T) {
c := testAlicloudImageConfig()
c.AlicloudImageTags = map[string]string{
"TagKey1": "TagValue1",
"TagKey2": "TagValue2",
}

if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}

if len(c.AlicloudImageTags) != 2 || c.AlicloudImageTags["TagKey1"] != "TagValue1" ||
c.AlicloudImageTags["TagKey2"] != "TagValue2" {
t.Fatalf("invalid value, expected: %s, actual: %s", map[string]string{
"TagKey1": "TagValue1",
"TagKey2": "TagValue2",
}, c.AlicloudImageTags)
}
}

func regionsToString() []string {
var regions []string
for _, region := range common.ValidRegions {
Expand Down
48 changes: 48 additions & 0 deletions ecs/step_create_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ecs

import (
"context"
"fmt"

"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)

type stepCreateTags struct {
Tags map[string]string
}

func (s *stepCreateTags) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(Config)
client := state.Get("client").(*ecs.Client)
ui := state.Get("ui").(packer.Ui)
imageId := state.Get("alicloudimage").(string)

if len(s.Tags) == 0 {
return multistep.ActionContinue
}

ui.Say(fmt.Sprintf("Adding tags(%s) to image: %s", s.Tags, imageId))

err := client.AddTags(&ecs.AddTagsArgs{
ResourceId: imageId,
ResourceType: ecs.TagResourceImage,
RegionId: common.Region(config.AlicloudRegion),
Tag: s.Tags,
})

if err != nil {
err := fmt.Errorf("Error Adding tags to image: %s", err)
state.Put("error", err)
ui.Say(err.Error())
return multistep.ActionHalt
}

return multistep.ActionContinue
}

func (s *stepCreateTags) Cleanup(state multistep.StateBag) {
// Nothing need to do, tags will be cleaned when the resource is cleaned
}

0 comments on commit 6ef2e98

Please sign in to comment.