forked from heroku/terraform-provider-heroku
/
resource_heroku_pipeline_coupling.go
109 lines (88 loc) · 2.56 KB
/
resource_heroku_pipeline_coupling.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
102
103
104
105
106
107
108
109
package heroku
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/heroku/heroku-go/v3"
)
func resourceHerokuPipelineCoupling() *schema.Resource {
return &schema.Resource{
Create: resourceHerokuPipelineCouplingCreate,
Read: resourceHerokuPipelineCouplingRead,
Delete: resourceHerokuPipelineCouplingDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Computed: true,
},
"app": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"pipeline": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateUUID,
},
"stage": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(
[]string{"review", "development", "staging", "production"},
false,
),
},
},
}
}
func resourceHerokuPipelineCouplingCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).Api
opts := heroku.PipelineCouplingCreateOpts{
App: d.Get("app").(string),
Pipeline: d.Get("pipeline").(string),
Stage: d.Get("stage").(string),
}
log.Printf("[DEBUG] PipelineCoupling create configuration: %#v", opts)
p, err := client.PipelineCouplingCreate(context.TODO(), opts)
if err != nil {
return fmt.Errorf("Error creating pipeline: %s", err)
}
d.SetId(p.ID)
log.Printf("[INFO] PipelineCoupling ID: %s", d.Id())
return resourceHerokuPipelineCouplingRead(d, meta)
}
func resourceHerokuPipelineCouplingDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).Api
log.Printf("[INFO] Deleting pipeline: %s", d.Id())
_, err := client.PipelineCouplingDelete(context.TODO(), d.Id())
if err != nil {
return fmt.Errorf("Error deleting pipeline: %s", err)
}
return nil
}
func resourceHerokuPipelineCouplingRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).Api
p, err := client.PipelineCouplingInfo(context.TODO(), d.Id())
if err != nil {
return fmt.Errorf("Error retrieving pipeline: %s", err)
}
// grab App info
app, err := client.AppInfo(context.TODO(), p.App.ID)
if err != nil {
log.Printf("[WARN] Error looking up addional App info for pipeline coupling (%s): %s", d.Id(), err)
} else {
d.Set("app", app.Name)
}
d.Set("app_id", p.App.ID)
d.Set("stage", p.Stage)
d.Set("pipeline", p.Pipeline.ID)
return nil
}