github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/heroku/resource_heroku_drain.go (about)

     1  package heroku
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/cyberdelia/heroku-go/v3"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceHerokuDrain() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceHerokuDrainCreate,
    14  		Read:   resourceHerokuDrainRead,
    15  		Delete: resourceHerokuDrainDelete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"url": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  
    24  			"app": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"token": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Computed: true,
    33  			},
    34  		},
    35  	}
    36  }
    37  
    38  func resourceHerokuDrainCreate(d *schema.ResourceData, meta interface{}) error {
    39  	client := meta.(*heroku.Service)
    40  
    41  	app := d.Get("app").(string)
    42  	url := d.Get("url").(string)
    43  
    44  	log.Printf("[DEBUG] Drain create configuration: %#v, %#v", app, url)
    45  
    46  	dr, err := client.LogDrainCreate(app, heroku.LogDrainCreateOpts{url})
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	d.SetId(dr.ID)
    52  	d.Set("url", dr.URL)
    53  	d.Set("token", dr.Token)
    54  
    55  	log.Printf("[INFO] Drain ID: %s", d.Id())
    56  	return nil
    57  }
    58  
    59  func resourceHerokuDrainDelete(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*heroku.Service)
    61  
    62  	log.Printf("[INFO] Deleting drain: %s", d.Id())
    63  
    64  	// Destroy the drain
    65  	err := client.LogDrainDelete(d.Get("app").(string), d.Id())
    66  	if err != nil {
    67  		return fmt.Errorf("Error deleting drain: %s", err)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func resourceHerokuDrainRead(d *schema.ResourceData, meta interface{}) error {
    74  	client := meta.(*heroku.Service)
    75  
    76  	dr, err := client.LogDrainInfo(d.Get("app").(string), d.Id())
    77  	if err != nil {
    78  		return fmt.Errorf("Error retrieving drain: %s", err)
    79  	}
    80  
    81  	d.Set("url", dr.URL)
    82  	d.Set("token", dr.Token)
    83  
    84  	return nil
    85  }