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 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
55 changes: 46 additions & 9 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,14 +981,10 @@ 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.
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{}
res := &v3.VMResources{}
Expand Down Expand Up @@ -1020,7 +1016,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,6 +1027,7 @@ 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
}
spec.Name = response.Status.Name
if d.HasChange("name") {
Expand All @@ -1046,53 +1043,71 @@ 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
}
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
}
}
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
}
}
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 +1121,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 +1133,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 +1160,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 +1182,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 +1191,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 +1208,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 +1232,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