From b46f5874509175c77358bbd2cd90e8fd905241e1 Mon Sep 17 00:00:00 2001 From: sylviamoss Date: Tue, 22 Sep 2020 11:32:25 +0200 Subject: [PATCH] vsphere-clone use common step_add_cdrom --- builder/vsphere/clone/builder.go | 2 +- builder/vsphere/clone/common_test.go | 17 -- builder/vsphere/clone/config.go | 2 +- builder/vsphere/clone/config.hcl2spec.go | 2 + builder/vsphere/clone/step_add_cdrom.go | 59 ------ .../vsphere/clone/step_add_cdrom.hcl2spec.go | 30 --- builder/vsphere/clone/step_add_cdrom_test.go | 200 ------------------ .../vsphere/{iso => common}/step_add_cdrom.go | 2 +- .../step_add_cdrom.hcl2spec.go | 2 +- .../{iso => common}/step_add_cdrom_test.go | 14 +- builder/vsphere/iso/builder.go | 2 +- builder/vsphere/iso/config.go | 2 +- .../docs/builders/vmware/vsphere-clone.mdx | 2 +- .../docs/builders/vmware/vsphere-iso.mdx | 2 +- .../clone/CDRomConfig-not-required.mdx | 3 - .../CDRomConfig-not-required.mdx | 2 +- 16 files changed, 18 insertions(+), 325 deletions(-) delete mode 100644 builder/vsphere/clone/common_test.go delete mode 100644 builder/vsphere/clone/step_add_cdrom.go delete mode 100644 builder/vsphere/clone/step_add_cdrom.hcl2spec.go delete mode 100644 builder/vsphere/clone/step_add_cdrom_test.go rename builder/vsphere/{iso => common}/step_add_cdrom.go (99%) rename builder/vsphere/{iso => common}/step_add_cdrom.hcl2spec.go (98%) rename builder/vsphere/{iso => common}/step_add_cdrom_test.go (96%) delete mode 100644 website/pages/partials/builder/vsphere/clone/CDRomConfig-not-required.mdx rename website/pages/partials/builder/vsphere/{iso => common}/CDRomConfig-not-required.mdx (86%) diff --git a/builder/vsphere/clone/builder.go b/builder/vsphere/clone/builder.go index 2f5408ba5..92d574187 100644 --- a/builder/vsphere/clone/builder.go +++ b/builder/vsphere/clone/builder.go @@ -58,7 +58,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &common.StepConfigureHardware{ Config: &b.config.HardwareConfig, }, - &StepAddCDRom{ + &common.StepAddCDRom{ Config: &b.config.CDRomConfig, }, &common.StepConfigParams{ diff --git a/builder/vsphere/clone/common_test.go b/builder/vsphere/clone/common_test.go deleted file mode 100644 index 180cd7d2a..000000000 --- a/builder/vsphere/clone/common_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package clone - -import ( - "bytes" - - "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" -) - -func basicStateBag() *multistep.BasicStateBag { - state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ - Reader: new(bytes.Buffer), - Writer: new(bytes.Buffer), - }) - return state -} diff --git a/builder/vsphere/clone/config.go b/builder/vsphere/clone/config.go index 1542e4bf7..c2d9c2e8a 100644 --- a/builder/vsphere/clone/config.go +++ b/builder/vsphere/clone/config.go @@ -23,7 +23,7 @@ type Config struct { common.HardwareConfig `mapstructure:",squash"` common.ConfigParamsConfig `mapstructure:",squash"` - CDRomConfig `mapstructure:",squash"` + common.CDRomConfig `mapstructure:",squash"` common.RemoveCDRomConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"` common.RunConfig `mapstructure:",squash"` diff --git a/builder/vsphere/clone/config.hcl2spec.go b/builder/vsphere/clone/config.hcl2spec.go index 6c845095e..7db101c50 100644 --- a/builder/vsphere/clone/config.hcl2spec.go +++ b/builder/vsphere/clone/config.hcl2spec.go @@ -61,6 +61,7 @@ type FlatConfig struct { ToolsSyncTime *bool `mapstructure:"tools_sync_time" cty:"tools_sync_time" hcl:"tools_sync_time"` ToolsUpgradePolicy *bool `mapstructure:"tools_upgrade_policy" cty:"tools_upgrade_policy" hcl:"tools_upgrade_policy"` CdromType *string `mapstructure:"cdrom_type" cty:"cdrom_type" hcl:"cdrom_type"` + ISOPaths []string `mapstructure:"iso_paths" cty:"iso_paths" hcl:"iso_paths"` RemoveCdrom *bool `mapstructure:"remove_cdrom" cty:"remove_cdrom" hcl:"remove_cdrom"` FloppyIMGPath *string `mapstructure:"floppy_img_path" cty:"floppy_img_path" hcl:"floppy_img_path"` FloppyFiles []string `mapstructure:"floppy_files" cty:"floppy_files" hcl:"floppy_files"` @@ -194,6 +195,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "tools_sync_time": &hcldec.AttrSpec{Name: "tools_sync_time", Type: cty.Bool, Required: false}, "tools_upgrade_policy": &hcldec.AttrSpec{Name: "tools_upgrade_policy", Type: cty.Bool, Required: false}, "cdrom_type": &hcldec.AttrSpec{Name: "cdrom_type", Type: cty.String, Required: false}, + "iso_paths": &hcldec.AttrSpec{Name: "iso_paths", Type: cty.List(cty.String), Required: false}, "remove_cdrom": &hcldec.AttrSpec{Name: "remove_cdrom", Type: cty.Bool, Required: false}, "floppy_img_path": &hcldec.AttrSpec{Name: "floppy_img_path", Type: cty.String, Required: false}, "floppy_files": &hcldec.AttrSpec{Name: "floppy_files", Type: cty.List(cty.String), Required: false}, diff --git a/builder/vsphere/clone/step_add_cdrom.go b/builder/vsphere/clone/step_add_cdrom.go deleted file mode 100644 index f7c1996be..000000000 --- a/builder/vsphere/clone/step_add_cdrom.go +++ /dev/null @@ -1,59 +0,0 @@ -//go:generate struct-markdown -//go:generate mapstructure-to-hcl2 -type CDRomConfig - -package clone - -import ( - "context" - "fmt" - - "github.com/hashicorp/packer/builder/vsphere/driver" - "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" -) - -type CDRomConfig struct { - // Which controller to use. Example: `sata`. Defaults to `ide`. - CdromType string `mapstructure:"cdrom_type"` -} - -type StepAddCDRom struct { - Config *CDRomConfig -} - -func (c *CDRomConfig) Prepare() []error { - var errs []error - - if c.CdromType != "" && c.CdromType != "ide" && c.CdromType != "sata" { - errs = append(errs, fmt.Errorf("'cdrom_type' must be 'ide' or 'sata'")) - } - - return errs -} - -func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - ui := state.Get("ui").(packer.Ui) - vm := state.Get("vm").(driver.VirtualMachine) - - if s.Config.CdromType == "sata" { - if _, err := vm.FindSATAController(); err == driver.ErrNoSataController { - ui.Say("Adding SATA controller...") - if err := vm.AddSATAController(); err != nil { - state.Put("error", fmt.Errorf("error adding SATA controller: %v", err)) - return multistep.ActionHalt - } - } - } - - // Add our custom CD, if it exists - if cd_path, _ := state.Get("cd_path").(string); cd_path != "" { - if err := vm.AddCdrom(s.Config.CdromType, cd_path); err != nil { - state.Put("error", fmt.Errorf("error mounting a CD '%v': %v", cd_path, err)) - return multistep.ActionHalt - } - } - - return multistep.ActionContinue -} - -func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {} diff --git a/builder/vsphere/clone/step_add_cdrom.hcl2spec.go b/builder/vsphere/clone/step_add_cdrom.hcl2spec.go deleted file mode 100644 index 5af0a7602..000000000 --- a/builder/vsphere/clone/step_add_cdrom.hcl2spec.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by "mapstructure-to-hcl2 -type CDRomConfig"; DO NOT EDIT. -package clone - -import ( - "github.com/hashicorp/hcl/v2/hcldec" - "github.com/zclconf/go-cty/cty" -) - -// FlatCDRomConfig is an auto-generated flat version of CDRomConfig. -// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. -type FlatCDRomConfig struct { - CdromType *string `mapstructure:"cdrom_type" cty:"cdrom_type" hcl:"cdrom_type"` -} - -// FlatMapstructure returns a new FlatCDRomConfig. -// FlatCDRomConfig is an auto-generated flat version of CDRomConfig. -// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. -func (*CDRomConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { - return new(FlatCDRomConfig) -} - -// HCL2Spec returns the hcl spec of a CDRomConfig. -// This spec is used by HCL to read the fields of CDRomConfig. -// The decoded values from this spec will then be applied to a FlatCDRomConfig. -func (*FlatCDRomConfig) HCL2Spec() map[string]hcldec.Spec { - s := map[string]hcldec.Spec{ - "cdrom_type": &hcldec.AttrSpec{Name: "cdrom_type", Type: cty.String, Required: false}, - } - return s -} diff --git a/builder/vsphere/clone/step_add_cdrom_test.go b/builder/vsphere/clone/step_add_cdrom_test.go deleted file mode 100644 index 96b794555..000000000 --- a/builder/vsphere/clone/step_add_cdrom_test.go +++ /dev/null @@ -1,200 +0,0 @@ -package clone - -import ( - "context" - "fmt" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - "github.com/hashicorp/packer/builder/vsphere/driver" - "github.com/hashicorp/packer/helper/multistep" -) - -func TestCDRomConfig_Prepare(t *testing.T) { - // Data validation - tc := []struct { - name string - config *CDRomConfig - fail bool - expectedErrMsg string - }{ - { - name: "Should not fail for empty config", - config: new(CDRomConfig), - fail: false, - expectedErrMsg: "", - }, - { - name: "Valid cdroom type ide", - config: &CDRomConfig{CdromType: "ide"}, - fail: false, - expectedErrMsg: "", - }, - { - name: "Valid cdroom type sata", - config: &CDRomConfig{CdromType: "ide"}, - fail: false, - expectedErrMsg: "", - }, - { - name: "Invalid cdroom type", - config: &CDRomConfig{CdromType: "invalid"}, - fail: true, - expectedErrMsg: "'cdrom_type' must be 'ide' or 'sata'", - }, - } - - for _, c := range tc { - errs := c.config.Prepare() - if c.fail { - if len(errs) == 0 { - t.Fatalf("Config preprare should fail") - } - if errs[0].Error() != c.expectedErrMsg { - t.Fatalf("Expected error message: %s but was '%s'", c.expectedErrMsg, errs[0].Error()) - } - } else { - if len(errs) != 0 { - t.Fatalf("Config preprare should not fail") - } - } - } -} - -func TestStepAddCDRom_Run(t *testing.T) { - tc := []struct { - name string - state *multistep.BasicStateBag - step *StepAddCDRom - vmMock *driver.VirtualMachineMock - expectedAction multistep.StepAction - expectedVmMock *driver.VirtualMachineMock - fail bool - errMessage string - }{ - { - name: "CDRom SATA type with all cd paths set", - state: cdPathStateBag(), - step: &StepAddCDRom{ - Config: &CDRomConfig{ - CdromType: "sata", - }, - }, - vmMock: new(driver.VirtualMachineMock), - expectedAction: multistep.ActionContinue, - expectedVmMock: &driver.VirtualMachineMock{ - FindSATAControllerCalled: true, - AddCdromCalled: true, - AddCdromCalledTimes: 1, - AddCdromTypes: []string{"sata"}, - AddCdromPaths: []string{"cd/path"}, - }, - fail: false, - errMessage: "", - }, - { - name: "Add SATA Controller", - state: basicStateBag(), - step: &StepAddCDRom{ - Config: &CDRomConfig{ - CdromType: "sata", - }, - }, - vmMock: &driver.VirtualMachineMock{ - FindSATAControllerErr: driver.ErrNoSataController, - }, - expectedAction: multistep.ActionContinue, - expectedVmMock: &driver.VirtualMachineMock{ - FindSATAControllerCalled: true, - FindSATAControllerErr: driver.ErrNoSataController, - AddSATAControllerCalled: true, - }, - fail: false, - errMessage: "", - }, - { - name: "Fail to add SATA Controller", - state: basicStateBag(), - step: &StepAddCDRom{ - Config: &CDRomConfig{ - CdromType: "sata", - }, - }, - vmMock: &driver.VirtualMachineMock{ - FindSATAControllerErr: driver.ErrNoSataController, - AddSATAControllerErr: fmt.Errorf("AddSATAController error"), - }, - expectedAction: multistep.ActionHalt, - expectedVmMock: &driver.VirtualMachineMock{ - FindSATAControllerCalled: true, - AddSATAControllerCalled: true, - }, - fail: true, - errMessage: fmt.Sprintf("error adding SATA controller: %v", fmt.Errorf("AddSATAController error")), - }, - { - name: "IDE CDRom Type and Iso Path set", - state: basicStateBag(), - step: &StepAddCDRom{ - Config: &CDRomConfig{ - CdromType: "ide", - }, - }, - vmMock: new(driver.VirtualMachineMock), - expectedAction: multistep.ActionContinue, - expectedVmMock: new(driver.VirtualMachineMock), - fail: false, - errMessage: "", - }, - { - name: "Fail to add cdrom from state cd_path", - state: cdPathStateBag(), - step: &StepAddCDRom{ - Config: new(CDRomConfig), - }, - vmMock: &driver.VirtualMachineMock{ - AddCdromErr: fmt.Errorf("AddCdrom error"), - }, - expectedAction: multistep.ActionHalt, - expectedVmMock: &driver.VirtualMachineMock{ - AddCdromCalled: true, - AddCdromCalledTimes: 1, - AddCdromTypes: []string{""}, - AddCdromPaths: []string{"cd/path"}, - }, - fail: true, - errMessage: fmt.Sprintf("error mounting a CD 'cd/path': %v", fmt.Errorf("AddCdrom error")), - }, - } - - for _, c := range tc { - t.Run(c.name, func(t *testing.T) { - c.state.Put("vm", c.vmMock) - if action := c.step.Run(context.TODO(), c.state); action != c.expectedAction { - t.Fatalf("unexpected action %v", action) - } - err, ok := c.state.Get("error").(error) - if ok { - if err.Error() != c.errMessage { - t.Fatalf("unexpected error %s", err.Error()) - } - } else { - if c.fail { - t.Fatalf("expected to fail but it didn't") - } - } - - if diff := cmp.Diff(c.vmMock, c.expectedVmMock, - cmpopts.IgnoreInterfaces(struct{ error }{})); diff != "" { - t.Fatalf("unexpected VirtualMachine calls: %s", diff) - } - }) - } -} - -func cdPathStateBag() *multistep.BasicStateBag { - state := basicStateBag() - state.Put("cd_path", "cd/path") - return state -} diff --git a/builder/vsphere/iso/step_add_cdrom.go b/builder/vsphere/common/step_add_cdrom.go similarity index 99% rename from builder/vsphere/iso/step_add_cdrom.go rename to builder/vsphere/common/step_add_cdrom.go index ea8155b1c..d6a6f62b8 100644 --- a/builder/vsphere/iso/step_add_cdrom.go +++ b/builder/vsphere/common/step_add_cdrom.go @@ -1,7 +1,7 @@ //go:generate struct-markdown //go:generate mapstructure-to-hcl2 -type CDRomConfig -package iso +package common import ( "context" diff --git a/builder/vsphere/iso/step_add_cdrom.hcl2spec.go b/builder/vsphere/common/step_add_cdrom.hcl2spec.go similarity index 98% rename from builder/vsphere/iso/step_add_cdrom.hcl2spec.go rename to builder/vsphere/common/step_add_cdrom.hcl2spec.go index 572236571..6890fe091 100644 --- a/builder/vsphere/iso/step_add_cdrom.hcl2spec.go +++ b/builder/vsphere/common/step_add_cdrom.hcl2spec.go @@ -1,5 +1,5 @@ // Code generated by "mapstructure-to-hcl2 -type CDRomConfig"; DO NOT EDIT. -package iso +package common import ( "github.com/hashicorp/hcl/v2/hcldec" diff --git a/builder/vsphere/iso/step_add_cdrom_test.go b/builder/vsphere/common/step_add_cdrom_test.go similarity index 96% rename from builder/vsphere/iso/step_add_cdrom_test.go rename to builder/vsphere/common/step_add_cdrom_test.go index bc7755879..e1116660c 100644 --- a/builder/vsphere/iso/step_add_cdrom_test.go +++ b/builder/vsphere/common/step_add_cdrom_test.go @@ -1,4 +1,4 @@ -package iso +package common import ( "context" @@ -96,7 +96,7 @@ func TestStepAddCDRom_Run(t *testing.T) { }, { name: "Add SATA Controller", - state: basicStateBag(), + state: basicStateBag(nil), step: &StepAddCDRom{ Config: &CDRomConfig{ CdromType: "sata", @@ -116,7 +116,7 @@ func TestStepAddCDRom_Run(t *testing.T) { }, { name: "Fail to add SATA Controller", - state: basicStateBag(), + state: basicStateBag(nil), step: &StepAddCDRom{ Config: &CDRomConfig{ CdromType: "sata", @@ -136,7 +136,7 @@ func TestStepAddCDRom_Run(t *testing.T) { }, { name: "IDE CDRom Type and Iso Path set", - state: basicStateBag(), + state: basicStateBag(nil), step: &StepAddCDRom{ Config: &CDRomConfig{ CdromType: "ide", @@ -156,7 +156,7 @@ func TestStepAddCDRom_Run(t *testing.T) { }, { name: "Fail to add cdrom from ISOPaths", - state: basicStateBag(), + state: basicStateBag(nil), step: &StepAddCDRom{ Config: &CDRomConfig{ ISOPaths: []string{"iso/path"}, @@ -222,14 +222,14 @@ func TestStepAddCDRom_Run(t *testing.T) { } func cdAndIsoRemotePathStateBag() *multistep.BasicStateBag { - state := basicStateBag() + state := basicStateBag(nil) state.Put("iso_remote_path", "remote/path") state.Put("cd_path", "cd/path") return state } func isoRemotePathStateBag() *multistep.BasicStateBag { - state := basicStateBag() + state := basicStateBag(nil) state.Put("iso_remote_path", "remote/path") return state } diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index 009d2e54d..e3fc9ec46 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -65,7 +65,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &common.StepConfigureHardware{ Config: &b.config.HardwareConfig, }, - &StepAddCDRom{ + &common.StepAddCDRom{ Config: &b.config.CDRomConfig, }, &common.StepConfigParams{ diff --git a/builder/vsphere/iso/config.go b/builder/vsphere/iso/config.go index 0e5113d8b..2d83c35d0 100644 --- a/builder/vsphere/iso/config.go +++ b/builder/vsphere/iso/config.go @@ -25,7 +25,7 @@ type Config struct { packerCommon.ISOConfig `mapstructure:",squash"` - CDRomConfig `mapstructure:",squash"` + common.CDRomConfig `mapstructure:",squash"` common.RemoveCDRomConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"` common.RunConfig `mapstructure:",squash"` diff --git a/website/pages/docs/builders/vmware/vsphere-clone.mdx b/website/pages/docs/builders/vmware/vsphere-clone.mdx index 51cde158f..6c892fd13 100644 --- a/website/pages/docs/builders/vmware/vsphere-clone.mdx +++ b/website/pages/docs/builders/vmware/vsphere-clone.mdx @@ -204,7 +204,7 @@ can be done via environment variable: @include 'common/CDConfig-not-required.mdx' -@include 'builder/vsphere/clone/CDRomConfig-not-required.mdx' +@include 'builder/vsphere/common/CDRomConfig-not-required.mdx' ### Communicator configuration diff --git a/website/pages/docs/builders/vmware/vsphere-iso.mdx b/website/pages/docs/builders/vmware/vsphere-iso.mdx index a1aad8ec2..e5bf05eae 100644 --- a/website/pages/docs/builders/vmware/vsphere-iso.mdx +++ b/website/pages/docs/builders/vmware/vsphere-iso.mdx @@ -172,7 +172,7 @@ iso_paths = [ -@include 'builder/vsphere/iso/CDRomConfig-not-required.mdx' +@include 'builder/vsphere/common/CDRomConfig-not-required.mdx' @include 'common/CDConfig.mdx' diff --git a/website/pages/partials/builder/vsphere/clone/CDRomConfig-not-required.mdx b/website/pages/partials/builder/vsphere/clone/CDRomConfig-not-required.mdx deleted file mode 100644 index e29843daa..000000000 --- a/website/pages/partials/builder/vsphere/clone/CDRomConfig-not-required.mdx +++ /dev/null @@ -1,3 +0,0 @@ - - -- `cdrom_type` (string) - Which controller to use. Example: `sata`. Defaults to `ide`. diff --git a/website/pages/partials/builder/vsphere/iso/CDRomConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/CDRomConfig-not-required.mdx similarity index 86% rename from website/pages/partials/builder/vsphere/iso/CDRomConfig-not-required.mdx rename to website/pages/partials/builder/vsphere/common/CDRomConfig-not-required.mdx index b72e5de01..e225b4820 100644 --- a/website/pages/partials/builder/vsphere/iso/CDRomConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/CDRomConfig-not-required.mdx @@ -1,4 +1,4 @@ - + - `cdrom_type` (string) - Which controller to use. Example: `sata`. Defaults to `ide`.