From 7704ff2f247ecbf4a652f6165dedafd3e01f6316 Mon Sep 17 00:00:00 2001 From: "bozhi.ch" Date: Mon, 26 Nov 2018 16:13:06 +0800 Subject: [PATCH] apply tags to relevant snapshots --- builder/alicloud/ecs/step_create_image.go | 28 ++++++++----------- builder/alicloud/ecs/step_create_snapshot.go | 4 --- builder/alicloud/ecs/step_create_tags.go | 21 +++++++++++--- .../source/docs/builders/alicloud-ecs.html.md | 2 +- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/builder/alicloud/ecs/step_create_image.go b/builder/alicloud/ecs/step_create_image.go index d80dca8ea..e9fb009b1 100644 --- a/builder/alicloud/ecs/step_create_image.go +++ b/builder/alicloud/ecs/step_create_image.go @@ -45,38 +45,34 @@ func (s *stepCreateAlicloudImage) Run(_ context.Context, state multistep.StateBa } if err != nil { - err := fmt.Errorf("Error creating image: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + return halt(state, err, "Error creating image") } err = client.WaitForImageReady(common.Region(config.AlicloudRegion), imageId, s.WaitSnapshotReadyTimeout) if err != nil { - err := fmt.Errorf("Timeout waiting for image to be created: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + return halt(state, err, "Timeout waiting for image to be created") } images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{ RegionId: common.Region(config.AlicloudRegion), ImageId: imageId}) if err != nil { - err := fmt.Errorf("Error querying created image: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + return halt(state, err, "Error querying created imaged") } if len(images) == 0 { - err := fmt.Errorf("Unable to find created image: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + return halt(state, err, "Unable to find created image") } + s.image = &images[0] + var snapshotIds = []string{} + for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping { + snapshotIds = append(snapshotIds, device.SnapshotId) + } + state.Put("alicloudimage", imageId) + state.Put("alicloudsnapshots", snapshotIds) + alicloudImages := make(map[string]string) alicloudImages[config.AlicloudRegion] = images[0].ImageId state.Put("alicloudimages", alicloudImages) diff --git a/builder/alicloud/ecs/step_create_snapshot.go b/builder/alicloud/ecs/step_create_snapshot.go index a9f5ae590..e1b0386c8 100644 --- a/builder/alicloud/ecs/step_create_snapshot.go +++ b/builder/alicloud/ecs/step_create_snapshot.go @@ -62,11 +62,7 @@ func (s *stepCreateAlicloudSnapshot) Run(_ context.Context, state multistep.Stat return halt(state, err, "Unable to find created snapshot") } s.snapshot = &snapshots[0] - state.Put("alicloudsnapshot", snapshotId) - alicloudSnapshots := make(map[string]string) - alicloudSnapshots[config.AlicloudRegion] = snapshotId - state.Put("alicloudsnapshots", alicloudSnapshots) return multistep.ActionContinue } diff --git a/builder/alicloud/ecs/step_create_tags.go b/builder/alicloud/ecs/step_create_tags.go index b9a22e5fe..75dc602ff 100644 --- a/builder/alicloud/ecs/step_create_tags.go +++ b/builder/alicloud/ecs/step_create_tags.go @@ -18,6 +18,8 @@ func (s *stepCreateTags) Run(_ context.Context, state multistep.StateBag) multis client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) imageId := state.Get("alicloudimage").(string) + snapshotIds := state.Get("alicloudsnapshots").([]string) + if len(s.Tags) == 0 { return multistep.ActionContinue } @@ -29,11 +31,22 @@ func (s *stepCreateTags) Run(_ context.Context, state multistep.StateBag) multis 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 halt(state, err, "Error Adding tags to image") } + + for _, snapshotId := range snapshotIds { + ui.Say(fmt.Sprintf("Adding tags(%s) to snapshot: %s", s.Tags, snapshotId)) + err = client.AddTags(&ecs.AddTagsArgs{ + ResourceId: snapshotId, + ResourceType: ecs.TagResourceSnapshot, + RegionId: common.Region(config.AlicloudRegion), + Tag: s.Tags, + }) + if err != nil { + return halt(state, err, "Error Adding tags to snapshot") + } + } + return multistep.ActionContinue } func (s *stepCreateTags) Cleanup(state multistep.StateBag) { diff --git a/website/source/docs/builders/alicloud-ecs.html.md b/website/source/docs/builders/alicloud-ecs.html.md index 0c743ca4b..8b7e117cf 100644 --- a/website/source/docs/builders/alicloud-ecs.html.md +++ b/website/source/docs/builders/alicloud-ecs.html.md @@ -258,7 +258,7 @@ builder. EIP. The default value is false. - `tags` (object of key/value strings) - Tags applied to the destination - image. + image and relevant snapshots. ## Basic Example