generated from hashicorp/packer-plugin-scaffolding
/
block_device.go
158 lines (127 loc) · 4.12 KB
/
block_device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package common
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
oscgo "github.com/outscale/osc-sdk-go/v2"
)
// 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 {
OMIMappings []BlockDevice `mapstructure:"omi_block_device_mappings"`
}
type LaunchBlockDevices struct {
LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
}
func buildOscBlockDevicesImage(b []BlockDevice) []oscgo.BlockDeviceMappingImage {
var blockDevices []oscgo.BlockDeviceMappingImage
for _, blockDevice := range b {
mapping := oscgo.BlockDeviceMappingImage{
DeviceName: &blockDevice.DeviceName,
}
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
}
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,
}
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
}
log.Printf("block device mapping")
blockDevices = append(blockDevices, mapping)
}
return blockDevices
}
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
}
func (b *OMIBlockDevices) BuildOscOMIDevices() []oscgo.BlockDeviceMappingImage {
return buildOscBlockDevicesImage(b.OMIMappings)
}
func (b *LaunchBlockDevices) BuildOSCLaunchDevices() []oscgo.BlockDeviceMappingVmCreation {
return buildOscBlockDevicesVmCreation(b.LaunchMappings)
}