in cloudstack/resource_cloudstack_instance.go [512:741]
func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
// Check if the display name is changed and if so, update the virtual machine
if d.HasChange("display_name") {
log.Printf("[DEBUG] Display name changed for %s, starting update", name)
// Create a new parameter struct
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
// Set the new display name
p.SetDisplayname(d.Get("display_name").(string))
// Update the display name
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the display name for instance %s: %s", name, err)
}
}
// Check if the group is changed and if so, update the virtual machine
if d.HasChange("group") {
log.Printf("[DEBUG] Group changed for %s, starting update", name)
// Create a new parameter struct
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
// Set the new group
p.SetGroup(d.Get("group").(string))
// Update the display name
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the group for instance %s: %s", name, err)
}
}
// Attributes that require reboot to update
if d.HasChange("name") || d.HasChange("service_offering") || d.HasChange("affinity_group_ids") ||
d.HasChange("affinity_group_names") || d.HasChange("keypair") || d.HasChange("keypairs") || d.HasChange("user_data") {
// Before we can actually make these changes, the virtual machine must be stopped
_, err := cs.VirtualMachine.StopVirtualMachine(
cs.VirtualMachine.NewStopVirtualMachineParams(d.Id()))
if err != nil {
return fmt.Errorf(
"Error stopping instance %s before making changes: %s", name, err)
}
// Check if the name has changed and if so, update the name
if d.HasChange("name") {
log.Printf("[DEBUG] Name for %s changed to %s, starting update", d.Id(), name)
// Create a new parameter struct
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
// Set the new name
p.SetName(name)
// Update the display name
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the name for instance %s: %s", name, err)
}
}
// Check if the service offering is changed and if so, update the offering
if d.HasChange("service_offering") {
log.Printf("[DEBUG] Service offering changed for %s, starting update", name)
// Retrieve the service_offering ID
serviceofferingid, e := retrieveID(cs, "service_offering", d.Get("service_offering").(string))
if e != nil {
return e.Error()
}
// Create a new parameter struct
p := cs.VirtualMachine.NewChangeServiceForVirtualMachineParams(d.Id(), serviceofferingid)
// Change the service offering
_, err = cs.VirtualMachine.ChangeServiceForVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error changing the service offering for instance %s: %s", name, err)
}
}
// Check if the affinity group IDs have changed and if so, update the IDs
if d.HasChange("affinity_group_ids") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}
if agIDs := d.Get("affinity_group_ids").(*schema.Set); agIDs.Len() > 0 {
for _, group := range agIDs.List() {
groups = append(groups, group.(string))
}
}
// Set the new groups
p.SetAffinitygroupids(groups)
// Update the affinity groups
_, err = cs.AffinityGroup.UpdateVMAffinityGroup(p)
if err != nil {
return fmt.Errorf(
"Error updating the affinity groups for instance %s: %s", name, err)
}
}
// Check if the affinity group names have changed and if so, update the names
if d.HasChange("affinity_group_names") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}
if agNames := d.Get("affinity_group_names").(*schema.Set); agNames.Len() > 0 {
for _, group := range agNames.List() {
groups = append(groups, group.(string))
}
}
// Set the new groups
p.SetAffinitygroupnames(groups)
// Update the affinity groups
_, err = cs.AffinityGroup.UpdateVMAffinityGroup(p)
if err != nil {
return fmt.Errorf(
"Error updating the affinity groups for instance %s: %s", name, err)
}
}
// Check if the keypair has changed and if so, update the keypair
if d.HasChange("keypair") || d.HasChange("keypairs") {
log.Printf("[DEBUG] SSH keypair(s) changed for %s, starting update", name)
p := cs.SSH.NewResetSSHKeyForVirtualMachineParams(d.Id())
if keypair, ok := d.GetOk("keypair"); ok {
p.SetKeypair(keypair.(string))
}
if keypairs, ok := d.GetOk("keypairs"); ok {
// Convert keypairsInterface to []interface{}
keypairsInterfaces := keypairs.([]interface{})
// Now, safely convert []interface{} to []string with error handling
strKeyPairs := make([]string, len(keypairsInterfaces))
for i, v := range keypairsInterfaces {
switch v := v.(type) {
case string:
strKeyPairs[i] = v
default:
log.Printf("Value at index %d is not a string: %v", i, v)
continue
}
}
p.SetKeypairs(strKeyPairs)
}
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// Change the ssh keypair
_, err = cs.SSH.ResetSSHKeyForVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error changing the SSH keypair(s) for instance %s: %s", name, err)
}
}
// Check if the user data has changed and if so, update the user data
if d.HasChange("user_data") {
log.Printf("[DEBUG] user_data changed for %s, starting update", name)
ud, err := getUserData(d.Get("user_data").(string))
if err != nil {
return err
}
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
p.SetUserdata(ud)
_, err = cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating user_data for instance %s: %s", name, err)
}
}
// Start the virtual machine again
_, err = cs.VirtualMachine.StartVirtualMachine(
cs.VirtualMachine.NewStartVirtualMachineParams(d.Id()))
if err != nil {
return fmt.Errorf(
"Error starting instance %s after making changes", name)
}
}
// Check if the tags have changed and if so, update the tags
if d.HasChange("tags") {
if err := updateTags(cs, d, "UserVm"); err != nil {
return fmt.Errorf("Error updating tags on instance %s: %s", name, err)
}
}
// Check if the details have changed and if so, update the details
if d.HasChange("details") {
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
vmDetails := make(map[string]string)
if details := d.Get("details"); details != nil {
for k, v := range details.(map[string]interface{}) {
vmDetails[k] = v.(string)
}
}
p.SetDetails(vmDetails)
}
return resourceCloudStackInstanceRead(d, meta)
}