2019-01-28 18:11:57 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-07 12:10:16 -05:00
|
|
|
"log"
|
2019-01-28 18:11:57 -05:00
|
|
|
"strings"
|
|
|
|
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2020-08-20 21:37:09 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-01-28 18:11:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockDevice
|
|
|
|
type BlockDevice struct {
|
|
|
|
DeleteOnVmDeletion bool `mapstructure:"delete_on_vm_deletion"`
|
|
|
|
DeviceName string `mapstructure:"device_name"`
|
|
|
|
IOPS int64 `mapstructure:"iops"`
|
|
|
|
NoDevice bool `mapstructure:"no_device"`
|
|
|
|
SnapshotId string `mapstructure:"snapshot_id"`
|
|
|
|
VirtualName string `mapstructure:"virtual_name"`
|
|
|
|
VolumeType string `mapstructure:"volume_type"`
|
|
|
|
VolumeSize int64 `mapstructure:"volume_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockDevices struct {
|
|
|
|
OMIBlockDevices `mapstructure:",squash"`
|
|
|
|
LaunchBlockDevices `mapstructure:",squash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type OMIBlockDevices struct {
|
2019-02-04 18:12:47 -05:00
|
|
|
OMIMappings []BlockDevice `mapstructure:"omi_block_device_mappings"`
|
2019-01-28 18:11:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type LaunchBlockDevices struct {
|
|
|
|
LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:02:11 -04:00
|
|
|
func buildOscBlockDevicesImage(b []BlockDevice) []osc.BlockDeviceMappingImage {
|
|
|
|
var blockDevices []osc.BlockDeviceMappingImage
|
|
|
|
|
|
|
|
for _, blockDevice := range b {
|
|
|
|
mapping := osc.BlockDeviceMappingImage{
|
|
|
|
DeviceName: blockDevice.DeviceName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.VirtualName != "" {
|
|
|
|
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
|
|
|
|
mapping.VirtualDeviceName = blockDevice.VirtualName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bsu := osc.BsuToCreate{
|
|
|
|
DeleteOnVmDeletion: blockDevice.DeleteOnVmDeletion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.VolumeType != "" {
|
|
|
|
bsu.VolumeType = blockDevice.VolumeType
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.VolumeSize > 0 {
|
|
|
|
bsu.VolumeSize = int32(blockDevice.VolumeSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IOPS is only valid for io1 type
|
|
|
|
if blockDevice.VolumeType == "io1" {
|
|
|
|
bsu.Iops = int32(blockDevice.IOPS)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.SnapshotId != "" {
|
|
|
|
bsu.SnapshotId = blockDevice.SnapshotId
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping.Bsu = bsu
|
|
|
|
}
|
|
|
|
|
|
|
|
blockDevices = append(blockDevices, mapping)
|
|
|
|
}
|
|
|
|
return blockDevices
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:37:09 -04:00
|
|
|
func buildOscBlockDevicesVmCreation(b []BlockDevice) []osc.BlockDeviceMappingVmCreation {
|
|
|
|
log.Printf("[DEBUG] Launch Block Device %#v", b)
|
|
|
|
|
|
|
|
var blockDevices []osc.BlockDeviceMappingVmCreation
|
|
|
|
|
|
|
|
for _, blockDevice := range b {
|
|
|
|
mapping := osc.BlockDeviceMappingVmCreation{
|
|
|
|
DeviceName: blockDevice.DeviceName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.NoDevice {
|
|
|
|
mapping.NoDevice = ""
|
|
|
|
} else if blockDevice.VirtualName != "" {
|
|
|
|
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
|
|
|
|
mapping.VirtualDeviceName = blockDevice.VirtualName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bsu := osc.BsuToCreate{
|
|
|
|
DeleteOnVmDeletion: blockDevice.DeleteOnVmDeletion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.VolumeType != "" {
|
|
|
|
bsu.VolumeType = blockDevice.VolumeType
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.VolumeSize > 0 {
|
|
|
|
bsu.VolumeSize = int32(blockDevice.VolumeSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IOPS is only valid for io1 type
|
|
|
|
if blockDevice.VolumeType == "io1" {
|
|
|
|
bsu.Iops = int32(blockDevice.IOPS)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockDevice.SnapshotId != "" {
|
|
|
|
bsu.SnapshotId = blockDevice.SnapshotId
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping.Bsu = bsu
|
|
|
|
}
|
|
|
|
|
|
|
|
blockDevices = append(blockDevices, mapping)
|
|
|
|
}
|
|
|
|
return blockDevices
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:11:57 -05:00
|
|
|
func (b *BlockDevice) Prepare(ctx *interpolate.Context) error {
|
|
|
|
if b.DeviceName == "" {
|
|
|
|
return fmt.Errorf("The `device_name` must be specified " +
|
|
|
|
"for every device in the block device mapping.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
|
|
|
|
for _, d := range b.OMIMappings {
|
|
|
|
if err := d.Prepare(ctx); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("OMIMapping: %s", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range b.LaunchMappings {
|
|
|
|
if err := d.Prepare(ctx); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("LaunchMapping: %s", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:02:11 -04:00
|
|
|
func (b *OMIBlockDevices) BuildOscOMIDevices() []osc.BlockDeviceMappingImage {
|
|
|
|
return buildOscBlockDevicesImage(b.OMIMappings)
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:37:09 -04:00
|
|
|
func (b *LaunchBlockDevices) BuildOSCLaunchDevices() []osc.BlockDeviceMappingVmCreation {
|
|
|
|
return buildOscBlockDevicesVmCreation(b.LaunchMappings)
|
|
|
|
}
|