github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/amazon/ebssurrogate/root_block_device.go (about)

     1  package ebssurrogate
     2  
     3  import (
     4  	"errors"
     5  	"github.com/aws/aws-sdk-go/aws"
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/hashicorp/packer/template/interpolate"
     8  )
     9  
    10  type RootBlockDevice struct {
    11  	SourceDeviceName    string `mapstructure:"source_device_name"`
    12  	DeviceName          string `mapstructure:"device_name"`
    13  	DeleteOnTermination bool   `mapstructure:"delete_on_termination"`
    14  	IOPS                int64  `mapstructure:"iops"`
    15  	VolumeType          string `mapstructure:"volume_type"`
    16  	VolumeSize          int64  `mapstructure:"volume_size"`
    17  }
    18  
    19  func (c *RootBlockDevice) Prepare(ctx *interpolate.Context) []error {
    20  	var errs []error
    21  
    22  	if c.SourceDeviceName == "" {
    23  		errs = append(errs, errors.New("source_device_name for the root_device must be specified"))
    24  	}
    25  
    26  	if c.DeviceName == "" {
    27  		errs = append(errs, errors.New("device_name for the root_device must be specified"))
    28  	}
    29  
    30  	if c.VolumeType == "gp2" && c.IOPS != 0 {
    31  		errs = append(errs, errors.New("iops may not be specified for a gp2 volume"))
    32  	}
    33  
    34  	if c.IOPS < 0 {
    35  		errs = append(errs, errors.New("iops must be greater than 0"))
    36  	}
    37  
    38  	if c.VolumeSize < 0 {
    39  		errs = append(errs, errors.New("volume_size must be greater than 0"))
    40  	}
    41  
    42  	if len(errs) > 0 {
    43  		return errs
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (d *RootBlockDevice) createBlockDeviceMapping(snapshotId string) *ec2.BlockDeviceMapping {
    50  	rootBlockDevice := &ec2.EbsBlockDevice{
    51  		SnapshotId:          aws.String(snapshotId),
    52  		VolumeType:          aws.String(d.VolumeType),
    53  		VolumeSize:          aws.Int64(d.VolumeSize),
    54  		DeleteOnTermination: aws.Bool(d.DeleteOnTermination),
    55  	}
    56  
    57  	if d.IOPS != 0 {
    58  		rootBlockDevice.Iops = aws.Int64(d.IOPS)
    59  	}
    60  
    61  	return &ec2.BlockDeviceMapping{
    62  		DeviceName: aws.String(d.DeviceName),
    63  		Ebs:        rootBlockDevice,
    64  	}
    65  }