From 99e7ac5f42e163769de808ebed0908f0c9e9b40e Mon Sep 17 00:00:00 2001 From: sylviamoss Date: Fri, 20 Nov 2020 16:42:49 +0100 Subject: [PATCH] fix test and generated code --- .../vsphere/clone/step_customize.hcl2spec.go | 11 ++- builder/vsphere/driver/vm.go | 71 ------------------ builder/vsphere/iso/step_create_test.go | 64 +++++++++++++++- .../vsphere/iso/DiskConfig-not-required.mdx | 7 -- .../vsphere/iso/DiskConfig-required.mdx | 3 - .../builder/vsphere/iso/DiskConfig.mdx | 74 ------------------- 6 files changed, 66 insertions(+), 164 deletions(-) delete mode 100644 website/pages/partials/builder/vsphere/iso/DiskConfig-not-required.mdx delete mode 100644 website/pages/partials/builder/vsphere/iso/DiskConfig-required.mdx delete mode 100644 website/pages/partials/builder/vsphere/iso/DiskConfig.mdx diff --git a/builder/vsphere/clone/step_customize.hcl2spec.go b/builder/vsphere/clone/step_customize.hcl2spec.go index ff2f1dded..2620122b2 100644 --- a/builder/vsphere/clone/step_customize.hcl2spec.go +++ b/builder/vsphere/clone/step_customize.hcl2spec.go @@ -3,7 +3,6 @@ package clone import ( "github.com/hashicorp/hcl/v2/hcldec" - "github.com/hashicorp/packer/packer-plugin-sdk/template/config" "github.com/zclconf/go-cty/cty" ) @@ -95,10 +94,10 @@ func (*FlatGlobalRoutingSettings) HCL2Spec() map[string]hcldec.Spec { // FlatLinuxOptions is an auto-generated flat version of LinuxOptions. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. type FlatLinuxOptions struct { - Domain *string `mapstructure:"domain" cty:"domain" hcl:"domain"` - Hostname *string `mapstructure:"host_name" cty:"host_name" hcl:"host_name"` - HWClockUTC *config.Trilean `mapstructure:"hw_clock_utc" cty:"hw_clock_utc" hcl:"hw_clock_utc"` - Timezone *string `mapstructure:"time_zone" cty:"time_zone" hcl:"time_zone"` + Domain *string `mapstructure:"domain" cty:"domain" hcl:"domain"` + Hostname *string `mapstructure:"host_name" cty:"host_name" hcl:"host_name"` + HWClockUTC *bool `mapstructure:"hw_clock_utc" cty:"hw_clock_utc" hcl:"hw_clock_utc"` + Timezone *string `mapstructure:"time_zone" cty:"time_zone" hcl:"time_zone"` } // FlatMapstructure returns a new FlatLinuxOptions. @@ -115,7 +114,7 @@ func (*FlatLinuxOptions) HCL2Spec() map[string]hcldec.Spec { s := map[string]hcldec.Spec{ "domain": &hcldec.AttrSpec{Name: "domain", Type: cty.String, Required: false}, "host_name": &hcldec.AttrSpec{Name: "host_name", Type: cty.String, Required: false}, - "hw_clock_utc": &hcldec.AttrSpec{Name: "hw_clock_utc", Type: cty.Number, Required: false}, + "hw_clock_utc": &hcldec.AttrSpec{Name: "hw_clock_utc", Type: cty.Bool, Required: false}, "time_zone": &hcldec.AttrSpec{Name: "time_zone", Type: cty.String, Required: false}, } return s diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index 020af6809..acc80ec4e 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -97,7 +97,6 @@ type HardwareConfig struct { VGPUProfile string Firmware string ForceBIOSSetup bool - StorageConfig StorageConfig } type NIC struct { @@ -521,76 +520,6 @@ func (vm *VirtualMachineDriver) Configure(config *HardwareConfig) error { confSpec.CpuHotAddEnabled = &config.CpuHotAddEnabled confSpec.MemoryHotAddEnabled = &config.MemoryHotAddEnabled - // - //if len(config.StorageConfig.Storage) > 0 { - // ds, err := vm.vm.Device(vm.driver.ctx) - // if err != nil { - // return err - // } - // vd := ds.SelectByType((*types.VirtualDisk)(nil)) - // vc := ds.SelectByType((*types.VirtualController)(nil)) - // - // // Use existing devices to avoid wrong configuration - // devices := object.VirtualDeviceList{} - // devices = append(devices, vd...) - // devices = append(devices, vc...) - // - // newDevices := object.VirtualDeviceList{} - // - // // Adds new controllers - // var controllers []types.BaseVirtualController - // for _, controllerType := range config.StorageConfig.DiskControllerType { - // var device types.BaseVirtualDevice - // var err error - // if controllerType == "nvme" { - // device, err = devices.CreateNVMEController() - // } else { - // device, err = devices.CreateSCSIController(controllerType) - // } - // if err != nil { - // return err - // } - // devices = append(devices, device) - // newDevices = append(newDevices, device) - // name := devices.Name(device) - // log.Printf("MOSS controller name %s", name) - // controller, err := devices.FindDiskController(name) - // if err != nil { - // return err - // } - // controllers = append(controllers, controller) - // } - // - // for _, dc := range config.StorageConfig.Storage { - // key := devices.NewKey() - // disk := &types.VirtualDisk{ - // VirtualDevice: types.VirtualDevice{ - // Key: key, - // Backing: &types.VirtualDiskFlatVer2BackingInfo{ - // DiskMode: string(types.VirtualDiskModePersistent), - // ThinProvisioned: types.NewBool(dc.DiskThinProvisioned), - // EagerlyScrub: types.NewBool(dc.DiskEagerlyScrub), - // }, - // }, - // CapacityInKB: dc.DiskSize * 1024, - // } - // - // log.Printf("MOSS device key %d", key) - // - // devices.AssignController(disk, controllers[dc.ControllerIndex]) - // newDevices = append(newDevices, disk) - // } - // //devices, err = config.StorageConfig.AddStorageDevices(devices) - // //if err != nil { - // // return err - // //} - // - // devicesConfigSpec, err := newDevices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd) - // if err != nil { - // return err - // } - // confSpec.DeviceChange = append(confSpec.DeviceChange, devicesConfigSpec...) - //} if config.VideoRAM != 0 { devices, err := vm.vm.Device(vm.driver.ctx) diff --git a/builder/vsphere/iso/step_create_test.go b/builder/vsphere/iso/step_create_test.go index a4efc95fa..cd0b4fa57 100644 --- a/builder/vsphere/iso/step_create_test.go +++ b/builder/vsphere/iso/step_create_test.go @@ -17,9 +17,18 @@ import ( func TestCreateConfig_Prepare(t *testing.T) { // Empty config - check defaults - config := &CreateConfig{} + config := &CreateConfig{ + // Storage is required + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, + } if errs := config.Prepare(); len(errs) != 0 { - t.Fatalf("Config preprare should not fail") + t.Fatalf("Config preprare should not fail: %s", errs[0]) } if config.GuestOSType != "otherGuest" { t.Fatalf("GuestOSType should default to 'otherGuest'") @@ -69,6 +78,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate 'usb' and 'xhci' can be set together", config: &CreateConfig{ USBController: []string{"usb", "xhci"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: false, }, @@ -76,6 +92,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate '1' and '0' can be set together", config: &CreateConfig{ USBController: []string{"1", "0"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: false, }, @@ -83,6 +106,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate 'true' and 'false' can be set together", config: &CreateConfig{ USBController: []string{"true", "false"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: false, }, @@ -90,6 +120,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate 'true' and 'usb' cannot be set together", config: &CreateConfig{ USBController: []string{"true", "usb"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: true, expectedErrMsg: "there can only be one usb controller and one xhci controller", @@ -98,6 +135,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate '1' and 'usb' cannot be set together", config: &CreateConfig{ USBController: []string{"1", "usb"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: true, expectedErrMsg: "there can only be one usb controller and one xhci controller", @@ -106,6 +150,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate 'xhci' cannot be set more that once", config: &CreateConfig{ USBController: []string{"xhci", "xhci"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: true, expectedErrMsg: "there can only be one usb controller and one xhci controller", @@ -114,6 +165,13 @@ func TestCreateConfig_Prepare(t *testing.T) { name: "USBController validate unknown value cannot be set", config: &CreateConfig{ USBController: []string{"unknown"}, + StorageConfig: common.StorageConfig{ + Storage: []common.DiskConfig{ + { + DiskSize: 32768, + }, + }, + }, }, fail: true, expectedErrMsg: "usb_controller[0] references an unknown usb controller", @@ -131,7 +189,7 @@ func TestCreateConfig_Prepare(t *testing.T) { } } else { if len(errs) != 0 { - t.Fatalf("Config preprare should not fail") + t.Fatalf("Config preprare should not fail: %s", errs[0]) } } } diff --git a/website/pages/partials/builder/vsphere/iso/DiskConfig-not-required.mdx b/website/pages/partials/builder/vsphere/iso/DiskConfig-not-required.mdx deleted file mode 100644 index 8e31b90d4..000000000 --- a/website/pages/partials/builder/vsphere/iso/DiskConfig-not-required.mdx +++ /dev/null @@ -1,7 +0,0 @@ - - -- `disk_thin_provisioned` (bool) - Enable VMDK thin provisioning for VM. Defaults to `false`. - -- `disk_eagerly_scrub` (bool) - Enable VMDK eager scrubbing for VM. Defaults to `false`. - -- `disk_controller_index` (int) - The assigned disk controller. Defaults to the first one (0) diff --git a/website/pages/partials/builder/vsphere/iso/DiskConfig-required.mdx b/website/pages/partials/builder/vsphere/iso/DiskConfig-required.mdx deleted file mode 100644 index 83683c366..000000000 --- a/website/pages/partials/builder/vsphere/iso/DiskConfig-required.mdx +++ /dev/null @@ -1,3 +0,0 @@ - - -- `disk_size` (int64) - The size of the disk in MB. diff --git a/website/pages/partials/builder/vsphere/iso/DiskConfig.mdx b/website/pages/partials/builder/vsphere/iso/DiskConfig.mdx deleted file mode 100644 index 988ce2819..000000000 --- a/website/pages/partials/builder/vsphere/iso/DiskConfig.mdx +++ /dev/null @@ -1,74 +0,0 @@ - - -Defines the disk storage for a VM. - -Example that will create a 15GB and a 20GB disk on the VM. The second disk will be thin provisioned: - -In JSON: -```json - "storage": [ - { - "disk_size": 15000 - }, - { - "disk_size": 20000, - "disk_thin_provisioned": true - } - ], -``` -In HCL2: -```hcl - storage { - disk_size = 15000 - } - storage { - disk_size = 20000 - disk_thin_provisioned = true - } -``` - -Example that creates 2 pvscsi controllers and adds 2 disks to each one: - -In JSON: -```json - "disk_controller_type": ["pvscsi", "pvscsi"], - "storage": [ - { - "disk_size": 15000, - "disk_controller_index": 0 - }, - { - "disk_size": 15000, - "disk_controller_index": 0 - }, - { - "disk_size": 15000, - "disk_controller_index": 1 - }, - { - "disk_size": 15000, - "disk_controller_index": 1 - } - ], -``` - -In HCL2: -```hcl - disk_controller_type = ["pvscsi", "pvscsi"] - storage { - disk_size = 15000, - disk_controller_index = 0 - } - storage { - disk_size = 15000 - disk_controller_index = 0 - } - storage { - disk_size = 15000 - disk_controller_index = 1 - } - storage { - disk_size = 15000 - disk_controller_index = 1 - } -```