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

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