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

     1  package logentries
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  	"github.com/logentries/le_goclient"
     6  )
     7  
     8  func resourceLogentriesLogSet() *schema.Resource {
     9  
    10  	return &schema.Resource{
    11  		Create: resourceLogentriesLogSetCreate,
    12  		Read:   resourceLogentriesLogSetRead,
    13  		Update: resourceLogentriesLogSetUpdate,
    14  		Delete: resourceLogentriesLogSetDelete,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			"location": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Optional: true,
    24  				Default:  "nonlocation",
    25  			},
    26  		},
    27  	}
    28  }
    29  
    30  func resourceLogentriesLogSetCreate(d *schema.ResourceData, meta interface{}) error {
    31  	client := meta.(*logentries.Client)
    32  	res, err := client.LogSet.Create(logentries.LogSetCreateRequest{
    33  		Name:     d.Get("name").(string),
    34  		Location: d.Get("location").(string),
    35  	})
    36  
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	d.SetId(res.Key)
    42  
    43  	return resourceLogentriesLogSetRead(d, meta)
    44  }
    45  
    46  func resourceLogentriesLogSetRead(d *schema.ResourceData, meta interface{}) error {
    47  	client := meta.(*logentries.Client)
    48  	res, err := client.LogSet.Read(logentries.LogSetReadRequest{
    49  		Key: d.Id(),
    50  	})
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	if res == nil {
    56  		d.SetId("")
    57  		return nil
    58  	}
    59  
    60  	d.Set("location", res.Location)
    61  	return nil
    62  }
    63  
    64  func resourceLogentriesLogSetUpdate(d *schema.ResourceData, meta interface{}) error {
    65  	client := meta.(*logentries.Client)
    66  	_, err := client.LogSet.Update(logentries.LogSetUpdateRequest{
    67  		Key:      d.Id(),
    68  		Name:     d.Get("name").(string),
    69  		Location: d.Get("location").(string),
    70  	})
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	return resourceLogentriesLogRead(d, meta)
    76  }
    77  
    78  func resourceLogentriesLogSetDelete(d *schema.ResourceData, meta interface{}) error {
    79  	client := meta.(*logentries.Client)
    80  	err := client.LogSet.Delete(logentries.LogSetDeleteRequest{
    81  		Key: d.Id(),
    82  	})
    83  	return err
    84  }