Skip to content

Commit

Permalink
Merge pull request #3588 from dilyar85/bugfix/assign-controller
Browse files Browse the repository at this point in the history
fix: update controller's device list in AssignController
  • Loading branch information
dilyar85 authored Oct 10, 2024
2 parents 892dbcf + c44199a commit 4bc1e3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion object/virtual_device_list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015-2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -545,6 +545,8 @@ func (l VirtualDeviceList) AssignController(device types.BaseVirtualDevice, c ty
if d.Key == 0 {
d.Key = l.newRandomKey()
}

c.GetVirtualController().Device = append(c.GetVirtualController().Device, d.Key)
}

// newRandomKey returns a random negative device key.
Expand Down
8 changes: 7 additions & 1 deletion object/virtual_device_list_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -744,6 +744,12 @@ func TestAssignController(t *testing.T) {
if disk.Key >= 0 {
t.Errorf("device key %d should be negative", disk.Key)
}

// AssignController should add the disk to the controller's device list.
cd := scsi.(*types.VirtualLsiLogicController).Device[0]
if cd != disk.Key {
t.Errorf("expected controller device key: %d, got: %d\n", disk.Key, cd)
}
}

func TestCreateSCSIController(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,12 +1279,26 @@ func (vm *VirtualMachine) configureDevice(
d := device.GetVirtualDevice()
var controller types.BaseVirtualController

key := d.Key
if d.Key <= 0 {
// Keys can't be negative; Key 0 is reserved
d.Key = devices.NewKey()
d.Key *= -1
}

// Update device controller's key reference
if key != d.Key {
if device := devices.FindByKey(d.ControllerKey); device != nil {
c := device.(types.BaseVirtualController).GetVirtualController()
for i := range c.Device {
if c.Device[i] == key {
c.Device[i] = d.Key
break
}
}
}
}

// Choose a unique key
for {
if devices.FindByKey(d.Key) == nil {
Expand Down

0 comments on commit 4bc1e3d

Please sign in to comment.