diff --git a/builtin/providers/heroku/import_heroku_app_test.go b/builtin/providers/heroku/import_heroku_app_test.go new file mode 100644 index 000000000..7ede51c67 --- /dev/null +++ b/builtin/providers/heroku/import_heroku_app_test.go @@ -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"}, + }, + }, + }) +} diff --git a/builtin/providers/heroku/resource_heroku_app.go b/builtin/providers/heroku/resource_heroku_app.go index 820296325..575f8f08b 100644 --- a/builtin/providers/heroku/resource_heroku_app.go +++ b/builtin/providers/heroku/resource_heroku_app.go @@ -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)