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

provider/cloudstack: add the option to assign affinity groups to a VM #6898

Merged
merged 1 commit into from
May 27, 2016
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
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