diff --git a/builtin/providers/heroku/provider.go b/builtin/providers/heroku/provider.go index b6ea2bc98..6a8c9b986 100644 --- a/builtin/providers/heroku/provider.go +++ b/builtin/providers/heroku/provider.go @@ -25,11 +25,12 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ - "heroku_app": resourceHerokuApp(), "heroku_addon": resourceHerokuAddon(), + "heroku_app": resourceHerokuApp(), + "heroku_cert": resourceHerokuCert(), "heroku_domain": resourceHerokuDomain(), "heroku_drain": resourceHerokuDrain(), - "heroku_cert": resourceHerokuCert(), + "heroku_space": resourceHerokuSpace(), }, ConfigureFunc: providerConfigure, diff --git a/builtin/providers/heroku/resource_heroku_space.go b/builtin/providers/heroku/resource_heroku_space.go new file mode 100644 index 000000000..d11a01194 --- /dev/null +++ b/builtin/providers/heroku/resource_heroku_space.go @@ -0,0 +1,31 @@ +package heroku + +import "github.com/hashicorp/terraform/helper/schema" + +func resourceHerokuSpace() *schema.Resource { + return &schema.Resource{ + Create: func(_ *schema.ResourceData, _ interface{}) error { return nil }, + Read: func(_ *schema.ResourceData, _ interface{}) error { return nil }, + Update: func(_ *schema.ResourceData, _ interface{}) error { return nil }, + Delete: func(_ *schema.ResourceData, _ interface{}) error { return nil }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "organization": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "region": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, + } +} diff --git a/builtin/providers/heroku/resource_heroku_space_test.go b/builtin/providers/heroku/resource_heroku_space_test.go new file mode 100644 index 000000000..d9112f321 --- /dev/null +++ b/builtin/providers/heroku/resource_heroku_space_test.go @@ -0,0 +1,95 @@ +package heroku + +import ( + "context" + "fmt" + "os" + "testing" + + heroku "github.com/cyberdelia/heroku-go/v3" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccHerokuSpace_Basic(t *testing.T) { + var space heroku.SpaceInfoResult + spaceName := 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: testAccCheckHerokuSpaceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckHerokuSpaceConfig_basic(spaceName, org), + Check: resource.ComposeTestCheckFunc( + testAccCheckHerokuSpaceExists("heroku_space.foobar", &space), + ), + }, + }, + }) +} + +func testAccCheckHerokuSpaceConfig_basic(spaceName, orgName string) string { + return fmt.Sprintf(` +resource "heroku_space" "foobar" { + name = "%s" + organization = "%s" + region = "virginia" +} +`, spaceName, orgName) +} + +func testAccCheckHerokuSpaceExists(n string, space *heroku.SpaceInfoResult) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No space name set") + } + + client := testAccProvider.Meta().(*heroku.Service) + + foundSpace, err := client.SpaceInfo(context.TODO(), rs.Primary.ID) + if err != nil { + return err + } + + if foundSpace.Name != rs.Primary.ID { + return fmt.Errorf("Space not found") + } + + *space = *foundSpace + + return nil + } +} + +func testAccCheckHerokuSpaceDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*heroku.Service) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "heroku_space" { + continue + } + + _, err := client.SpaceInfo(context.TODO(), rs.Primary.ID) + + if err == nil { + return fmt.Errorf("Space still exists") + } + } + + return nil +}