2016-09-01 10:48:49 +02:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackAffinityGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackAffinityGroupCreate,
|
|
|
|
Read: resourceCloudStackAffinityGroupRead,
|
|
|
|
Delete: resourceCloudStackAffinityGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-09-01 11:19:37 +02:00
|
|
|
Computed: true,
|
2016-09-01 10:48:49 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
affinityGroupType := d.Get("type").(string)
|
|
|
|
|
2016-09-01 11:19:37 +02:00
|
|
|
// Create a new parameter struct
|
2016-09-01 10:48:49 +02:00
|
|
|
p := cs.AffinityGroup.NewCreateAffinityGroupParams(name, affinityGroupType)
|
|
|
|
|
|
|
|
// Set the description
|
|
|
|
if description, ok := d.GetOk("description"); ok {
|
|
|
|
p.SetDescription(description.(string))
|
2016-09-01 11:19:37 +02:00
|
|
|
} else {
|
|
|
|
p.SetDescription(name)
|
2016-09-01 10:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a project supplied, we retrieve and set the project id
|
|
|
|
if err := setProjectid(p, cs, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 11:19:37 +02:00
|
|
|
log.Printf("[DEBUG] Creating affinity group %s", name)
|
2016-09-01 10:48:49 +02:00
|
|
|
r, err := cs.AffinityGroup.CreateAffinityGroup(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 11:19:37 +02:00
|
|
|
log.Printf("[DEBUG] Affinity group %s successfully created", name)
|
2016-09-01 10:48:49 +02:00
|
|
|
d.SetId(r.Id)
|
|
|
|
|
|
|
|
return resourceCloudStackAffinityGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackAffinityGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2016-09-01 11:19:37 +02:00
|
|
|
log.Printf("[DEBUG] Rerieving affinity group %s", d.Get("name").(string))
|
2016-09-01 10:48:49 +02:00
|
|
|
|
|
|
|
// Get the affinity group details
|
2016-09-01 11:19:37 +02:00
|
|
|
ag, count, err := cs.AffinityGroup.GetAffinityGroupByID(
|
|
|
|
d.Id(),
|
|
|
|
cloudstack.WithProject(d.Get("project").(string)),
|
|
|
|
)
|
2016-09-01 10:48:49 +02:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
2016-09-01 11:19:37 +02:00
|
|
|
log.Printf("[DEBUG] Affinity group %s does not longer exist", d.Get("name").(string))
|
2016-09-01 10:48:49 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 11:19:37 +02:00
|
|
|
|
2016-09-01 10:48:49 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 11:19:37 +02:00
|
|
|
// Update the config
|
2016-09-01 10:48:49 +02:00
|
|
|
d.Set("name", ag.Name)
|
|
|
|
d.Set("description", ag.Description)
|
|
|
|
d.Set("type", ag.Type)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackAffinityGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.AffinityGroup.NewDeleteAffinityGroupParams()
|
|
|
|
p.SetId(d.Id())
|
|
|
|
|
|
|
|
// If there is a project supplied, we retrieve and set the project id
|
|
|
|
if err := setProjectid(p, cs, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 11:19:37 +02:00
|
|
|
// Delete the affinity group
|
2016-09-01 10:48:49 +02:00
|
|
|
_, err := cs.AffinityGroup.DeleteAffinityGroup(p)
|
|
|
|
if err != nil {
|
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf(
|
|
|
|
"Invalid parameter id value=%s due to incorrect long value format, "+
|
|
|
|
"or entity does not exist", d.Id())) {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 11:19:37 +02:00
|
|
|
|
2016-09-01 10:48:49 +02:00
|
|
|
return fmt.Errorf("Error deleting affinity group: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|