Skip to content

Commit

Permalink
add address type when updating macaddres at clone (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss authored Jun 4, 2021
1 parent 52dd87d commit c01e5dc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion builder/vsphere/driver/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ func (vm *VirtualMachineDriver) Clone(ctx context.Context, config *CloneConfig)

current := adapter.GetVirtualEthernetCard()
current.Backing = backing
current.MacAddress = config.MacAddress

if config.MacAddress != "" {
current.AddressType = string(types.VirtualEthernetCardMacTypeManual)
current.MacAddress = config.MacAddress
}

config := &types.VirtualDeviceConfigSpec{
Device: adapter.(types.BaseVirtualDevice),
Expand Down
59 changes: 59 additions & 0 deletions builder/vsphere/driver/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,62 @@ func TestVirtualMachineDriver_CloneWithPrimaryDiskResize(t *testing.T) {
t.Fatalf("unexpected disk size for primary disk: %d", disks[2].CapacityInKB)
}
}

func TestVirtualMachineDriver_CloneWithMacAddress(t *testing.T) {
sim, err := NewVCenterSimulator()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
defer sim.Close()

_, datastore := sim.ChooseSimulatorPreCreatedDatastore()
vm, _ := sim.ChooseSimulatorPreCreatedVM()

devices, err := vm.Devices()
if err != nil {
t.Fatalf("unexpected error %s", err.Error())
}

adapter, err := findNetworkAdapter(devices)
if err != nil {
t.Fatalf("unexpected error %s", err.Error())
}

network := adapter.GetVirtualEthernetCard()
oldMacAddress := network.MacAddress

newMacAddress := "D4:B4:D4:96:70:26"
config := &CloneConfig{
Name: "mock name",
Host: "DC0_H0",
Datastore: datastore.Name,
Network: "/DC0/network/VM Network",
MacAddress: newMacAddress,
}

ctx := context.TODO()
clonedVM, err := vm.Clone(ctx, config)
if err != nil {
t.Fatalf("unexpected error %s", err.Error())
}

devices, err = clonedVM.Devices()
if err != nil {
t.Fatalf("unexpected error %s", err.Error())
}
adapter, err = findNetworkAdapter(devices)
if err != nil {
t.Fatalf("unexpected error %s", err.Error())
}

network = adapter.GetVirtualEthernetCard()
if network.AddressType != string(types.VirtualEthernetCardMacTypeManual) {
t.Fatalf("unexpected address type")
}
if network.MacAddress == oldMacAddress {
t.Fatalf("expected mac address to be different from old one")
}
if network.MacAddress != newMacAddress {
t.Fatalf("unexpected mac address")
}
}

2 comments on commit c01e5dc

@rconde01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Would it be possible to get a build for testing?

@sylviamoss
Copy link
Contributor Author

@sylviamoss sylviamoss commented on c01e5dc Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rconde01 Yes, of course! You can download the plugin binary here https://app.circleci.com/pipelines/github/hashicorp/packer-plugin-vsphere/7/workflows/ff1d372c-7602-4ee7-9367-297d9e85dc12/jobs/98/artifacts and make sure its name is packer-plugin-vsphere and it's placed under the working directory.

Instructions are here under the manually (multi-component plugin): https://www.packer.io/docs/plugins#installing-plugins

Please sign in to comment.