terraform/builtin/providers/heroku/resource_heroku_addon.go

170 lines
3.7 KiB
Go
Raw Normal View History

2014-08-19 00:49:54 +02:00
package heroku
import (
"context"
2014-08-19 00:49:54 +02:00
"fmt"
"log"
"strings"
2014-08-31 02:02:11 +02:00
"sync"
2014-08-19 00:49:54 +02:00
"github.com/cyberdelia/heroku-go/v3"
2014-08-19 00:49:54 +02:00
"github.com/hashicorp/terraform/helper/schema"
)
// Global lock to prevent parallelism for heroku_addon since
// the Heroku API cannot handle a single application requesting
// multiple addons simultaneously.
var addonLock sync.Mutex
func resourceHerokuAddon() *schema.Resource {
return &schema.Resource{
Create: resourceHerokuAddonCreate,
Read: resourceHerokuAddonRead,
Update: resourceHerokuAddonUpdate,
Delete: resourceHerokuAddonDelete,
Schema: map[string]*schema.Schema{
"app": {
2014-08-19 00:49:54 +02:00
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"plan": {
2014-08-19 00:49:54 +02:00
Type: schema.TypeString,
Required: true,
},
"config": {
2014-08-19 00:49:54 +02:00
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeMap,
},
},
"provider_id": {
2014-08-19 00:49:54 +02:00
Type: schema.TypeString,
Computed: true,
},
"config_vars": {
2014-08-19 00:49:54 +02:00
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
2014-08-19 00:49:54 +02:00
},
},
}
}
func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
addonLock.Lock()
defer addonLock.Unlock()
client := meta.(*heroku.Service)
2014-08-19 00:49:54 +02:00
app := d.Get("app").(string)
opts := heroku.AddOnCreateOpts{Plan: d.Get("plan").(string)}
2014-08-19 00:49:54 +02:00
if v := d.Get("config"); v != nil {
config := make(map[string]string)
for _, v := range v.([]interface{}) {
for k, v := range v.(map[string]interface{}) {
config[k] = v.(string)
}
}
opts.Config = &config
}
log.Printf("[DEBUG] Addon create configuration: %#v, %#v", app, opts)
a, err := client.AddOnCreate(context.TODO(), app, opts)
2014-08-19 00:49:54 +02:00
if err != nil {
return err
}
d.SetId(a.ID)
2014-08-19 00:49:54 +02:00
log.Printf("[INFO] Addon ID: %s", d.Id())
return resourceHerokuAddonRead(d, meta)
}
func resourceHerokuAddonRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
2014-08-19 00:49:54 +02:00
addon, err := resourceHerokuAddonRetrieve(
2014-08-19 00:49:54 +02:00
d.Get("app").(string), d.Id(), client)
if err != nil {
return err
}
// Determine the plan. If we were configured without a specific plan,
// then just avoid the plan altogether (accepting anything that
// Heroku sends down).
plan := addon.Plan.Name
if v := d.Get("plan").(string); v != "" {
if idx := strings.IndexRune(v, ':'); idx == -1 {
idx = strings.IndexRune(plan, ':')
if idx > -1 {
plan = plan[:idx]
}
}
}
2014-08-19 00:49:54 +02:00
d.Set("name", addon.Name)
d.Set("plan", plan)
d.Set("provider_id", addon.ProviderID)
if err := d.Set("config_vars", addon.ConfigVars); err != nil {
return err
}
2014-08-19 00:49:54 +02:00
return nil
}
func resourceHerokuAddonUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
2014-08-19 00:49:54 +02:00
app := d.Get("app").(string)
if d.HasChange("plan") {
ad, err := client.AddOnUpdate(
context.TODO(), app, d.Id(), heroku.AddOnUpdateOpts{Plan: d.Get("plan").(string)})
2014-08-19 00:49:54 +02:00
if err != nil {
return err
}
// Store the new ID
d.SetId(ad.ID)
2014-08-19 00:49:54 +02:00
}
return resourceHerokuAddonRead(d, meta)
}
func resourceHerokuAddonDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
2014-08-19 00:49:54 +02:00
log.Printf("[INFO] Deleting Addon: %s", d.Id())
// Destroy the app
_, err := client.AddOnDelete(context.TODO(), d.Get("app").(string), d.Id())
2014-08-19 00:49:54 +02:00
if err != nil {
return fmt.Errorf("Error deleting addon: %s", err)
}
d.SetId("")
return nil
}
func resourceHerokuAddonRetrieve(app string, id string, client *heroku.Service) (*heroku.AddOnInfoResult, error) {
addon, err := client.AddOnInfo(context.TODO(), app, id)
2014-08-19 00:49:54 +02:00
if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
}
return addon, nil
}