Implementation

This commit is contained in:
Bernerd Schaefer 2017-04-24 17:34:34 -07:00
parent 43257492b9
commit dbf82e651e
2 changed files with 105 additions and 6 deletions

View File

@ -1,13 +1,19 @@
package heroku
import "github.com/hashicorp/terraform/helper/schema"
import (
"context"
"log"
heroku "github.com/cyberdelia/heroku-go/v3"
"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 },
Create: resourceHerokuSpaceCreate,
Read: resourceHerokuSpaceRead,
Update: resourceHerokuSpaceUpdate,
Delete: resourceHerokuSpaceDelete,
Schema: map[string]*schema.Schema{
"name": {
@ -29,3 +35,77 @@ func resourceHerokuSpace() *schema.Resource {
},
}
}
func resourceHerokuSpaceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
opts := heroku.SpaceCreateOpts{}
opts.Name = d.Get("name").(string)
opts.Organization = d.Get("organization").(string)
if v, ok := d.GetOk("region"); ok {
vs := v.(string)
opts.Region = &vs
}
space, err := client.SpaceCreate(context.TODO(), opts)
if err != nil {
return err
}
d.SetId(space.ID)
log.Printf("[INFO] Space ID: %s", d.Id())
setSpaceAttributes(d, (*heroku.Space)(space))
return nil
}
func resourceHerokuSpaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
space, err := client.SpaceInfo(context.TODO(), d.Id())
if err != nil {
return err
}
setSpaceAttributes(d, (*heroku.Space)(space))
return nil
}
func resourceHerokuSpaceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
if !d.HasChange("name") {
return nil
}
name := d.Get("name").(string)
opts := heroku.SpaceUpdateOpts{Name: &name}
space, err := client.SpaceUpdate(context.TODO(), d.Id(), opts)
if err != nil {
return err
}
setSpaceAttributes(d, (*heroku.Space)(space))
return nil
}
func setSpaceAttributes(d *schema.ResourceData, space *heroku.Space) {
d.Set("name", space.Name)
d.Set("organization", space.Organization.Name)
d.Set("region", space.Region.Name)
}
func resourceHerokuSpaceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
log.Printf("[INFO] Deleting space: %s", d.Id())
_, err := client.SpaceDelete(context.TODO(), d.Id())
if err != nil {
return err
}
d.SetId("")
return nil
}

View File

@ -15,6 +15,7 @@ import (
func TestAccHerokuSpace_Basic(t *testing.T) {
var space heroku.SpaceInfoResult
spaceName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
spaceName2 := fmt.Sprintf("tftest-%s", acctest.RandString(10))
org := os.Getenv("HEROKU_ORGANIZATION")
resource.Test(t, resource.TestCase{
@ -31,6 +32,14 @@ func TestAccHerokuSpace_Basic(t *testing.T) {
Config: testAccCheckHerokuSpaceConfig_basic(spaceName, org),
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuSpaceExists("heroku_space.foobar", &space),
testAccCheckHerokuSpaceAttributes(&space, spaceName),
),
},
{
Config: testAccCheckHerokuSpaceConfig_basic(spaceName2, org),
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuSpaceExists("heroku_space.foobar", &space),
testAccCheckHerokuSpaceAttributes(&space, spaceName2),
),
},
},
@ -66,7 +75,7 @@ func testAccCheckHerokuSpaceExists(n string, space *heroku.SpaceInfoResult) reso
return err
}
if foundSpace.Name != rs.Primary.ID {
if foundSpace.ID != rs.Primary.ID {
return fmt.Errorf("Space not found")
}
@ -76,6 +85,16 @@ func testAccCheckHerokuSpaceExists(n string, space *heroku.SpaceInfoResult) reso
}
}
func testAccCheckHerokuSpaceAttributes(space *heroku.SpaceInfoResult, spaceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if space.Name != spaceName {
return fmt.Errorf("Bad name: %s", space.Name)
}
return nil
}
}
func testAccCheckHerokuSpaceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*heroku.Service)