Skip to content

Commit

Permalink
(vsphere-iso) remove deprecated network and disk fields (#9149)
Browse files Browse the repository at this point in the history
* remove deprecated network and disk fields
  • Loading branch information
jhawk28 authored May 19, 2020
1 parent 541ec89 commit 820f353
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 70 deletions.
10 changes: 0 additions & 10 deletions builder/vsphere/iso/config.hcl2spec.go

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

24 changes: 0 additions & 24 deletions builder/vsphere/iso/step_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,8 @@ type CreateConfig struct {
Firmware string `mapstructure:"firmware"`
// Set VM disk controller type. Example `pvscsi`.
DiskControllerType string `mapstructure:"disk_controller_type"`
// The size of the disk in MB.
DiskSize int64 `mapstructure:"disk_size"`
// Enable VMDK thin provisioning for VM. Defaults to `false`.
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
// Enable VMDK eager scrubbing for VM. Defaults to `false`.
DiskEagerlyScrub bool `mapstructure:"disk_eagerly_scrub"`
// A collection of one or more disks to be provisioned along with the VM.
Storage []DiskConfig `mapstructure:"storage"`
// Set network VM will be connected to.
Network string `mapstructure:"network"`
// Set VM network card type. Example `vmxnet3`.
NetworkCard string `mapstructure:"network_card"`
// Network adapters
NICs []NIC `mapstructure:"network_adapters"`
// Create USB controller for virtual machine. Defaults to `false`.
Expand All @@ -108,8 +98,6 @@ func (c *CreateConfig) Prepare() []error {
errs = append(errs, fmt.Errorf("storage[%d].'disk_size' is required", i))
}
}
} else if c.DiskSize == 0 {
errs = append(errs, fmt.Errorf("'disk_size' or 'storage' is required"))
}

if c.GuestOSType == "" {
Expand Down Expand Up @@ -144,11 +132,6 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste

// add network/network card an the first nic for backwards compatibility in the type is defined
var networkCards []driver.NIC
if s.Config.NetworkCard != "" {
networkCards = append(networkCards, driver.NIC{
NetworkCard: s.Config.NetworkCard,
Network: s.Config.Network})
}
for _, nic := range s.Config.NICs {
networkCards = append(networkCards, driver.NIC{
Network: nic.Network,
Expand All @@ -160,13 +143,6 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste

// add disk as the first drive for backwards compatibility if the type is defined
var disks []driver.Disk
if s.Config.DiskSize != 0 {
disks = append(disks, driver.Disk{
DiskSize: s.Config.DiskSize,
DiskEagerlyScrub: s.Config.DiskEagerlyScrub,
DiskThinProvisioned: s.Config.DiskThinProvisioned,
})
}
for _, disk := range s.Config.Storage {
disks = append(disks, driver.Disk{
DiskSize: disk.DiskSize,
Expand Down
42 changes: 16 additions & 26 deletions builder/vsphere/iso/step_create.hcl2spec.go

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

2 changes: 2 additions & 0 deletions fix/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {
"comm-config": new(FixerCommConfig),
"ssh-wait-timeout": new(FixerSSHTimout),
"docker-tag-tags": new(FixerDockerTagtoTags),
"vsphere-iso-net-disk": new(FixerVSphereNetworkDisk),
}

FixerOrder = []string{
Expand Down Expand Up @@ -81,5 +82,6 @@ func init() {
"galaxy-command",
"comm-config",
"ssh-wait-timeout",
"vsphere-iso-net-disk",
}
}
110 changes: 110 additions & 0 deletions fix/fixer_vsphere_network_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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) 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
}

networkCardRaw, ok := builder["networkCard"]
if ok {
nic["networkCard"] = networkCardRaw
delete(builder, "networkCard")
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`
}
Loading

0 comments on commit 820f353

Please sign in to comment.