Skip to content

Commit

Permalink
providers/heroku: plans work without specific plan name [hashicorpGH-198
Browse files Browse the repository at this point in the history
]
  • Loading branch information
mitchellh committed Aug 28, 2014
1 parent 1b05406 commit 8ee32f3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
16 changes: 15 additions & 1 deletion resource_heroku_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"sync"
"strings"

"github.com/cyberdelia/heroku-go/v3"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -98,8 +99,21 @@ func resourceHerokuAddonRead(d *schema.ResourceData, meta interface{}) error {
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]
}
}
}

d.Set("name", addon.Name)
d.Set("plan", addon.Plan.Name)
d.Set("plan", plan)
d.Set("provider_id", addon.ProviderID)
d.Set("config_vars", []interface{}{addon.ConfigVars})
d.SetDependencies([]terraform.ResourceDependency{
Expand Down
54 changes: 50 additions & 4 deletions resource_heroku_addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccHerokuAddon_Basic(t *testing.T) {
Config: testAccCheckHerokuAddonConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon),
testAccCheckHerokuAddonAttributes(&addon, "deployhooks:http"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "config.0.url", "http://google.com"),
resource.TestCheckResourceAttr(
Expand All @@ -34,6 +34,41 @@ func TestAccHerokuAddon_Basic(t *testing.T) {
})
}

// GH-198
func TestAccHerokuAddon_noPlan(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_no_plan,
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "app", "terraform-test-app"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "plan", "memcachier"),
),
},
resource.TestStep{
Config: testAccCheckHerokuAddonConfig_no_plan,
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "app", "terraform-test-app"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "plan", "memcachier"),
),
},
},
})
}

func testAccCheckHerokuAddonDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*heroku.Service)

Expand All @@ -52,11 +87,11 @@ func testAccCheckHerokuAddonDestroy(s *terraform.State) error {
return nil
}

func testAccCheckHerokuAddonAttributes(addon *heroku.Addon) resource.TestCheckFunc {
func testAccCheckHerokuAddonAttributes(addon *heroku.Addon, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

if addon.Plan.Name != "deployhooks:http" {
return fmt.Errorf("Bad plan: %s", addon.Plan)
if addon.Plan.Name != n {
return fmt.Errorf("Bad plan: %s", addon.Plan.Name)
}

return nil
Expand Down Expand Up @@ -106,3 +141,14 @@ resource "heroku_addon" "foobar" {
url = "http://google.com"
}
}`

const testAccCheckHerokuAddonConfig_no_plan = `
resource "heroku_app" "foobar" {
name = "terraform-test-app"
region = "us"
}
resource "heroku_addon" "foobar" {
app = "${heroku_app.foobar.name}"
plan = "memcachier"
}`

0 comments on commit 8ee32f3

Please sign in to comment.