2013-08-15 17:05:08 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2015-07-15 20:07:36 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-08-15 17:05:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockDevice
|
|
|
|
type BlockDevice struct {
|
2014-04-30 18:43:11 -04:00
|
|
|
DeleteOnTermination bool `mapstructure:"delete_on_termination"`
|
2013-08-15 17:05:08 -04:00
|
|
|
DeviceName string `mapstructure:"device_name"`
|
2014-05-22 14:36:22 -04:00
|
|
|
Encrypted bool `mapstructure:"encrypted"`
|
2014-04-30 18:43:11 -04:00
|
|
|
IOPS int64 `mapstructure:"iops"`
|
|
|
|
NoDevice bool `mapstructure:"no_device"`
|
2013-08-15 17:05:08 -04:00
|
|
|
SnapshotId string `mapstructure:"snapshot_id"`
|
2014-04-30 18:43:11 -04:00
|
|
|
VirtualName string `mapstructure:"virtual_name"`
|
2013-08-15 17:05:08 -04:00
|
|
|
VolumeType string `mapstructure:"volume_type"`
|
|
|
|
VolumeSize int64 `mapstructure:"volume_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockDevices struct {
|
2016-08-12 19:29:55 -04:00
|
|
|
AMIBlockDevices `mapstructure:",squash"`
|
|
|
|
LaunchBlockDevices `mapstructure:",squash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AMIBlockDevices struct {
|
|
|
|
AMIMappings []BlockDevice `mapstructure:"ami_block_device_mappings"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LaunchBlockDevices struct {
|
2013-09-06 14:48:38 -04:00
|
|
|
LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
|
2013-08-15 17:05:08 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
func buildBlockDevices(b []BlockDevice) []*ec2.BlockDeviceMapping {
|
|
|
|
var blockDevices []*ec2.BlockDeviceMapping
|
2013-08-15 17:05:08 -04:00
|
|
|
|
|
|
|
for _, blockDevice := range b {
|
2015-10-18 14:00:05 -04:00
|
|
|
mapping := &ec2.BlockDeviceMapping{
|
2015-08-24 17:19:11 -04:00
|
|
|
DeviceName: aws.String(blockDevice.DeviceName),
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
2015-06-09 12:38:53 -04:00
|
|
|
|
2015-08-24 17:19:11 -04:00
|
|
|
if blockDevice.NoDevice {
|
|
|
|
mapping.NoDevice = aws.String("")
|
2015-10-18 14:05:21 -04:00
|
|
|
} else if blockDevice.VirtualName != "" {
|
|
|
|
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
|
|
|
|
mapping.VirtualName = aws.String(blockDevice.VirtualName)
|
|
|
|
}
|
2015-08-24 17:19:11 -04:00
|
|
|
} else {
|
|
|
|
ebsBlockDevice := &ec2.EbsBlockDevice{
|
|
|
|
DeleteOnTermination: aws.Bool(blockDevice.DeleteOnTermination),
|
|
|
|
}
|
2015-06-09 12:38:53 -04:00
|
|
|
|
2015-08-24 17:19:11 -04:00
|
|
|
if blockDevice.VolumeType != "" {
|
|
|
|
ebsBlockDevice.VolumeType = aws.String(blockDevice.VolumeType)
|
|
|
|
}
|
2015-06-09 12:38:53 -04:00
|
|
|
|
2015-08-24 17:19:11 -04:00
|
|
|
if blockDevice.VolumeSize > 0 {
|
|
|
|
ebsBlockDevice.VolumeSize = aws.Int64(blockDevice.VolumeSize)
|
|
|
|
}
|
2015-06-29 17:11:58 -04:00
|
|
|
|
2015-08-24 17:19:11 -04:00
|
|
|
// IOPS is only valid for io1 type
|
|
|
|
if blockDevice.VolumeType == "io1" {
|
|
|
|
ebsBlockDevice.Iops = aws.Int64(blockDevice.IOPS)
|
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2015-08-24 17:19:11 -04:00
|
|
|
// You cannot specify Encrypted if you specify a Snapshot ID
|
|
|
|
if blockDevice.SnapshotId != "" {
|
|
|
|
ebsBlockDevice.SnapshotId = aws.String(blockDevice.SnapshotId)
|
|
|
|
} else if blockDevice.Encrypted {
|
|
|
|
ebsBlockDevice.Encrypted = aws.Bool(blockDevice.Encrypted)
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping.Ebs = ebsBlockDevice
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
blockDevices = append(blockDevices, mapping)
|
2013-08-15 17:05:08 -04:00
|
|
|
}
|
|
|
|
return blockDevices
|
|
|
|
}
|
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
func (b *BlockDevices) Prepare(ctx *interpolate.Context) []error {
|
2014-09-05 15:38:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:29:55 -04:00
|
|
|
func (b *AMIBlockDevices) BuildAMIDevices() []*ec2.BlockDeviceMapping {
|
2013-08-15 17:05:08 -04:00
|
|
|
return buildBlockDevices(b.AMIMappings)
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:29:55 -04:00
|
|
|
func (b *LaunchBlockDevices) BuildLaunchDevices() []*ec2.BlockDeviceMapping {
|
2013-08-15 17:05:08 -04:00
|
|
|
return buildBlockDevices(b.LaunchMappings)
|
|
|
|
}
|