terraform/builtin/providers/heroku/resource_heroku_space.go

118 lines
2.6 KiB
Go
Raw Normal View History

package heroku
2017-04-25 02:34:34 +02:00
import (
"context"
"log"
heroku "github.com/cyberdelia/heroku-go/v3"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceHerokuSpace() *schema.Resource {
return &schema.Resource{
2017-04-25 02:34:34 +02:00
Create: resourceHerokuSpaceCreate,
Read: resourceHerokuSpaceRead,
Update: resourceHerokuSpaceUpdate,
Delete: resourceHerokuSpaceDelete,
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-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 resourceHerokuSpaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
space, err := client.SpaceInfo(context.TODO(), d.Id())
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 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
}