github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/librato/resource_librato_space.go (about)

     1  package librato
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  	"github.com/henrikhodne/go-librato/librato"
    12  )
    13  
    14  func resourceLibratoSpace() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceLibratoSpaceCreate,
    17  		Read:   resourceLibratoSpaceRead,
    18  		Update: resourceLibratoSpaceUpdate,
    19  		Delete: resourceLibratoSpaceDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: false,
    26  			},
    27  			"id": &schema.Schema{
    28  				Type:     schema.TypeInt,
    29  				Computed: true,
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func resourceLibratoSpaceCreate(d *schema.ResourceData, meta interface{}) error {
    36  	client := meta.(*librato.Client)
    37  
    38  	name := d.Get("name").(string)
    39  
    40  	space, _, err := client.Spaces.Create(&librato.Space{Name: librato.String(name)})
    41  	if err != nil {
    42  		return fmt.Errorf("Error creating Librato space %s: %s", name, err)
    43  	}
    44  
    45  	resource.Retry(1*time.Minute, func() *resource.RetryError {
    46  		_, _, err := client.Spaces.Get(*space.ID)
    47  		if err != nil {
    48  			if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
    49  				return resource.RetryableError(err)
    50  			}
    51  			return resource.NonRetryableError(err)
    52  		}
    53  		return nil
    54  	})
    55  
    56  	return resourceLibratoSpaceReadResult(d, space)
    57  }
    58  
    59  func resourceLibratoSpaceRead(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*librato.Client)
    61  
    62  	id, err := strconv.ParseUint(d.Id(), 10, 0)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	space, _, err := client.Spaces.Get(uint(id))
    68  	if err != nil {
    69  		if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
    70  			d.SetId("")
    71  			return nil
    72  		}
    73  		return fmt.Errorf("Error reading Librato Space %s: %s", d.Id(), err)
    74  	}
    75  
    76  	return resourceLibratoSpaceReadResult(d, space)
    77  }
    78  
    79  func resourceLibratoSpaceReadResult(d *schema.ResourceData, space *librato.Space) error {
    80  	d.SetId(strconv.FormatUint(uint64(*space.ID), 10))
    81  	if err := d.Set("id", *space.ID); err != nil {
    82  		return err
    83  	}
    84  	if err := d.Set("name", *space.Name); err != nil {
    85  		return err
    86  	}
    87  	return nil
    88  }
    89  
    90  func resourceLibratoSpaceUpdate(d *schema.ResourceData, meta interface{}) error {
    91  	client := meta.(*librato.Client)
    92  	id, err := strconv.ParseUint(d.Id(), 10, 0)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	if d.HasChange("name") {
    98  		newName := d.Get("name").(string)
    99  		log.Printf("[INFO] Modifying name space attribute for %d: %#v", id, newName)
   100  		if _, err = client.Spaces.Edit(uint(id), &librato.Space{Name: &newName}); err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return resourceLibratoSpaceRead(d, meta)
   106  }
   107  
   108  func resourceLibratoSpaceDelete(d *schema.ResourceData, meta interface{}) error {
   109  	client := meta.(*librato.Client)
   110  	id, err := strconv.ParseUint(d.Id(), 10, 0)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	log.Printf("[INFO] Deleting Space: %d", id)
   116  	_, err = client.Spaces.Delete(uint(id))
   117  	if err != nil {
   118  		if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
   119  			log.Printf("Space %s not found", d.Id())
   120  			d.SetId("")
   121  			return nil
   122  		}
   123  		return fmt.Errorf("Error deleting space: %s", err)
   124  	}
   125  
   126  	resource.Retry(1*time.Minute, func() *resource.RetryError {
   127  		_, _, err := client.Spaces.Get(uint(id))
   128  		if err != nil {
   129  			if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
   130  				return nil
   131  			}
   132  			return resource.NonRetryableError(err)
   133  		}
   134  		return resource.RetryableError(fmt.Errorf("space still exists"))
   135  	})
   136  
   137  	d.SetId("")
   138  	return nil
   139  }