2014-07-23 06:03:30 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2014-07-23 17:09:05 +02:00
|
|
|
"github.com/bgentry/heroku-go"
|
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
2014-07-23 06:03:30 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
2014-07-23 18:09:27 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
2014-07-23 06:03:30 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
// type application is used to store all the details of a heroku app
|
|
|
|
type application struct {
|
|
|
|
Id string // Id of the resource
|
|
|
|
|
|
|
|
App *heroku.App // The heroku application
|
|
|
|
Client *heroku.Client // Client to interact with the heroku API
|
|
|
|
Vars map[string]string // The vars on the application
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the application to have the latest from remote
|
|
|
|
func (a *application) Update() error {
|
|
|
|
var errs []error
|
|
|
|
var err error
|
|
|
|
|
|
|
|
a.App, err = a.Client.AppInfo(a.Id)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Vars, err = retrieve_config_vars(a.Id, a.Client)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &multierror.Error{Errors: errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-23 06:03:30 +02:00
|
|
|
func resource_heroku_app_create(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
// Build up our creation options
|
|
|
|
opts := heroku.AppCreateOpts{}
|
|
|
|
|
|
|
|
if attr := rs.Attributes["name"]; attr != "" {
|
|
|
|
opts.Name = &attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr := rs.Attributes["region"]; attr != "" {
|
|
|
|
opts.Region = &attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr := rs.Attributes["stack"]; attr != "" {
|
|
|
|
opts.Stack = &attr
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] App create configuration: %#v", opts)
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
a, err := client.AppCreate(&opts)
|
2014-07-23 06:03:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
rs.ID = a.Name
|
2014-07-23 06:03:30 +02:00
|
|
|
log.Printf("[INFO] App ID: %s", rs.ID)
|
|
|
|
|
2014-07-23 17:09:05 +02:00
|
|
|
if attr, ok := rs.Attributes["config_vars.#"]; ok && attr == "1" {
|
|
|
|
vs := flatmap.Expand(
|
|
|
|
rs.Attributes, "config_vars").([]interface{})
|
|
|
|
|
2014-07-23 17:26:31 +02:00
|
|
|
err = update_config_vars(rs.ID, vs, client)
|
2014-07-23 17:09:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
app, err := resource_heroku_app_retrieve(rs.ID, client)
|
2014-07-23 17:09:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
2014-07-23 06:03:30 +02:00
|
|
|
return resource_heroku_app_update_state(rs, app)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_heroku_app_update(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
2014-07-23 18:38:45 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["name"]; ok {
|
|
|
|
opts := heroku.AppUpdateOpts{
|
|
|
|
Name: &attr.New,
|
|
|
|
}
|
|
|
|
|
2014-07-23 20:12:53 +02:00
|
|
|
renamedApp, err := client.AppUpdate(rs.ID, &opts)
|
2014-07-23 18:38:45 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2014-07-23 20:12:53 +02:00
|
|
|
// Store the new ID
|
|
|
|
rs.ID = renamedApp.Name
|
2014-07-23 18:38:45 +02:00
|
|
|
}
|
2014-07-23 06:03:30 +02:00
|
|
|
|
2014-07-23 20:46:12 +02:00
|
|
|
attr, ok := s.Attributes["config_vars.#"]
|
|
|
|
|
|
|
|
// If the config var block was removed, nuke all config vars
|
|
|
|
if ok && attr == "1" {
|
2014-07-23 19:56:47 +02:00
|
|
|
vs := flatmap.Expand(
|
|
|
|
rs.Attributes, "config_vars").([]interface{})
|
|
|
|
|
|
|
|
err := update_config_vars(rs.ID, vs, client)
|
2014-07-23 20:46:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
} else if ok && attr == "0" {
|
|
|
|
log.Println("[INFO] Config vars removed, removing all vars")
|
|
|
|
|
|
|
|
err := update_config_vars(rs.ID, make([]interface{}, 0), client)
|
|
|
|
|
2014-07-23 19:56:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:38:45 +02:00
|
|
|
app, err := resource_heroku_app_retrieve(rs.ID, client)
|
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
2014-07-23 06:03:30 +02:00
|
|
|
|
2014-07-23 18:38:45 +02:00
|
|
|
return resource_heroku_app_update_state(rs, app)
|
2014-07-23 06:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_heroku_app_destroy(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting App: %s", s.ID)
|
|
|
|
|
|
|
|
// Destroy the app
|
|
|
|
err := client.AppDelete(s.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting App: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_heroku_app_refresh(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
|
|
|
|
app, err := resource_heroku_app_retrieve(s.ID, client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_heroku_app_update_state(s, app)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_heroku_app_diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
|
|
|
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
2014-07-23 20:12:53 +02:00
|
|
|
"name": diff.AttrTypeUpdate,
|
2014-07-23 17:09:05 +02:00
|
|
|
"region": diff.AttrTypeUpdate,
|
|
|
|
"stack": diff.AttrTypeCreate,
|
|
|
|
"config_vars": diff.AttrTypeUpdate,
|
2014-07-23 06:03:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ComputedAttrs: []string{
|
|
|
|
"name",
|
|
|
|
"region",
|
|
|
|
"stack",
|
|
|
|
"git_url",
|
|
|
|
"web_url",
|
|
|
|
"id",
|
2014-07-23 17:09:05 +02:00
|
|
|
"config_vars",
|
2014-07-23 06:03:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_heroku_app_update_state(
|
|
|
|
s *terraform.ResourceState,
|
2014-07-23 18:09:27 +02:00
|
|
|
app *application) (*terraform.ResourceState, error) {
|
|
|
|
|
|
|
|
s.Attributes["name"] = app.App.Name
|
|
|
|
s.Attributes["stack"] = app.App.Stack.Name
|
|
|
|
s.Attributes["region"] = app.App.Region.Name
|
|
|
|
s.Attributes["git_url"] = app.App.GitURL
|
|
|
|
s.Attributes["web_url"] = app.App.WebURL
|
2014-07-23 06:03:30 +02:00
|
|
|
|
2014-07-23 21:50:17 +02:00
|
|
|
// We know that the hostname on heroku will be the name+herokuapp.com
|
|
|
|
// You need this to do things like create DNS CNAME records
|
|
|
|
s.Attributes["heroku_hostname"] = fmt.Sprintf("%s.herokuapp.com", app.App.Name)
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
toFlatten := make(map[string]interface{})
|
|
|
|
|
|
|
|
if len(app.Vars) > 0 {
|
2014-07-23 19:56:47 +02:00
|
|
|
toFlatten["config_vars"] = []map[string]string{app.Vars}
|
2014-07-23 18:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range flatmap.Flatten(toFlatten) {
|
|
|
|
s.Attributes[k] = v
|
|
|
|
}
|
2014-07-23 06:03:30 +02:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
func resource_heroku_app_retrieve(id string, client *heroku.Client) (*application, error) {
|
|
|
|
app := application{Id: id, Client: client}
|
|
|
|
|
|
|
|
err := app.Update()
|
2014-07-23 06:03:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving app: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
return &app, nil
|
2014-07-23 06:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_heroku_app_validation() *config.Validator {
|
|
|
|
return &config.Validator{
|
|
|
|
Required: []string{},
|
|
|
|
Optional: []string{
|
|
|
|
"name",
|
|
|
|
"region",
|
|
|
|
"stack",
|
2014-07-23 17:09:05 +02:00
|
|
|
"config_vars.*",
|
2014-07-23 06:03:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 17:26:31 +02:00
|
|
|
|
2014-07-23 18:09:27 +02:00
|
|
|
func retrieve_config_vars(id string, client *heroku.Client) (map[string]string, error) {
|
|
|
|
vars, err := client.ConfigVarInfo(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return vars, nil
|
|
|
|
}
|
|
|
|
|
2014-07-23 17:26:31 +02:00
|
|
|
// Updates the config vars for from an expanded (prior to assertion)
|
|
|
|
// []map[string]string config
|
|
|
|
func update_config_vars(id string, vs []interface{}, client *heroku.Client) error {
|
|
|
|
vars := make(map[string]*string)
|
|
|
|
|
|
|
|
for k, v := range vs[0].(map[string]interface{}) {
|
|
|
|
val := v.(string)
|
|
|
|
vars[k] = &val
|
|
|
|
}
|
|
|
|
|
2014-07-23 20:46:12 +02:00
|
|
|
log.Printf("[INFO] Updating config vars: *%#v", vars)
|
|
|
|
|
2014-07-23 17:26:31 +02:00
|
|
|
_, err := client.ConfigVarUpdate(id, vars)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating config vars: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|