providers/heroku: addons

This commit is contained in:
Jack Pearkes 2014-07-23 15:33:28 -04:00
parent ae1fc27cc0
commit 89985a3dc6
3 changed files with 300 additions and 0 deletions

View File

@ -1 +1,186 @@
package heroku
import (
"fmt"
"log"
"github.com/bgentry/heroku-go"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform"
)
func resource_heroku_addon_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)
app := rs.Attributes["app"]
plan := rs.Attributes["plan"]
opts := heroku.AddonCreateOpts{}
if attr, ok := rs.Attributes["config.#"]; ok && attr == "1" {
vs := flatmap.Expand(
rs.Attributes, "config").([]interface{})
config := make(map[string]string)
for k, v := range vs[0].(map[string]interface{}) {
config[k] = v.(string)
}
opts.Config = &config
}
log.Printf("[DEBUG] Addon create configuration: %#v, %#v, %#v", app, plan, opts)
a, err := client.AddonCreate(app, plan, &opts)
if err != nil {
return s, err
}
rs.ID = a.Id
log.Printf("[INFO] Addon ID: %s", rs.ID)
addon, err := resource_heroku_addon_retrieve(app, rs.ID, client)
if err != nil {
return rs, err
}
return resource_heroku_addon_update_state(rs, addon)
}
func resource_heroku_addon_update(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client
rs := s.MergeDiff(d)
app := rs.Attributes["app"]
if attr, ok := d.Attributes["plan"]; ok {
ad, err := client.AddonUpdate(
app, rs.ID,
attr.New)
if err != nil {
return s, err
}
// Store the new ID
rs.ID = ad.Id
}
addon, err := resource_heroku_addon_retrieve(app, rs.ID, client)
if err != nil {
return rs, err
}
return resource_heroku_addon_update_state(rs, addon)
}
func resource_heroku_addon_destroy(
s *terraform.ResourceState,
meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
log.Printf("[INFO] Deleting Addon: %s", s.ID)
// Destroy the app
err := client.AddonDelete(s.Attributes["app"], s.ID)
if err != nil {
return fmt.Errorf("Error deleting addon: %s", err)
}
return nil
}
func resource_heroku_addon_refresh(
s *terraform.ResourceState,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client
app, err := resource_heroku_addon_retrieve(s.Attributes["app"], s.ID, client)
if err != nil {
return nil, err
}
return resource_heroku_addon_update_state(s, app)
}
func resource_heroku_addon_diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"app": diff.AttrTypeCreate,
"plan": diff.AttrTypeUpdate,
"config": diff.AttrTypeCreate,
},
ComputedAttrs: []string{
"provider_id",
"config_vars",
},
}
return b.Diff(s, c)
}
func resource_heroku_addon_update_state(
s *terraform.ResourceState,
addon *heroku.Addon) (*terraform.ResourceState, error) {
s.Attributes["name"] = addon.Name
s.Attributes["plan"] = addon.Plan.Name
s.Attributes["provider_id"] = addon.ProviderId
toFlatten := make(map[string]interface{})
if len(addon.ConfigVars) > 0 {
toFlatten["config_vars"] = addon.ConfigVars
}
for k, v := range flatmap.Flatten(toFlatten) {
s.Attributes[k] = v
}
return s, nil
}
func resource_heroku_addon_retrieve(app string, id string, client *heroku.Client) (*heroku.Addon, error) {
addon, err := client.AddonInfo(app, id)
if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
}
return addon, nil
}
func resource_heroku_addon_validation() *config.Validator {
return &config.Validator{
Required: []string{
"app",
"plan",
},
Optional: []string{
"config.*",
},
}
}

View File

@ -1 +1,107 @@
package heroku
import (
"fmt"
"testing"
"github.com/bgentry/heroku-go"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccHerokuAddon_Basic(t *testing.T) {
var addon heroku.Addon
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckHerokuAddonConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "config.0.url", "http://google.com"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "app", "terraform-test-app"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "plan", "deployhooks:http"),
),
},
},
})
}
func testAccCheckHerokuAddonDestroy(s *terraform.State) error {
client := testAccProvider.client
for _, rs := range s.Resources {
if rs.Type != "heroku_app" {
continue
}
_, err := client.AddonInfo(rs.Attributes["app"], rs.ID)
if err == nil {
return fmt.Errorf("Addon still exists")
}
}
return nil
}
func testAccCheckHerokuAddonAttributes(addon *heroku.Addon) resource.TestCheckFunc {
return func(s *terraform.State) error {
if addon.Plan.Name != "deployhooks:http" {
return fmt.Errorf("Bad plan: %s", addon.Plan)
}
return nil
}
}
func testAccCheckHerokuAddonExists(n string, addon *heroku.Addon) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.ID == "" {
return fmt.Errorf("No Addon ID is set")
}
client := testAccProvider.client
foundAddon, err := client.AddonInfo(rs.Attributes["app"], rs.ID)
if err != nil {
return err
}
if foundAddon.Id != rs.ID {
return fmt.Errorf("Addon not found")
}
*addon = *foundAddon
return nil
}
}
const testAccCheckHerokuAddonConfig_basic = `
resource "heroku_app" "foobar" {
name = "terraform-test-app"
}
resource "heroku_addon" "foobar" {
app = "${heroku_app.foobar.name}"
plan = "deployhooks:http"
config {
url = "http://google.com"
}
}`

View File

@ -11,6 +11,15 @@ var resourceMap *resource.Map
func init() {
resourceMap = &resource.Map{
Mapping: map[string]resource.Resource{
"heroku_addon": resource.Resource{
ConfigValidator: resource_heroku_addon_validation(),
Create: resource_heroku_addon_create,
Destroy: resource_heroku_addon_destroy,
Diff: resource_heroku_addon_diff,
Refresh: resource_heroku_addon_refresh,
Update: resource_heroku_addon_update,
},
"heroku_app": resource.Resource{
ConfigValidator: resource_heroku_app_validation(),
Create: resource_heroku_app_create,