2014-08-19 18:18:45 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
2017-03-10 13:00:03 +01:00
|
|
|
"context"
|
2014-08-19 18:18:45 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-01-25 16:55:44 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-08-19 18:18:45 +02:00
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
"github.com/cyberdelia/heroku-go/v3"
|
2016-01-25 16:55:44 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-08-19 18:18:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceHerokuDrain() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceHerokuDrainCreate,
|
|
|
|
Read: resourceHerokuDrainRead,
|
|
|
|
Delete: resourceHerokuDrainDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-10 13:00:03 +01:00
|
|
|
"url": {
|
2014-08-19 18:18:45 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
"app": {
|
2014-08-19 18:18:45 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
"token": {
|
2014-08-19 18:18:45 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 16:55:44 +01:00
|
|
|
const retryableError = `App hasn't yet been assigned a log channel. Please try again momentarily.`
|
|
|
|
|
2014-08-19 18:18:45 +02:00
|
|
|
func resourceHerokuDrainCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := meta.(*heroku.Service)
|
2014-08-19 18:18:45 +02:00
|
|
|
|
|
|
|
app := d.Get("app").(string)
|
|
|
|
url := d.Get("url").(string)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Drain create configuration: %#v, %#v", app, url)
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
var dr *heroku.LogDrainCreateResult
|
2016-03-09 23:53:32 +01:00
|
|
|
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
|
2017-03-10 13:00:03 +01:00
|
|
|
d, err := client.LogDrainCreate(context.TODO(), app, heroku.LogDrainCreateOpts{URL: url})
|
2016-01-25 16:55:44 +01:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), retryableError) {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(err)
|
2016-01-25 16:55:44 +01:00
|
|
|
}
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2016-01-25 16:55:44 +01:00
|
|
|
}
|
|
|
|
dr = d
|
|
|
|
return nil
|
|
|
|
})
|
2014-08-19 18:18:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
d.SetId(dr.ID)
|
2014-08-19 18:18:45 +02:00
|
|
|
d.Set("url", dr.URL)
|
|
|
|
d.Set("token", dr.Token)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Drain ID: %s", d.Id())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceHerokuDrainDelete(d *schema.ResourceData, meta interface{}) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := meta.(*heroku.Service)
|
2014-08-19 18:18:45 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting drain: %s", d.Id())
|
|
|
|
|
|
|
|
// Destroy the drain
|
2017-03-10 13:00:03 +01:00
|
|
|
_, err := client.LogDrainDelete(context.TODO(), d.Get("app").(string), d.Id())
|
2014-08-19 18:18:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting drain: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceHerokuDrainRead(d *schema.ResourceData, meta interface{}) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := meta.(*heroku.Service)
|
2014-08-19 18:18:45 +02:00
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
dr, err := client.LogDrainInfo(context.TODO(), d.Get("app").(string), d.Id())
|
2014-08-19 18:18:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving drain: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("url", dr.URL)
|
|
|
|
d.Set("token", dr.Token)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|