func delVNIC(ui packer.Ui, f *find.Finder, ctx context.Context, vm *object.VirtualMachine) error { ui.Say("Deleting NIC ") devicelst, err := vm.Device(ctx) if err != nil { return err } for _, device := range devicelst { switch device.(type) { case *types.VirtualVmxnet3: ui.Message(fmt.Sprintf("Removing NIC %s\n", device.GetVirtualDevice().DeviceInfo)) err := vm.RemoveDevice(ctx, device) if err != nil { return err } return nil case *types.VirtualE1000: ui.Message(fmt.Sprintf("Removing NIC %s\n", device.GetVirtualDevice().DeviceInfo)) err := vm.RemoveDevice(ctx, device) if err != nil { return err } return nil default: fmt.Printf("Type %s\n", reflect.TypeOf(device).Elem()) fmt.Printf("Device info %s\n", device.GetVirtualDevice().DeviceInfo) } } return nil } //
// Removes SCSI controller which is latest attached to VM. func cleanUpController(newSCSIController types.BaseVirtualDevice, vmDevices object.VirtualDeviceList, vm *object.VirtualMachine, ctx context.Context) error { ctls := vmDevices.SelectByType(newSCSIController) if len(ctls) < 1 { return ErrNoDevicesFound } newScsi := ctls[len(ctls)-1] err := vm.RemoveDevice(ctx, true, newScsi) if err != nil { return err } return nil }
func (vmh *VMHost) DetachRDM(vm *object.VirtualMachine, deviceNAA string) error { scsiLuns, err := vmh.GetSCSILuns() if err != nil { return err } mapSDI := make(map[string]*types.ScsiLun) for _, d := range scsiLuns { mapSDI[d.Uuid] = d } devices, err := vm.Device(context.TODO()) if err != nil { return err } for _, device := range devices { device2 := device.(types.BaseVirtualDevice).GetVirtualDevice() if device2.Backing != nil { elem := reflect.ValueOf(device2.Backing).Elem() lunUuid := elem.FieldByName("LunUuid") if lunUuid.Kind() == reflect.Invalid { continue } if sd, ok := mapSDI[lunUuid.String()]; ok && strings.Contains(sd.CanonicalName, deviceNAA) { deviceName := devices.Name(device) newDevice := devices.Find(deviceName) if newDevice == nil { return fmt.Errorf("device '%s' not found", deviceName) } if err = vm.RemoveDevice(context.TODO(), newDevice); err != nil { return err } break } } } return nil }