providers/heroku: support config vars on create

This commit is contained in:
Jack Pearkes 2014-07-23 11:09:05 -04:00
parent 703d115638
commit 298b8090f0
2 changed files with 58 additions and 6 deletions

View File

@ -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.*",
},
}
}

View File

@ -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
}
}`