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

Bugfix/power_state_mechanism #99

Closed
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
27 changes: 27 additions & 0 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,20 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
}

if d.HasChange("disk_list") {
preCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
return err
}
if res.DiskList, err = expandDiskList(d, false); err != nil {
return err
}
postCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
return err
}
if preCdromCount != postCdromCount {
hotPlugChange = false
}
}

if d.HasChange("serial_port_list") {
Expand Down Expand Up @@ -1546,6 +1557,10 @@ func getVMResources(d *schema.ResourceData, vm *v3.VMResources) error {
vm.VgaConsoleEnabled = utils.BoolPtr(v.(bool))
}
if v, ok := d.GetOk("power_state_mechanism"); ok {
if vm.PowerStateMechanism == nil {
log.Printf("m.PowerStateMechanism was nil, setting correct value!")
vm.PowerStateMechanism = &v3.VMPowerStateMechanism{}
}
vm.PowerStateMechanism.Mechanism = utils.StringPtr(v.(string))
}
if v, ok := d.GetOk("should_fail_on_script_failure"); ok {
Expand Down Expand Up @@ -1913,3 +1928,15 @@ func waitForIPRefreshFunc(client *v3.Client, vmUUID string) resource.StateRefres
return resp, WAITING, nil
}
}

func CountDiskListCdrom(dl []*v3.VMDisk) (int, error) {
log.Printf("=====")
counter := 0
for _, v := range dl {
if *v.DeviceProperties.DeviceType == "CDROM" {
counter++
}
}
log.Printf("=====")
return counter, nil
}
98 changes: 98 additions & 0 deletions nutanix/resource_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,62 @@ func TestAccNutanixVirtualMachine_WithSerialPortList(t *testing.T) {
})
}

func TestAccNutanixVirtualMachine_PowerStateMechanism(t *testing.T) {
r := acctest.RandInt()
resourceName := "nutanix_virtual_machine.vm6"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixVMConfigPowerStateMechanism(r),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixVirtualMachineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "power_state_mechanism", "ACPI"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disk_list"},
},
},
})
}

func TestAccNutanixVirtualMachine_CdromGuestCustomisationReboot(t *testing.T) {
r := acctest.RandInt()
resourceName := "nutanix_virtual_machine.vm7"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixVMConfigCdromGuestCustomisationReboot(r),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixVirtualMachineExists(resourceName),
),
ExpectNonEmptyPlan: true,
},
{
Config: testAccNutanixVMConfigCdromGuestCustomisationReboot(r),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixVirtualMachineExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disk_list"},
},
},
})
}

func testAccCheckNutanixVirtualMachineExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -531,3 +587,45 @@ resource "nutanix_virtual_machine" "vm5" {
}
`, r)
}

func testAccNutanixVMConfigPowerStateMechanism(r int) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}

locals {
cluster1 = "${data.nutanix_clusters.clusters.entities.0.service_list.0 == "PRISM_CENTRAL"
? data.nutanix_clusters.clusters.entities.1.metadata.uuid : data.nutanix_clusters.clusters.entities.0.metadata.uuid}"
}

resource "nutanix_virtual_machine" "vm6" {
name = "test-dou-%d"
cluster_uuid = "${local.cluster1}"

num_vcpus_per_socket = 1
num_sockets = 1
memory_size_mib = 186
power_state_mechanism = "ACPI"
}
`, r)
}

func testAccNutanixVMConfigCdromGuestCustomisationReboot(r int) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}

locals {
cluster1 = "${data.nutanix_clusters.clusters.entities.0.service_list.0 == "PRISM_CENTRAL"
? data.nutanix_clusters.clusters.entities.1.metadata.uuid : data.nutanix_clusters.clusters.entities.0.metadata.uuid}"
}

resource "nutanix_virtual_machine" "vm7" {
name = "test-dou-%d"
cluster_uuid = "${local.cluster1}"

num_vcpus_per_socket = 1
num_sockets = 1
memory_size_mib = 186
guest_customization_cloud_init_user_data = base64encode("#cloud-config\nfqdn: test.domain.local")
}
`, r)
}