Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add and apply PciPassthroughPostCloneOperation() to subresource #1625

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ func PciPassthroughApplyOperation(d *schema.ResourceData, c *govmomi.Client, l o
Spec: []types.BaseVirtualDeviceConfigSpec{},
VirtualDevice: l,
}
if addDevs.Len() <= 0 && delDevs.Len() <= 0 {
if addDevs.Len() == 0 && delDevs.Len() == 0 {
return applyConfig.VirtualDevice, applyConfig.Spec, nil
}

Expand All @@ -1048,3 +1048,42 @@ func PciPassthroughApplyOperation(d *schema.ResourceData, c *govmomi.Client, l o
}
return applyConfig.VirtualDevice, applyConfig.Spec, nil
}

// PciPassthroughPostCloneOperation normalizes the PCI passthrough devices
// on a newly-cloned virtual machine and outputs any necessary device change
// operations. It also sets the state in advance of the post-create read.
func PciPassthroughPostCloneOperation(d *schema.ResourceData, c *govmomi.Client, l object.VirtualDeviceList) (object.VirtualDeviceList, []types.BaseVirtualDeviceConfigSpec, error) {
old, newValue := d.GetChange("pci_device_id")
oldDevIds := old.(*schema.Set)
newDevIds := newValue.(*schema.Set)

delDevs := oldDevIds.Difference(newDevIds)
addDevs := newDevIds.Difference(oldDevIds)
applyConfig := &pciApplyConfig{
Client: c,
ResourceData: d,
Spec: []types.BaseVirtualDeviceConfigSpec{},
VirtualDevice: l,
}
if addDevs.Len() <= 0 && delDevs.Len() <= 0 {
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
return applyConfig.VirtualDevice, applyConfig.Spec, nil
}

err := applyConfig.getPciSysID()
if err != nil {
return nil, nil, err
}

// Add new PCI passthrough devices
err = applyConfig.modifyVirtualPciDevices(addDevs, types.VirtualDeviceConfigSpecOperationAdd)
if err != nil {
return nil, nil, err
}

// Remove deleted PCI passthrough devices
err = applyConfig.modifyVirtualPciDevices(delDevs, types.VirtualDeviceConfigSpecOperationRemove)
if err != nil {
return nil, nil, err
}
return applyConfig.VirtualDevice, applyConfig.Spec, nil
}
11 changes: 11 additions & 0 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,17 @@ func resourceVSphereVirtualMachinePostDeployChanges(d *schema.ResourceData, meta
)
}
cfgSpec.DeviceChange = virtualdevice.AppendDeviceChangeSpec(cfgSpec.DeviceChange, delta...)
// PCI passthrough devices
devices, delta, err = virtualdevice.PciPassthroughPostCloneOperation(d, client, devices)
if err != nil {
return resourceVSphereVirtualMachineRollbackCreate(
d,
meta,
vm,
fmt.Errorf("error processing PCI passthrough device changes post-clone: %s", err),
)
}
cfgSpec.DeviceChange = virtualdevice.AppendDeviceChangeSpec(cfgSpec.DeviceChange, delta...)
log.Printf("[DEBUG] %s: Final device list: %s", resourceVSphereVirtualMachineIDString(d), virtualdevice.DeviceListString(devices))
log.Printf("[DEBUG] %s: Final device change cfgSpec: %s", resourceVSphereVirtualMachineIDString(d), virtualdevice.DeviceChangeString(cfgSpec.DeviceChange))

Expand Down