github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_space.go (about) 1 package heroku 2 3 import ( 4 "context" 5 "log" 6 7 heroku "github.com/cyberdelia/heroku-go/v3" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceHerokuSpace() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceHerokuSpaceCreate, 14 Read: resourceHerokuSpaceRead, 15 Update: resourceHerokuSpaceUpdate, 16 Delete: resourceHerokuSpaceDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": { 20 Type: schema.TypeString, 21 Required: true, 22 }, 23 24 "organization": { 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 30 "region": { 31 Type: schema.TypeString, 32 Optional: true, 33 ForceNew: true, 34 }, 35 }, 36 } 37 } 38 39 func resourceHerokuSpaceCreate(d *schema.ResourceData, meta interface{}) error { 40 client := meta.(*heroku.Service) 41 42 opts := heroku.SpaceCreateOpts{} 43 opts.Name = d.Get("name").(string) 44 opts.Organization = d.Get("organization").(string) 45 46 if v, ok := d.GetOk("region"); ok { 47 vs := v.(string) 48 opts.Region = &vs 49 } 50 51 space, err := client.SpaceCreate(context.TODO(), opts) 52 if err != nil { 53 return err 54 } 55 56 d.SetId(space.ID) 57 log.Printf("[INFO] Space ID: %s", d.Id()) 58 59 // The type conversion here can be dropped when the vendored version of 60 // heroku-go is updated. 61 setSpaceAttributes(d, (*heroku.Space)(space)) 62 return nil 63 } 64 65 func resourceHerokuSpaceRead(d *schema.ResourceData, meta interface{}) error { 66 client := meta.(*heroku.Service) 67 68 space, err := client.SpaceInfo(context.TODO(), d.Id()) 69 if err != nil { 70 return err 71 } 72 73 // The type conversion here can be dropped when the vendored version of 74 // heroku-go is updated. 75 setSpaceAttributes(d, (*heroku.Space)(space)) 76 return nil 77 } 78 79 func resourceHerokuSpaceUpdate(d *schema.ResourceData, meta interface{}) error { 80 client := meta.(*heroku.Service) 81 82 if !d.HasChange("name") { 83 return nil 84 } 85 86 name := d.Get("name").(string) 87 opts := heroku.SpaceUpdateOpts{Name: &name} 88 89 space, err := client.SpaceUpdate(context.TODO(), d.Id(), opts) 90 if err != nil { 91 return err 92 } 93 94 // The type conversion here can be dropped when the vendored version of 95 // heroku-go is updated. 96 setSpaceAttributes(d, (*heroku.Space)(space)) 97 return nil 98 } 99 100 func setSpaceAttributes(d *schema.ResourceData, space *heroku.Space) { 101 d.Set("name", space.Name) 102 d.Set("organization", space.Organization.Name) 103 d.Set("region", space.Region.Name) 104 } 105 106 func resourceHerokuSpaceDelete(d *schema.ResourceData, meta interface{}) error { 107 client := meta.(*heroku.Service) 108 109 log.Printf("[INFO] Deleting space: %s", d.Id()) 110 _, err := client.SpaceDelete(context.TODO(), d.Id()) 111 if err != nil { 112 return err 113 } 114 115 d.SetId("") 116 return nil 117 }