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

Patch to allow some changes to be hotplug #75

Merged
merged 5 commits into from
Aug 21, 2019
Merged
Changes from 4 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
62 changes: 55 additions & 7 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,13 +981,15 @@ func resourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{})

func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
var hotPlugChange = true

log.Printf("[Debug] Updating VM values %s", d.Id())

//First, shutDown the VM.
marinsalinas marked this conversation as resolved.
Show resolved Hide resolved
if err := changePowerState(conn, d.Id(), "OFF"); err != nil {
return fmt.Errorf("internal error: cannot shut down the VM with UUID(%s): %s", d.Id(), err)
}
//skip it here, It will depend on the changes
//if err := changePowerState(conn, d.Id(), "OFF"); err != nil {
// return fmt.Errorf("internal error: cannot shut down the VM with UUID(%s): %s", d.Id(), err)
//}

request := &v3.VMIntentInput{}
metadata := &v3.Metadata{}
Expand Down Expand Up @@ -1020,7 +1022,7 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if d.HasChange("categories") {
catl := d.Get("categories").(map[string]interface{})
metadata.Categories = expandCategories(catl)

hotPlugChange = false
}
metadata.OwnerReference = response.Metadata.OwnerReference
if d.HasChange("owner_reference") {
Expand All @@ -1031,12 +1033,15 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if d.HasChange("project_reference") {
_, n := d.GetChange("project_reference")
metadata.ProjectReference = validateRef(n.(map[string]interface{}))
hotPlugChange = false
}
// hotplug option
spec.Name = response.Status.Name
if d.HasChange("name") {
_, n := d.GetChange("name")
spec.Name = utils.StringPtr(n.(string))
}
// hotplug option
spec.Description = response.Status.Description
if d.HasChange("description") {
_, n := d.GetChange("description")
Expand All @@ -1046,53 +1051,74 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if d.HasChange("availability_zone_reference") {
_, n := d.GetChange("availability_zone_reference")
spec.AvailabilityZoneReference = validateRef(n.(map[string]interface{}))
hotPlugChange = false
}
spec.ClusterReference = response.Status.ClusterReference
if d.HasChange("cluster_uuid") {
_, n := d.GetChange("cluster_uuid")
spec.ClusterReference = buildReference(n.(string), "cluster")
hotPlugChange = false
}
if d.HasChange("parent_reference") {
_, n := d.GetChange("parent_reference")
res.ParentReference = validateRef(n.(map[string]interface{}))
hotPlugChange = false
}
if d.HasChange("num_vnuma_nodes") {
_, n := d.GetChange("num_vnuma_nodes")
res.VMVnumaConfig = &v3.VMVnumaConfig{
NumVnumaNodes: utils.Int64Ptr(int64(n.(int))),
}
hotPlugChange = false
}
if d.HasChange("guest_os_id") {
_, n := d.GetChange("guest_os_id")
res.GuestOsID = utils.StringPtr(n.(string))
hotPlugChange = false
}
// hotplug option just in case of higher value being applied
if d.HasChange("num_vcpus_per_socket") {
_, n := d.GetChange("num_vcpus_per_socket")
o, n := d.GetChange("num_vcpus_per_socket")
res.NumVcpusPerSocket = utils.Int64Ptr(int64(n.(int)))
if n.(int) < o.(int) {
hotPlugChange = false
}
}
// hotplug option just in case of higher value being applied
if d.HasChange("num_sockets") {
_, n := d.GetChange("num_sockets")
o, n := d.GetChange("num_sockets")
res.NumSockets = utils.Int64Ptr(int64(n.(int)))
if n.(int) < o.(int) {
hotPlugChange = false
}
}
// hotplug option just in case of higher value being applied
if d.HasChange("memory_size_mib") {
_, n := d.GetChange("memory_size_mib")
o, n := d.GetChange("memory_size_mib")
res.MemorySizeMib = utils.Int64Ptr(int64(n.(int)))
if n.(int) < o.(int) {
hotPlugChange = false
}
}
if d.HasChange("hardware_clock_timezone") {
_, n := d.GetChange("hardware_clock_timezone")
res.HardwareClockTimezone = utils.StringPtr(n.(string))
hotPlugChange = false
}
if d.HasChange("vga_console_enabled") {
_, n := d.GetChange("vga_console_enabled")
res.VgaConsoleEnabled = utils.BoolPtr(n.(bool))
hotPlugChange = false
}
if d.HasChange("guest_customization_is_overridable") {
_, n := d.GetChange("guest_customization_is_overridable")
guest.IsOverridable = utils.BoolPtr(n.(bool))
hotPlugChange = false
}
if d.HasChange("power_state_mechanism") {
_, n := d.GetChange("power_state_mechanism")
pw.Mechanism = utils.StringPtr(n.(string))
hotPlugChange = false
}
if d.HasChange("power_state_guest_transition_config") {
_, n := d.GetChange("power_state_guest_transition_config")
Expand All @@ -1106,6 +1132,7 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
p.ShouldFailOnScriptFailure = utils.BoolPtr(v.(bool))
}
pw.GuestTransitionConfig = p
hotPlugChange = false
}

cloudInit := guest.CloudInit
Expand All @@ -1117,16 +1144,19 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if d.HasChange("guest_customization_cloud_init_user_data") {
_, n := d.GetChange("guest_customization_cloud_init_user_data")
cloudInit.UserData = utils.StringPtr(n.(string))
hotPlugChange = false
}

if d.HasChange("guest_customization_cloud_init_meta_data") {
_, n := d.GetChange("guest_customization_cloud_init_meta_data")
cloudInit.MetaData = utils.StringPtr(n.(string))
hotPlugChange = false
}

if d.HasChange("guest_customization_cloud_init_custom_key_values") {
_, n := d.GetChange("guest_customization_cloud_init_custom_key_values")
cloudInit.CustomKeyValues = n.(map[string]string)
hotPlugChange = false
}

if !reflect.DeepEqual(*cloudInit, (v3.GuestCustomizationCloudInit{})) {
Expand All @@ -1141,13 +1171,15 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
InstallType: validateMapStringValue(a, "install_type"),
UnattendXML: validateMapStringValue(a, "unattend_xml"),
}
hotPlugChange = false
}
if d.HasChange("guest_customization_sysprep_custom_key_values") {
if guest.Sysprep == nil {
guest.Sysprep = &v3.GuestCustomizationSysprep{}
}
_, n := d.GetChange("guest_customization_sysprep_custom_key_values")
guest.Sysprep.CustomKeyValues = n.(map[string]string)
hotPlugChange = false
}
if d.HasChange("nic_list") {
res.NicList = expandNicList(d)
Expand All @@ -1161,6 +1193,7 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{

if d.HasChange("serial_port_list") {
res.SerialPortList = expandSerialPortList(d)
hotPlugChange = false
}

if d.HasChange("nutanix_guest_tools") || d.HasChange("ngt_credentials") || d.HasChange("ngt_enabled_capability_list") {
Expand All @@ -1169,10 +1202,12 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{

if d.HasChange("gpu_list") {
res.GpuList = expandGPUList(d)
hotPlugChange = false
}
if d.HasChange("boot_device_order_list") {
_, n := d.GetChange("boot_device_order_list")
boot.BootDeviceOrderList = expandStringList(n.([]interface{}))
hotPlugChange = false
}

bd := &v3.VMBootDevice{}
Expand All @@ -1184,10 +1219,12 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
DeviceIndex: validateMapIntValue(dai, "device_index"),
AdapterType: validateMapStringValue(dai, "adapter_type"),
}
hotPlugChange = false
}
if d.HasChange("boot_device_mac_address") {
_, n := d.GetChange("boot_device_mac_address")
bd.MacAddress = utils.StringPtr(n.(string))
hotPlugChange = false
}
boot.BootDevice = bd

Expand All @@ -1206,6 +1243,17 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
res.GuestCustomization = guest
}

// If there are non-hotPlug changes, then poweroff is needed
if !hotPlugChange {
if err := changePowerState(conn, d.Id(), "OFF"); err != nil {
return fmt.Errorf("internal error: cannot shut down the VM with UUID(%s): %s", d.Id(), err)
}
// SpecVersion has changed due previous poweroff, increasing it manually (without reading the value again)
mySpec := *metadata.SpecVersion
mySpec += 2
metadata.SpecVersion = &mySpec
}

spec.Resources = res
request.Metadata = metadata
request.Spec = spec
Expand Down