Skip to content

Commit

Permalink
providers/heroku: import heroku_app resource (hashicorp#14248)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bernerdschaefer authored and stack72 committed May 5, 2017
1 parent 695d014 commit 74c3a6d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
58 changes: 58 additions & 0 deletions import_heroku_app_test.go
Original file line number Diff line number Diff line change
@@ -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"},
},
},
})
}
22 changes: 22 additions & 0 deletions resource_heroku_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 74c3a6d

Please sign in to comment.