Skip to content

Commit

Permalink
Fix bsu builder
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Sep 2, 2024
1 parent d426cce commit 09ccf70
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 71 deletions.
2 changes: 2 additions & 0 deletions builder/bsu/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions builder/bsu/step_create_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
ui.Say(fmt.Sprintf("Creating OMI %s from vm %s", omiName, vm.GetVmId()))
blockDeviceMapping := config.BlockDevices.BuildOscOMIDevices()
createOpts := oscgo.CreateImageRequest{
VmId: vm.VmId,
ImageName: &omiName,
BlockDeviceMappings: &blockDeviceMapping,
ImageName: &omiName,
}
if config.OMIDescription != "" {
createOpts.Description = &config.OMIDescription
if len(blockDeviceMapping) == 0 {
createOpts.SetVmId(vm.GetVmId())
} else {
createOpts.SetBlockDeviceMappings(blockDeviceMapping)
if rootDName := config.RootDeviceName; rootDName != "" {
createOpts.SetRootDeviceName(rootDName)
} else {
err := fmt.Errorf("Error: MissingParameter: You must provide 'RootDeviceName' when creating omi with 'omi_block_device_mappings'.")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
if prodCode := config.ProductCodes; prodCode != nil {
createOpts.SetProductCodes(prodCode)
}

if description := config.OMIDescription; description != "" {
createOpts.SetDescription(description)
}
if config.ProductCodes != nil {
createOpts.ProductCodes = &config.ProductCodes
if prodCode := config.ProductCodes; prodCode != nil {
createOpts.SetProductCodes(prodCode)
}

resp, _, err := oscconn.Api.ImageApi.CreateImage(oscconn.Auth).CreateImageRequest(createOpts).Execute()
Expand Down
109 changes: 45 additions & 64 deletions builder/common/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -35,93 +34,75 @@ type LaunchBlockDevices struct {
LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
}

func buildOscBlockDevicesImage(b []BlockDevice) []oscgo.BlockDeviceMappingImage {
var blockDevices []oscgo.BlockDeviceMappingImage
func setBsuToCreate(blockDevice BlockDevice) oscgo.BsuToCreate {
bsu := oscgo.BsuToCreate{}

for _, blockDevice := range b {
mapping := oscgo.BlockDeviceMappingImage{
DeviceName: &blockDevice.DeviceName,
}
if deleteOnVmDeletion := blockDevice.DeleteOnVmDeletion; deleteOnVmDeletion {
bsu.SetDeleteOnVmDeletion(deleteOnVmDeletion)
}

if blockDevice.VirtualName != "" {
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
mapping.VirtualDeviceName = &blockDevice.VirtualName
}
} else {
bsu := oscgo.BsuToCreate{
DeleteOnVmDeletion: &blockDevice.DeleteOnVmDeletion,
}
if volType := blockDevice.VolumeType; volType != "" {
bsu.SetVolumeType(volType)
}

if blockDevice.VolumeType != "" {
bsu.VolumeType = &blockDevice.VolumeType
}
if volSize := blockDevice.VolumeSize; volSize > 0 {
bsu.SetVolumeSize(int32(blockDevice.VolumeSize))

if blockDevice.VolumeSize > 0 {
bsu.VolumeSize = oscgo.PtrInt32(int32(blockDevice.VolumeSize))
}
}

// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
bsu.Iops = oscgo.PtrInt32(int32(blockDevice.IOPS))
}
// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
bsu.SetIops(int32(blockDevice.IOPS))
}

if blockDevice.SnapshotId != "" {
bsu.SnapshotId = &blockDevice.SnapshotId
}
if snapId := blockDevice.SnapshotId; snapId != "" {
bsu.SetSnapshotId(snapId)
}
return bsu
}
func buildOscBlockDevicesImage(b []BlockDevice) []oscgo.BlockDeviceMappingImage {
var blockDevices []oscgo.BlockDeviceMappingImage

mapping.Bsu = &bsu
for _, blockDevice := range b {
mapping := oscgo.BlockDeviceMappingImage{}

if deviceName := blockDevice.DeviceName; deviceName != "" {
mapping.SetDeviceName(deviceName)
}

if VirName := blockDevice.VirtualName; VirName != "" {
if strings.HasPrefix(VirName, "ephemeral") {
mapping.SetVirtualDeviceName(VirName)
}
}
mapping.SetBsu(setBsuToCreate(blockDevice))

blockDevices = append(blockDevices, mapping)
}
return blockDevices
}

func buildOscBlockDevicesVmCreation(b []BlockDevice) []oscgo.BlockDeviceMappingVmCreation {
log.Printf("[DEBUG] Launch Block Device %#v", b)

var blockDevices []oscgo.BlockDeviceMappingVmCreation

for _, blockDevice := range b {
mapping := oscgo.BlockDeviceMappingVmCreation{
DeviceName: &blockDevice.DeviceName,
mapping := oscgo.BlockDeviceMappingVmCreation{}

if deviceName := blockDevice.DeviceName; deviceName != "" {
mapping.SetDeviceName(deviceName)
}

if VirName := blockDevice.VirtualName; VirName != "" {
if strings.HasPrefix(VirName, "ephemeral") {
mapping.SetVirtualDeviceName(VirName)
}
}

if blockDevice.NoDevice {
mapping.NoDevice = aws.String("")
//blockDevices = mapping[0]
} else if blockDevice.VirtualName != "" {
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
mapping.VirtualDeviceName = &blockDevice.VirtualName
}
} else {
bsu := oscgo.BsuToCreate{
DeleteOnVmDeletion: &blockDevice.DeleteOnVmDeletion,
}

if blockDevice.VolumeType != "" {
bsu.VolumeType = &blockDevice.VolumeType
}

if blockDevice.VolumeSize > 0 {
bsu.VolumeSize = oscgo.PtrInt32(int32(blockDevice.VolumeSize))

}

// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
bsu.Iops = oscgo.PtrInt32(int32(blockDevice.IOPS))
}

if blockDevice.SnapshotId != "" {
bsu.SnapshotId = &blockDevice.SnapshotId
}

mapping.Bsu = &bsu
mapping.SetBsu(setBsuToCreate(blockDevice))
}

log.Printf("block device mapping")

blockDevices = append(blockDevices, mapping)
}
return blockDevices
Expand Down
1 change: 1 addition & 0 deletions builder/common/omi_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type OMIConfig struct {
SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids"`
GlobalPermission bool `mapstructure:"global_permission"`
ProductCodes []string `mapstructure:"product_codes"`
RootDeviceName string `mapstructure:"root_device_name"`
}

func (c *OMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error {
Expand Down

0 comments on commit 09ccf70

Please sign in to comment.