providers/heroku: import heroku_app resource (#14248)

Adds support for importing normal and organization apps with the
heroku_app resource.

The resource itself depends on an `organization` value in the
configuration to switch behavior for the different kinds of apps, so
`schema.ImportStatePassthrough` is not sufficient.
This commit is contained in:
Bernerd Schaefer 2017-05-05 12:26:04 -07:00 committed by Paul Stack
parent 851a1333f5
commit 5b085f4c44
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package heroku
import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccHerokuApp_importBasic(t *testing.T) {
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckHerokuAppConfig_basic(appName),
},
{
ResourceName: "heroku_app.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"config_vars"},
},
},
})
}
func TestAccHerokuApp_importOrganization(t *testing.T) {
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
org := os.Getenv("HEROKU_ORGANIZATION")
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if org == "" {
t.Skip("HEROKU_ORGANIZATION is not set; skipping test.")
}
},
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckHerokuAppConfig_organization(appName, org),
},
{
ResourceName: "heroku_app.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"config_vars"},
},
},
})
}

View File

@ -100,6 +100,10 @@ func resourceHerokuApp() *schema.Resource {
Update: resourceHerokuAppUpdate,
Delete: resourceHerokuAppDelete,
Importer: &schema.ResourceImporter{
State: resourceHerokuAppImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
@ -193,6 +197,24 @@ func isOrganizationApp(d *schema.ResourceData) bool {
return len(v) > 0 && v[0] != nil
}
func resourceHerokuAppImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
client := m.(*heroku.Service)
app, err := client.AppInfo(context.TODO(), d.Id())
if err != nil {
return nil, err
}
// Flag organization apps by setting the organization name
if app.Organization != nil {
d.Set("organization", []map[string]interface{}{
{"name": app.Organization.Name},
})
}
return []*schema.ResourceData{d}, nil
}
func switchHerokuAppCreate(d *schema.ResourceData, meta interface{}) error {
if isOrganizationApp(d) {
return resourceHerokuOrgAppCreate(d, meta)