2017-04-28 21:12:54 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/cyberdelia/heroku-go/v3"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceHerokuPipelineCoupling() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceHerokuPipelineCouplingCreate,
|
|
|
|
Read: resourceHerokuPipelineCouplingRead,
|
|
|
|
Delete: resourceHerokuPipelineCouplingDelete,
|
|
|
|
|
2017-05-17 16:53:08 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
2017-04-28 21:12:54 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
2017-05-17 16:53:08 +02:00
|
|
|
"app_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-04-28 21:12:54 +02:00
|
|
|
"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: validatePipelineStageName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceHerokuPipelineCouplingCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*heroku.Service)
|
|
|
|
|
2017-05-01 16:19:44 +02:00
|
|
|
opts := heroku.PipelineCouplingCreateOpts{
|
|
|
|
App: d.Get("app").(string),
|
|
|
|
Pipeline: d.Get("pipeline").(string),
|
|
|
|
Stage: d.Get("stage").(string),
|
|
|
|
}
|
2017-04-28 21:12:54 +02:00
|
|
|
|
|
|
|
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.(*heroku.Service)
|
|
|
|
|
|
|
|
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.(*heroku.Service)
|
|
|
|
|
|
|
|
p, err := client.PipelineCouplingInfo(context.TODO(), d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving pipeline: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-17 16:53:08 +02:00
|
|
|
// 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)
|
2017-04-28 21:12:54 +02:00
|
|
|
d.Set("stage", p.Stage)
|
2017-05-17 16:53:08 +02:00
|
|
|
d.Set("pipeline", p.Pipeline.ID)
|
2017-04-28 21:12:54 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|