From caf65781d71531798e59585234e7e8da84014cf4 Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Mon, 14 Sep 2020 13:18:39 +0200 Subject: [PATCH] Add vsphere-clone mac_address option (#9930) * add vsphere-clone mac_address option * check on prepare if network is set together with mac_address --- builder/vsphere/clone/config.hcl2spec.go | 2 ++ builder/vsphere/clone/step_clone.go | 7 +++++++ builder/vsphere/clone/step_clone.hcl2spec.go | 2 ++ builder/vsphere/driver/vm.go | 5 ++++- .../builder/vsphere/clone/CloneConfig-not-required.mdx | 2 ++ 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/builder/vsphere/clone/config.hcl2spec.go b/builder/vsphere/clone/config.hcl2spec.go index 83e86cef3..d43e56543 100644 --- a/builder/vsphere/clone/config.hcl2spec.go +++ b/builder/vsphere/clone/config.hcl2spec.go @@ -31,6 +31,7 @@ type FlatConfig struct { DiskSize *int64 `mapstructure:"disk_size" cty:"disk_size" hcl:"disk_size"` LinkedClone *bool `mapstructure:"linked_clone" cty:"linked_clone" hcl:"linked_clone"` Network *string `mapstructure:"network" cty:"network" hcl:"network"` + MacAddress *string `mapstructure:"mac_address" cty:"mac_address" hcl:"mac_address"` Notes *string `mapstructure:"notes" cty:"notes" hcl:"notes"` VAppConfig *FlatvAppConfig `mapstructure:"vapp" cty:"vapp" hcl:"vapp"` VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"` @@ -155,6 +156,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "disk_size": &hcldec.AttrSpec{Name: "disk_size", Type: cty.Number, Required: false}, "linked_clone": &hcldec.AttrSpec{Name: "linked_clone", Type: cty.Bool, Required: false}, "network": &hcldec.AttrSpec{Name: "network", Type: cty.String, Required: false}, + "mac_address": &hcldec.AttrSpec{Name: "mac_address", Type: cty.String, Required: false}, "notes": &hcldec.AttrSpec{Name: "notes", Type: cty.String, Required: false}, "vapp": &hcldec.BlockSpec{TypeName: "vapp", Nested: hcldec.ObjectSpec((*FlatvAppConfig)(nil).HCL2Spec())}, "vm_name": &hcldec.AttrSpec{Name: "vm_name", Type: cty.String, Required: false}, diff --git a/builder/vsphere/clone/step_clone.go b/builder/vsphere/clone/step_clone.go index ec55a3ecd..18ba50a30 100644 --- a/builder/vsphere/clone/step_clone.go +++ b/builder/vsphere/clone/step_clone.go @@ -37,6 +37,8 @@ type CloneConfig struct { // available network. If the network is inside a network folder in vCenter, // you need to provide the full path to the network. Network string `mapstructure:"network"` + // Sets a custom Mac Address to the network adapter. If set, the [network](#network) must be also specified. + MacAddress string `mapstructure:"mac_address"` // VM notes. Notes string `mapstructure:"notes"` // Set the vApp Options to a virtual machine. @@ -56,6 +58,10 @@ func (c *CloneConfig) Prepare() []error { errs = append(errs, fmt.Errorf("'linked_clone' and 'disk_size' cannot be used together")) } + if c.MacAddress != "" && c.Network == "" { + errs = append(errs, fmt.Errorf("'network' is required when 'mac_address' is specified")) + } + return errs } @@ -92,6 +98,7 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist Datastore: s.Location.Datastore, LinkedClone: s.Config.LinkedClone, Network: s.Config.Network, + MacAddress: s.Config.MacAddress, Annotation: s.Config.Notes, VAppProperties: s.Config.VAppConfig.Properties, }) diff --git a/builder/vsphere/clone/step_clone.hcl2spec.go b/builder/vsphere/clone/step_clone.hcl2spec.go index a9f670f94..ad55ebfa8 100644 --- a/builder/vsphere/clone/step_clone.hcl2spec.go +++ b/builder/vsphere/clone/step_clone.hcl2spec.go @@ -13,6 +13,7 @@ type FlatCloneConfig struct { DiskSize *int64 `mapstructure:"disk_size" cty:"disk_size" hcl:"disk_size"` LinkedClone *bool `mapstructure:"linked_clone" cty:"linked_clone" hcl:"linked_clone"` Network *string `mapstructure:"network" cty:"network" hcl:"network"` + MacAddress *string `mapstructure:"mac_address" cty:"mac_address" hcl:"mac_address"` Notes *string `mapstructure:"notes" cty:"notes" hcl:"notes"` VAppConfig *FlatvAppConfig `mapstructure:"vapp" cty:"vapp" hcl:"vapp"` } @@ -33,6 +34,7 @@ func (*FlatCloneConfig) HCL2Spec() map[string]hcldec.Spec { "disk_size": &hcldec.AttrSpec{Name: "disk_size", Type: cty.Number, Required: false}, "linked_clone": &hcldec.AttrSpec{Name: "linked_clone", Type: cty.Bool, Required: false}, "network": &hcldec.AttrSpec{Name: "network", Type: cty.String, Required: false}, + "mac_address": &hcldec.AttrSpec{Name: "mac_address", Type: cty.String, Required: false}, "notes": &hcldec.AttrSpec{Name: "notes", Type: cty.String, Required: false}, "vapp": &hcldec.BlockSpec{TypeName: "vapp", Nested: hcldec.ObjectSpec((*FlatvAppConfig)(nil).HCL2Spec())}, } diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index 0310af1c9..4138fb34b 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -69,6 +69,7 @@ type CloneConfig struct { Datastore string LinkedClone bool Network string + MacAddress string Annotation string VAppProperties map[string]string } @@ -344,7 +345,9 @@ func (vm *VirtualMachineDriver) Clone(ctx context.Context, config *CloneConfig) return nil, err } - adapter.GetVirtualEthernetCard().Backing = backing + current := adapter.GetVirtualEthernetCard() + current.Backing = backing + current.MacAddress = config.MacAddress config := &types.VirtualDeviceConfigSpec{ Device: adapter.(types.BaseVirtualDevice), diff --git a/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx b/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx index 804237daa..d9ffbd790 100644 --- a/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx @@ -11,6 +11,8 @@ available network. If the network is inside a network folder in vCenter, you need to provide the full path to the network. +- `mac_address` (string) - Sets a custom Mac Address to the network adapter. If set, the [network](#network) must be also specified. + - `notes` (string) - VM notes. - `vapp` (vAppConfig) - Set the vApp Options to a virtual machine.