github.com/rothwerx/packer@v0.9.0/builder/amazon/ebs/step_tag_ebs_volumes.go (about)

     1  package ebs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  type stepTagEBSVolumes struct {
    12  	VolumeRunTags map[string]string
    13  }
    14  
    15  func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction {
    16  	ec2conn := state.Get("ec2").(*ec2.EC2)
    17  	instance := state.Get("instance").(*ec2.Instance)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	if len(s.VolumeRunTags) > 0 {
    21  		ui.Say("Tagging source EBS volumes...")
    22  
    23  		volumeIds := make([]*string, 0)
    24  		for _, v := range instance.BlockDeviceMappings {
    25  			if ebs := v.Ebs; ebs != nil {
    26  				volumeIds = append(volumeIds, ebs.VolumeId)
    27  			}
    28  		}
    29  
    30  		if len(volumeIds) == 0 {
    31  			return multistep.ActionContinue
    32  		}
    33  
    34  		tags := make([]*ec2.Tag, len(s.VolumeRunTags))
    35  		for key, value := range s.VolumeRunTags {
    36  			tags = append(tags, &ec2.Tag{Key: &key, Value: &value})
    37  		}
    38  
    39  		_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{
    40  			Resources: []*string{
    41  				instance.BlockDeviceMappings[0].Ebs.VolumeId,
    42  			},
    43  			Tags: tags,
    44  		})
    45  		if err != nil {
    46  			err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
    47  			state.Put("error", err)
    48  			ui.Error(err.Error())
    49  			return multistep.ActionHalt
    50  		}
    51  	}
    52  
    53  	return multistep.ActionContinue
    54  }
    55  
    56  func (s *stepTagEBSVolumes) Cleanup(state multistep.StateBag) {
    57  	// No cleanup...
    58  }