github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/amazon/common/block_device.go (about) 1 package common 2 3 import ( 4 "strings" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/mitchellh/packer/template/interpolate" 9 ) 10 11 // BlockDevice 12 type BlockDevice struct { 13 DeleteOnTermination bool `mapstructure:"delete_on_termination"` 14 DeviceName string `mapstructure:"device_name"` 15 Encrypted bool `mapstructure:"encrypted"` 16 IOPS int64 `mapstructure:"iops"` 17 NoDevice bool `mapstructure:"no_device"` 18 SnapshotId string `mapstructure:"snapshot_id"` 19 VirtualName string `mapstructure:"virtual_name"` 20 VolumeType string `mapstructure:"volume_type"` 21 VolumeSize int64 `mapstructure:"volume_size"` 22 } 23 24 type BlockDevices struct { 25 AMIBlockDevices `mapstructure:",squash"` 26 LaunchBlockDevices `mapstructure:",squash"` 27 } 28 29 type AMIBlockDevices struct { 30 AMIMappings []BlockDevice `mapstructure:"ami_block_device_mappings"` 31 } 32 33 type LaunchBlockDevices struct { 34 LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"` 35 } 36 37 func buildBlockDevices(b []BlockDevice) []*ec2.BlockDeviceMapping { 38 var blockDevices []*ec2.BlockDeviceMapping 39 40 for _, blockDevice := range b { 41 mapping := &ec2.BlockDeviceMapping{ 42 DeviceName: aws.String(blockDevice.DeviceName), 43 } 44 45 if blockDevice.NoDevice { 46 mapping.NoDevice = aws.String("") 47 } else if blockDevice.VirtualName != "" { 48 if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") { 49 mapping.VirtualName = aws.String(blockDevice.VirtualName) 50 } 51 } else { 52 ebsBlockDevice := &ec2.EbsBlockDevice{ 53 DeleteOnTermination: aws.Bool(blockDevice.DeleteOnTermination), 54 } 55 56 if blockDevice.VolumeType != "" { 57 ebsBlockDevice.VolumeType = aws.String(blockDevice.VolumeType) 58 } 59 60 if blockDevice.VolumeSize > 0 { 61 ebsBlockDevice.VolumeSize = aws.Int64(blockDevice.VolumeSize) 62 } 63 64 // IOPS is only valid for io1 type 65 if blockDevice.VolumeType == "io1" { 66 ebsBlockDevice.Iops = aws.Int64(blockDevice.IOPS) 67 } 68 69 // You cannot specify Encrypted if you specify a Snapshot ID 70 if blockDevice.SnapshotId != "" { 71 ebsBlockDevice.SnapshotId = aws.String(blockDevice.SnapshotId) 72 } else if blockDevice.Encrypted { 73 ebsBlockDevice.Encrypted = aws.Bool(blockDevice.Encrypted) 74 } 75 76 mapping.Ebs = ebsBlockDevice 77 } 78 79 blockDevices = append(blockDevices, mapping) 80 } 81 return blockDevices 82 } 83 84 func (b *BlockDevices) Prepare(ctx *interpolate.Context) []error { 85 return nil 86 } 87 88 func (b *AMIBlockDevices) BuildAMIDevices() []*ec2.BlockDeviceMapping { 89 return buildBlockDevices(b.AMIMappings) 90 } 91 92 func (b *LaunchBlockDevices) BuildLaunchDevices() []*ec2.BlockDeviceMapping { 93 return buildBlockDevices(b.LaunchMappings) 94 }