simplify code flow: skip the step if no run_volume_tags provided

This commit is contained in:
Roman Zhuzha 2017-01-18 13:03:05 +01:00
parent a65af51a4b
commit ae016a1f25
1 changed files with 24 additions and 23 deletions

View File

@ -18,34 +18,35 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction {
instance := state.Get("instance").(*ec2.Instance) instance := state.Get("instance").(*ec2.Instance)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if len(s.VolumeRunTags) > 0 { if len(s.VolumeRunTags) == 0 {
return multistep.ActionContinue
}
volumeIds := make([]*string, 0) volumeIds := make([]*string, 0)
for _, v := range instance.BlockDeviceMappings { for _, v := range instance.BlockDeviceMappings {
if ebs := v.Ebs; ebs != nil { if ebs := v.Ebs; ebs != nil {
volumeIds = append(volumeIds, ebs.VolumeId) volumeIds = append(volumeIds, ebs.VolumeId)
}
} }
}
if len(volumeIds) == 0 { if len(volumeIds) == 0 {
return multistep.ActionContinue return multistep.ActionContinue
} }
ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:"))
tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui) tags := common.ConvertToEC2Tags(s.VolumeRunTags, ui)
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{ Resources: []*string{
instance.BlockDeviceMappings[0].Ebs.VolumeId, instance.BlockDeviceMappings[0].Ebs.VolumeId,
}, },
Tags: tags, Tags: tags,
}) })
if err != nil { if err != nil {
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
}
} }
return multistep.ActionContinue return multistep.ActionContinue