github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/cloudstack/resource_cloudstack_affinity_group.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func resourceCloudStackAffinityGroup() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackAffinityGroupCreate,
    15  		Read:   resourceCloudStackAffinityGroupRead,
    16  		Delete: resourceCloudStackAffinityGroupDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"description": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				Computed: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"type": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"project": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				ForceNew: true,
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interface{}) error {
    48  	cs := meta.(*cloudstack.CloudStackClient)
    49  
    50  	name := d.Get("name").(string)
    51  	affinityGroupType := d.Get("type").(string)
    52  
    53  	// Create a new parameter struct
    54  	p := cs.AffinityGroup.NewCreateAffinityGroupParams(name, affinityGroupType)
    55  
    56  	// Set the description
    57  	if description, ok := d.GetOk("description"); ok {
    58  		p.SetDescription(description.(string))
    59  	} else {
    60  		p.SetDescription(name)
    61  	}
    62  
    63  	// If there is a project supplied, we retrieve and set the project id
    64  	if err := setProjectid(p, cs, d); err != nil {
    65  		return err
    66  	}
    67  
    68  	log.Printf("[DEBUG] Creating affinity group %s", name)
    69  	r, err := cs.AffinityGroup.CreateAffinityGroup(p)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	log.Printf("[DEBUG] Affinity group %s successfully created", name)
    75  	d.SetId(r.Id)
    76  
    77  	return resourceCloudStackAffinityGroupRead(d, meta)
    78  }
    79  
    80  func resourceCloudStackAffinityGroupRead(d *schema.ResourceData, meta interface{}) error {
    81  	cs := meta.(*cloudstack.CloudStackClient)
    82  
    83  	log.Printf("[DEBUG] Rerieving affinity group %s", d.Get("name").(string))
    84  
    85  	// Get the affinity group details
    86  	ag, count, err := cs.AffinityGroup.GetAffinityGroupByID(
    87  		d.Id(),
    88  		cloudstack.WithProject(d.Get("project").(string)),
    89  	)
    90  	if err != nil {
    91  		if count == 0 {
    92  			log.Printf("[DEBUG] Affinity group %s does not longer exist", d.Get("name").(string))
    93  			d.SetId("")
    94  			return nil
    95  		}
    96  
    97  		return err
    98  	}
    99  
   100  	// Update the config
   101  	d.Set("name", ag.Name)
   102  	d.Set("description", ag.Description)
   103  	d.Set("type", ag.Type)
   104  
   105  	return nil
   106  }
   107  
   108  func resourceCloudStackAffinityGroupDelete(d *schema.ResourceData, meta interface{}) error {
   109  	cs := meta.(*cloudstack.CloudStackClient)
   110  
   111  	// Create a new parameter struct
   112  	p := cs.AffinityGroup.NewDeleteAffinityGroupParams()
   113  	p.SetId(d.Id())
   114  
   115  	// If there is a project supplied, we retrieve and set the project id
   116  	if err := setProjectid(p, cs, d); err != nil {
   117  		return err
   118  	}
   119  
   120  	// Delete the affinity group
   121  	_, err := cs.AffinityGroup.DeleteAffinityGroup(p)
   122  	if err != nil {
   123  		// This is a very poor way to be told the ID does no longer exist :(
   124  		if strings.Contains(err.Error(), fmt.Sprintf(
   125  			"Invalid parameter id value=%s due to incorrect long value format, "+
   126  				"or entity does not exist", d.Id())) {
   127  			return nil
   128  		}
   129  
   130  		return fmt.Errorf("Error deleting affinity group: %s", err)
   131  	}
   132  
   133  	return nil
   134  }