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