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

     1  package heroku
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/cyberdelia/heroku-go/v3"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceHerokuDrain() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceHerokuDrainCreate,
    18  		Read:   resourceHerokuDrainRead,
    19  		Delete: resourceHerokuDrainDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"url": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"app": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"token": {
    35  				Type:     schema.TypeString,
    36  				Computed: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  const retryableError = `App hasn't yet been assigned a log channel. Please try again momentarily.`
    43  
    44  func resourceHerokuDrainCreate(d *schema.ResourceData, meta interface{}) error {
    45  	client := meta.(*heroku.Service)
    46  
    47  	app := d.Get("app").(string)
    48  	url := d.Get("url").(string)
    49  
    50  	log.Printf("[DEBUG] Drain create configuration: %#v, %#v", app, url)
    51  
    52  	var dr *heroku.LogDrainCreateResult
    53  	err := resource.Retry(2*time.Minute, func() *resource.RetryError {
    54  		d, err := client.LogDrainCreate(context.TODO(), app, heroku.LogDrainCreateOpts{URL: url})
    55  		if err != nil {
    56  			if strings.Contains(err.Error(), retryableError) {
    57  				return resource.RetryableError(err)
    58  			}
    59  			return resource.NonRetryableError(err)
    60  		}
    61  		dr = d
    62  		return nil
    63  	})
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	d.SetId(dr.ID)
    69  	d.Set("url", dr.URL)
    70  	d.Set("token", dr.Token)
    71  
    72  	log.Printf("[INFO] Drain ID: %s", d.Id())
    73  	return nil
    74  }
    75  
    76  func resourceHerokuDrainDelete(d *schema.ResourceData, meta interface{}) error {
    77  	client := meta.(*heroku.Service)
    78  
    79  	log.Printf("[INFO] Deleting drain: %s", d.Id())
    80  
    81  	// Destroy the drain
    82  	_, err := client.LogDrainDelete(context.TODO(), d.Get("app").(string), d.Id())
    83  	if err != nil {
    84  		return fmt.Errorf("Error deleting drain: %s", err)
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func resourceHerokuDrainRead(d *schema.ResourceData, meta interface{}) error {
    91  	client := meta.(*heroku.Service)
    92  
    93  	dr, err := client.LogDrainInfo(context.TODO(), d.Get("app").(string), d.Id())
    94  	if err != nil {
    95  		return fmt.Errorf("Error retrieving drain: %s", err)
    96  	}
    97  
    98  	d.Set("url", dr.URL)
    99  	d.Set("token", dr.Token)
   100  
   101  	return nil
   102  }