From 298b8090f02c889b34c1beb40e0d604c40f900cf Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Wed, 23 Jul 2014 11:09:05 -0400 Subject: [PATCH] providers/heroku: support config vars on create --- .../providers/heroku/resource_heroku_app.go | 34 ++++++++++++++++--- .../heroku/resource_heroku_app_test.go | 30 ++++++++++++++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/builtin/providers/heroku/resource_heroku_app.go b/builtin/providers/heroku/resource_heroku_app.go index c1f6b728c..258701746 100644 --- a/builtin/providers/heroku/resource_heroku_app.go +++ b/builtin/providers/heroku/resource_heroku_app.go @@ -4,10 +4,11 @@ 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" - "github.com/bgentry/heroku-go" ) func resource_heroku_app_create( @@ -47,6 +48,28 @@ func resource_heroku_app_create( log.Printf("[INFO] App ID: %s", rs.ID) + if attr, ok := rs.Attributes["config_vars.#"]; ok && attr == "1" { + vs := flatmap.Expand( + rs.Attributes, "config_vars").([]interface{}) + vars := make(map[string]*string) + + for k, v := range vs[0].(map[string]interface{}) { + val := v.(string) + vars[k] = &val + } + + _, err = client.ConfigVarUpdate(rs.ID, vars) + + if err != nil { + return rs, err + } + } + + app, err = resource_heroku_app_retrieve(rs.ID, client) + if err != nil { + return rs, err + } + return resource_heroku_app_update_state(rs, app) } @@ -99,9 +122,10 @@ func resource_heroku_app_diff( b := &diff.ResourceBuilder{ Attrs: map[string]diff.AttrType{ - "name": diff.AttrTypeCreate, - "region": diff.AttrTypeUpdate, - "stack": diff.AttrTypeCreate, + "name": diff.AttrTypeCreate, + "region": diff.AttrTypeUpdate, + "stack": diff.AttrTypeCreate, + "config_vars": diff.AttrTypeUpdate, }, ComputedAttrs: []string{ @@ -111,6 +135,7 @@ func resource_heroku_app_diff( "git_url", "web_url", "id", + "config_vars", }, } @@ -148,6 +173,7 @@ func resource_heroku_app_validation() *config.Validator { "name", "region", "stack", + "config_vars.*", }, } } diff --git a/builtin/providers/heroku/resource_heroku_app_test.go b/builtin/providers/heroku/resource_heroku_app_test.go index f6f196ce7..5238bc345 100644 --- a/builtin/providers/heroku/resource_heroku_app_test.go +++ b/builtin/providers/heroku/resource_heroku_app_test.go @@ -24,6 +24,8 @@ func TestAccHerokuApp_Basic(t *testing.T) { testAccCheckHerokuAppAttributes(&app), resource.TestCheckResourceAttr( "heroku_app.foobar", "name", "terraform-test-app"), + resource.TestCheckResourceAttr( + "heroku_app.foobar", "config_vars.0.foo", "bar"), ), }, }, @@ -50,8 +52,28 @@ func testAccCheckHerokuAppDestroy(s *terraform.State) error { func testAccCheckHerokuAppAttributes(app *heroku.App) resource.TestCheckFunc { return func(s *terraform.State) error { + client := testAccProvider.client - // check attrs + if app.Region.Name != "us" { + return fmt.Errorf("Bad region: %s", app.Region.Name) + } + + if app.Stack.Name != "cedar" { + return fmt.Errorf("Bad stack: %s", app.Stack.Name) + } + + if app.Name != "terraform-test-app" { + return fmt.Errorf("Bad name: %s", app.Name) + } + + vars, err := client.ConfigVarInfo(app.Name) + if err != nil { + return err + } + + if vars["foo"] != "bar" { + return fmt.Errorf("Bad config vars: %v", vars) + } return nil } @@ -81,7 +103,7 @@ func testAccCheckHerokuAppExists(n string, app *heroku.App) resource.TestCheckFu return fmt.Errorf("App not found") } - app = foundApp + *app = *foundApp return nil } @@ -90,4 +112,8 @@ func testAccCheckHerokuAppExists(n string, app *heroku.App) resource.TestCheckFu const testAccCheckHerokuAppConfig_basic = ` resource "heroku_app" "foobar" { name = "terraform-test-app" + + config_vars = { + FOO = bar + } }`