diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index 3e2c7d463..255580f27 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -74,11 +74,19 @@ func resourceCloudStackInstance() *schema.Resource { }, "affinity_group_ids": &schema.Schema{ - Type: schema.TypeSet, - Optional: true, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + ConflictsWith: []string{"affinity_group_names"}, + }, + + "affinity_group_names": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + ConflictsWith: []string{"affinity_group_ids"}, }, "project": &schema.Schema{ @@ -221,6 +229,17 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) p.SetAffinitygroupids(groups) } + // If there is a affinity_group_names supplied, add it to the parameter struct + if agns := d.Get("affinity_group_names").(*schema.Set); agns.Len() > 0 { + var groups []string + + for _, group := range agns.List() { + groups = append(groups, group.(string)) + } + + p.SetAffinitygroupnames(groups) + } + // If there is a project supplied, we retrieve and set the project id if err := setProjectid(p, cs, d); err != nil { return err @@ -319,6 +338,15 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er d.Set("affinity_group_ids", groups) } + agns := &schema.Set{F: schema.HashString} + for _, group := range vm.Affinitygroup { + agns.Add(group.Name) + } + + if agns.Len() > 0 { + d.Set("affinity_group_names", agns) + } + return nil } diff --git a/website/source/docs/providers/cloudstack/r/instance.html.markdown b/website/source/docs/providers/cloudstack/r/instance.html.markdown index 9ed4a9db5..fbb22d0df 100644 --- a/website/source/docs/providers/cloudstack/r/instance.html.markdown +++ b/website/source/docs/providers/cloudstack/r/instance.html.markdown @@ -70,6 +70,9 @@ The following arguments are supported: gigabytes. The root disk is resized on deploy. Only applies to template-based deployments. +* `affinity_group_names` - (Optional) List of affinity groups to apply to this + instance. Changing this forces a new resource to be created. + ## Attributes Reference The following attributes are exported: