Skip to content

Commit

Permalink
Merge pull request #6898 from svanharmelen/f-add-affinitygroup
Browse files Browse the repository at this point in the history
provider/cloudstack: add the option to assign affinity groups to a VM
  • Loading branch information
Sander van Harmelen committed May 27, 2016
2 parents fcaad20 + 23801b5 commit 22e914a
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion builtin/providers/cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func resourceCloudStackInstance() *schema.Resource {
ForceNew: true,
},

"affinity_group_ids": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -197,6 +205,16 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetIpaddress(ipaddress.(string))
}

if ags := d.Get("affinity_group_ids").(*schema.Set); ags.Len() > 0 {
var groups []string

for _, group := range ags.List() {
groups = append(groups, group.(string))
}

p.SetAffinitygroupids(groups)
}

// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
Expand Down Expand Up @@ -281,6 +299,15 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
setValueOrID(d, "project", vm.Project, vm.Projectid)
setValueOrID(d, "zone", vm.Zonename, vm.Zoneid)

groups := &schema.Set{F: schema.HashString}
for _, group := range vm.Affinitygroup {
groups.Add(group.Id)
}

if groups.Len() > 0 {
d.Set("affinity_group_ids", groups)
}

return nil
}

Expand Down Expand Up @@ -331,7 +358,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
}

// Attributes that require reboot to update
if d.HasChange("name") || d.HasChange("service_offering") || d.HasChange("keypair") {
if d.HasChange("name") || d.HasChange("service_offering") || d.HasChange("affinity_group_ids") || d.HasChange("keypair") {
// Before we can actually make these changes, the virtual machine must be stopped
_, err := cs.VirtualMachine.StopVirtualMachine(
cs.VirtualMachine.NewStopVirtualMachineParams(d.Id()))
Expand Down Expand Up @@ -382,6 +409,19 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("service_offering")
}

if d.HasChange("affinity_group_ids") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}

if ags := d.Get("affinity_group_ids").(*schema.Set); ags.Len() > 0 {
for _, group := range ags.List() {
groups = append(groups, group.(string))
}
}

p.SetAffinitygroupids(groups)
}

if d.HasChange("keypair") {
log.Printf("[DEBUG] SSH keypair changed for %s, starting update", name)

Expand Down

0 comments on commit 22e914a

Please sign in to comment.