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