From ae016a1f250f08f65ae045735f197629492c812a Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 18 Jan 2017 13:03:05 +0100 Subject: [PATCH] simplify code flow: skip the step if no run_volume_tags provided --- builder/amazon/ebs/step_tag_ebs_volumes.go | 47 +++++++++++----------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go index ecd25e4f0..a9ead9ebe 100644 --- a/builder/amazon/ebs/step_tag_ebs_volumes.go +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -18,34 +18,35 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { instance := state.Get("instance").(*ec2.Instance) ui := state.Get("ui").(packer.Ui) - if len(s.VolumeRunTags) > 0 { + if len(s.VolumeRunTags) == 0 { + return multistep.ActionContinue + } - volumeIds := make([]*string, 0) - for _, v := range instance.BlockDeviceMappings { - if ebs := v.Ebs; ebs != nil { - volumeIds = append(volumeIds, ebs.VolumeId) - } + volumeIds := make([]*string, 0) + for _, v := range instance.BlockDeviceMappings { + if ebs := v.Ebs; ebs != nil { + volumeIds = append(volumeIds, ebs.VolumeId) } + } - if len(volumeIds) == 0 { - return multistep.ActionContinue - } + if len(volumeIds) == 0 { + return multistep.ActionContinue + } - ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) - tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) + ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) + tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) - _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ - Resources: []*string{ - instance.BlockDeviceMappings[0].Ebs.VolumeId, - }, - Tags: tags, - }) - if err != nil { - err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ + Resources: []*string{ + instance.BlockDeviceMappings[0].Ebs.VolumeId, + }, + Tags: tags, + }) + if err != nil { + err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } return multistep.ActionContinue