Skip to content

Commit

Permalink
r/virtual_machine: Correct present NIC device calculation
Browse files Browse the repository at this point in the history
This corrects an issue with the way NIC devices were checked to see if
the slots they are supposed to occupy on the PCI bus were already used
or not. An offset that was used to populate the current device range was
not being used when the devices were actually checked, creating invalid
results and out of range errors.

Fixes #279.
  • Loading branch information
vancluever committed Dec 6, 2017
1 parent efa7cfc commit 539a1e4
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@ func (r *NetworkInterfaceSubresource) assignEthernetCard(l object.VirtualDeviceL

// Now that we know which units are used, we can pick one
newUnit := int32(r.Index) + pciDeviceOffset
if units[newUnit] {
log.Printf("[TRACE] newUnit: %d", newUnit)
if units[newUnit-pciDeviceOffset] {
return fmt.Errorf("device unit at %d is currently in use on the PCI bus", newUnit)
}

Expand Down
142 changes: 142 additions & 0 deletions vsphere/resource_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ func TestAccResourceVSphereVirtualMachine(t *testing.T) {
},
},
},
{
"maximum number of nics",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereVirtualMachinePreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereVirtualMachineCheckExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereVirtualMachineConfigMaxNIC(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereVirtualMachineCheckExists(true),
testAccResourceVSphereVirtualMachineCheckNICCount(10),
),
},
},
},
},
{
"upgrade cpu and ram",
resource.TestCase{
Expand Down Expand Up @@ -1808,6 +1828,28 @@ func testAccResourceVSphereVirtualMachineCheckVmdkDatastore(name, expected strin
}
}

// testAccResourceVSphereVirtualMachineCheckNICCount checks the number of NICs
// on the virtual machine.
func testAccResourceVSphereVirtualMachineCheckNICCount(expected int) resource.TestCheckFunc {
return func(s *terraform.State) error {
props, err := testGetVirtualMachineProperties(s, "vm")
if err != nil {
return err
}

var actual int
for _, dev := range props.Config.Hardware.Device {
if _, ok := dev.(types.BaseVirtualEthernetCard); ok {
actual++
}
}
if expected != actual {
return fmt.Errorf("expected %d number of NICs, got %d", expected, actual)
}
return nil
}
}

func testAccResourceVSphereVirtualMachineConfigBasic() string {
return fmt.Sprintf(`
variable "datacenter" {
Expand Down Expand Up @@ -2391,6 +2433,105 @@ resource "vsphere_virtual_machine" "vm" {
)
}

func testAccResourceVSphereVirtualMachineConfigMaxNIC() string {
return fmt.Sprintf(`
variable "datacenter" {
default = "%s"
}
variable "resource_pool" {
default = "%s"
}
variable "network_label" {
default = "%s"
}
variable "datastore" {
default = "%s"
}
data "vsphere_datacenter" "dc" {
name = "${var.datacenter}"
}
data "vsphere_datastore" "datastore" {
name = "${var.datastore}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
data "vsphere_resource_pool" "pool" {
name = "${var.resource_pool}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
data "vsphere_network" "network" {
name = "${var.network_label}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
resource "vsphere_virtual_machine" "vm" {
name = "terraform-test"
resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
datastore_id = "${data.vsphere_datastore.datastore.id}"
num_cpus = 2
memory = 2048
guest_id = "other3xLinux64Guest"
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
}
disk {
name = "terraform-test.vmdk"
size = 20
}
}
`,
os.Getenv("VSPHERE_DATACENTER"),
os.Getenv("VSPHERE_RESOURCE_POOL"),
os.Getenv("VSPHERE_NETWORK_LABEL_PXE"),
os.Getenv("VSPHERE_DATASTORE"),
)
}

func testAccResourceVSphereVirtualMachineConfigBasicAnnotation() string {
return fmt.Sprintf(`
variable "datacenter" {
Expand Down Expand Up @@ -3295,6 +3436,7 @@ resource "vsphere_virtual_machine" "vm" {
os.Getenv("VSPHERE_USE_LINKED_CLONE"),
)
}

func testAccResourceVSphereVirtualMachineConfigCloneTimeZone(zone string) string {
return fmt.Sprintf(`
variable "datacenter" {
Expand Down

0 comments on commit 539a1e4

Please sign in to comment.