2017-04-25 02:08:13 +02:00
|
|
|
package heroku
|
|
|
|
|
2017-04-25 02:34:34 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2017-04-28 15:40:18 +02:00
|
|
|
"fmt"
|
2017-04-25 02:34:34 +02:00
|
|
|
"log"
|
2017-04-28 15:40:18 +02:00
|
|
|
"time"
|
2017-04-25 02:34:34 +02:00
|
|
|
|
|
|
|
heroku "github.com/cyberdelia/heroku-go/v3"
|
2017-04-28 15:40:18 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2017-04-25 02:34:34 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
2017-04-25 02:08:13 +02:00
|
|
|
|
|
|
|
func resourceHerokuSpace() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
2017-04-25 02:34:34 +02:00
|
|
|
Create: resourceHerokuSpaceCreate,
|
|
|
|
Read: resourceHerokuSpaceRead,
|
|
|
|
Update: resourceHerokuSpaceUpdate,
|
|
|
|
Delete: resourceHerokuSpaceDelete,
|
2017-04-25 02:08:13 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 02:34:34 +02:00
|
|
|
|
|
|
|
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())
|
|
|
|
|
2017-04-28 15:40:18 +02:00
|
|
|
// Wait for the Space to be allocated
|
|
|
|
log.Printf("[DEBUG] Waiting for Space (%s) to be allocated", d.Id())
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"allocating"},
|
|
|
|
Target: []string{"allocated"},
|
|
|
|
Refresh: SpaceStateRefreshFunc(client, d.Id()),
|
|
|
|
Timeout: 20 * time.Minute,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for Space (%s) to become available: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceHerokuSpaceRead(d, meta)
|
2017-04-25 02:34:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceHerokuSpaceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*heroku.Service)
|
|
|
|
|
2017-04-28 15:40:18 +02:00
|
|
|
spaceRaw, _, err := SpaceStateRefreshFunc(client, d.Id())()
|
2017-04-25 02:34:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-28 15:40:18 +02:00
|
|
|
space := spaceRaw.(*heroku.Space)
|
2017-04-25 02:34:34 +02:00
|
|
|
|
2017-04-28 15:40:18 +02:00
|
|
|
setSpaceAttributes(d, space)
|
2017-04-25 02:34:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:32:27 +02:00
|
|
|
// The type conversion here can be dropped when the vendored version of
|
|
|
|
// heroku-go is updated.
|
2017-04-25 02:34:34 +02:00
|
|
|
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
|
|
|
|
}
|
2017-04-28 15:40:18 +02:00
|
|
|
|
|
|
|
// SpaceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// a Space.
|
|
|
|
func SpaceStateRefreshFunc(client *heroku.Service, id string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
space, err := client.SpaceInfo(context.TODO(), id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type conversion here can be dropped when the vendored version of
|
|
|
|
// heroku-go is updated.
|
|
|
|
return (*heroku.Space)(space), space.State, nil
|
|
|
|
}
|
|
|
|
}
|