From f840f49fbbf429b0918599c0d91897ec143687e0 Mon Sep 17 00:00:00 2001 From: Jeff LaPlante Date: Mon, 11 Apr 2016 10:23:19 -0700 Subject: [PATCH] Added support to read and update group attribute from existing vm state. --- .../resource_cloudstack_instance.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index 75b1ce06e..fbc0c4860 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -240,6 +240,8 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er d.Set("name", vm.Name) d.Set("display_name", vm.Displayname) d.Set("ipaddress", vm.Nic[0].Ipaddress) + d.Set("group", vm.Group) + //NB cloudstack sometimes sends back the wrong keypair name, so dont update it setValueOrID(d, "network", vm.Nic[0].Networkname, vm.Nic[0].Networkid) @@ -277,6 +279,26 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{}) d.SetPartial("display_name") } + // 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) + } + + d.SetPartial("group") + } + // Attributes that require reboot to update if d.HasChange("name") || d.HasChange("service_offering") || d.HasChange("keypair") { // Before we can actually make these changes, the virtual machine must be stopped