From cf607e8a5871aef57d2bdd3caef2b48f8e71b2d4 Mon Sep 17 00:00:00 2001 From: Jeff LaPlante Date: Tue, 5 Apr 2016 09:12:45 -0700 Subject: [PATCH 1/4] Added Group attribute to cloudstack instance resource --- .../cloudstack/resource_cloudstack_instance.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index 05898dc23..315b99ad3 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -93,6 +93,12 @@ func resourceCloudStackInstance() *schema.Resource { Optional: true, Default: false, }, + + "group": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, }, } } @@ -193,6 +199,11 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) p.SetUserdata(ud) } + // If there is a group supplied, add it to the parameter struct + if group, ok := d.GetOk("group"); ok { + p.SetGroup(group.(string)) + } + // Create the new instance r, err := cs.VirtualMachine.DeployVirtualMachine(p) if err != nil { From ba4ec0097a47820db3bd686fdb708eca3b717d74 Mon Sep 17 00:00:00 2001 From: Jeff LaPlante Date: Tue, 5 Apr 2016 09:21:23 -0700 Subject: [PATCH 2/4] fixed formatting --- .../cloudstack/resource_cloudstack_instance.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index 315b99ad3..75b1ce06e 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -94,11 +94,11 @@ func resourceCloudStackInstance() *schema.Resource { Default: false, }, - "group": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Computed: true, - }, + "group": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, }, } } @@ -199,10 +199,10 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) p.SetUserdata(ud) } - // If there is a group supplied, add it to the parameter struct - if group, ok := d.GetOk("group"); ok { - p.SetGroup(group.(string)) - } + // If there is a group supplied, add it to the parameter struct + if group, ok := d.GetOk("group"); ok { + p.SetGroup(group.(string)) + } // Create the new instance r, err := cs.VirtualMachine.DeployVirtualMachine(p) From f840f49fbbf429b0918599c0d91897ec143687e0 Mon Sep 17 00:00:00 2001 From: Jeff LaPlante Date: Mon, 11 Apr 2016 10:23:19 -0700 Subject: [PATCH 3/4] 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 From 014f2d5671471e58cfbece31999189686d1476dc Mon Sep 17 00:00:00 2001 From: Jeff LaPlante Date: Mon, 11 Apr 2016 10:26:46 -0700 Subject: [PATCH 4/4] Added group attribute to cloudstack instance documentation markdown --- .../source/docs/providers/cloudstack/r/instance.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/source/docs/providers/cloudstack/r/instance.html.markdown b/website/source/docs/providers/cloudstack/r/instance.html.markdown index 1351ab107..a78086f31 100644 --- a/website/source/docs/providers/cloudstack/r/instance.html.markdown +++ b/website/source/docs/providers/cloudstack/r/instance.html.markdown @@ -31,6 +31,8 @@ The following arguments are supported: * `display_name` - (Optional) The display name of the instance. +* `group` - (Optional) The group name of the instance. + * `service_offering` - (Required) The name or ID of the service offering used for this instance.