forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixer_vsphere_network_storage.go
125 lines (103 loc) · 3.05 KB
/
fixer_vsphere_network_storage.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
package fix
import (
"github.com/mitchellh/mapstructure"
)
// FixerVSphereNetworkDisk changes vsphere-iso network and networkCard fields
// into a network adapter and changes the disk_size, disk_thin_provisioned, and
// disk_eagerly_scrub into a storage adapter
type FixerVSphereNetworkDisk struct{}
func (FixerVSphereNetworkDisk) DeprecatedOptions() []string {
return []string{"network_card", "network", "networkCard", "disk_size",
"disk_thin_provisioned", "disk_eagerly_scrub"}
}
func (FixerVSphereNetworkDisk) Fix(input map[string]interface{}) (map[string]interface{}, error) {
// The type we'll decode into; we only care about builders
type template struct {
Builders []map[string]interface{}
}
// Decode the input into our structure, if we can
var tpl template
if err := mapstructure.Decode(input, &tpl); err != nil {
return nil, err
}
for _, builder := range tpl.Builders {
builderTypeRaw, ok := builder["type"]
if !ok {
continue
}
builderType, ok := builderTypeRaw.(string)
if !ok {
continue
}
if builderType != "vsphere-iso" {
continue
}
var networkAdapters []interface{}
nic := make(map[string]interface{})
hasNetwork := false
networkRaw, ok := builder["network"]
if ok {
nic["network"] = networkRaw
delete(builder, "network")
hasNetwork = true
}
// legacy syntax from when VSphere was 3rd party
networkCardRaw, ok := builder["networkCard"]
if ok {
nic["network_card"] = networkCardRaw
delete(builder, "networkCard")
hasNetwork = true
}
// underscored syntax used when Packer merged vSphere
networkCardRaw, ok = builder["network_card"]
if ok {
nic["network_card"] = networkCardRaw
delete(builder, "network_card")
hasNetwork = true
}
if hasNetwork {
networkAdapters = append(networkAdapters, nic)
adaptersRaw, ok := builder["network_adapters"]
if ok {
existingAdapters := adaptersRaw.([]interface{})
networkAdapters = append(networkAdapters, existingAdapters...)
}
builder["network_adapters"] = networkAdapters
}
var storage []interface{}
disk := make(map[string]interface{})
hasStorage := false
diskSizeRaw, ok := builder["disk_size"]
if ok {
disk["disk_size"] = diskSizeRaw
delete(builder, "disk_size")
hasStorage = true
}
discThinProvisionedRaw, ok := builder["disk_thin_provisioned"]
if ok {
disk["disk_thin_provisioned"] = discThinProvisionedRaw
hasStorage = true
delete(builder, "disk_thin_provisioned")
}
diskEagerlyScrubRaw, ok := builder["disk_eagerly_scrub"]
if ok {
disk["disk_eagerly_scrub"] = diskEagerlyScrubRaw
hasStorage = true
delete(builder, "disk_eagerly_scrub")
}
if hasStorage {
storage = append(storage, disk)
storageRaw, ok := builder["storage"]
if ok {
existingStorage := storageRaw.([]interface{})
storage = append(storage, existingStorage...)
}
builder["storage"] = storage
}
}
input["builders"] = tpl.Builders
return input, nil
}
func (FixerVSphereNetworkDisk) Synopsis() string {
return `Removes deprecated network and disk fields from "vsphere-iso" builder`
}