From 5b085f4c44d7998eeb55004add966ecc73b54738 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Fri, 5 May 2017 12:26:04 -0700 Subject: [PATCH] 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. --- .../heroku/import_heroku_app_test.go | 58 +++++++++++++++++++ .../providers/heroku/resource_heroku_app.go | 22 +++++++ 2 files changed, 80 insertions(+) create mode 100644 builtin/providers/heroku/import_heroku_app_test.go 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)