github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/profitbricks/resource_profitbricks_ipblock.go (about) 1 package profitbricks 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/terraform/helper/schema" 6 "github.com/profitbricks/profitbricks-sdk-go" 7 "log" 8 "strings" 9 ) 10 11 func resourceProfitBricksIPBlock() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceProfitBricksIPBlockCreate, 14 Read: resourceProfitBricksIPBlockRead, 15 //Update: resourceProfitBricksIPBlockUpdate, 16 Delete: resourceProfitBricksIPBlockDelete, 17 Schema: map[string]*schema.Schema{ 18 "location": { 19 Type: schema.TypeString, 20 Required: true, 21 ForceNew: true, 22 }, 23 "size": { 24 Type: schema.TypeInt, 25 Required: true, 26 ForceNew: true, 27 }, 28 "ips": { 29 Type: schema.TypeString, 30 Computed: true, 31 }, 32 }, 33 } 34 } 35 36 func resourceProfitBricksIPBlockCreate(d *schema.ResourceData, meta interface{}) error { 37 ipblock := profitbricks.IpBlock{ 38 Properties: profitbricks.IpBlockProperties{ 39 Size: d.Get("size").(int), 40 Location: d.Get("location").(string), 41 }, 42 } 43 44 ipblock = profitbricks.ReserveIpBlock(ipblock) 45 46 if ipblock.StatusCode > 299 { 47 return fmt.Errorf("An error occured while reserving an ip block: %s", ipblock.Response) 48 } 49 err := waitTillProvisioned(meta, ipblock.Headers.Get("Location")) 50 if err != nil { 51 return err 52 } 53 d.SetId(ipblock.Id) 54 55 return resourceProfitBricksIPBlockRead(d, meta) 56 } 57 58 func resourceProfitBricksIPBlockRead(d *schema.ResourceData, meta interface{}) error { 59 ipblock := profitbricks.GetIpBlock(d.Id()) 60 61 if ipblock.StatusCode > 299 { 62 return fmt.Errorf("An error occured while fetching an ip block ID %s %s", d.Id(), ipblock.Response) 63 } 64 65 log.Printf("[INFO] IPS: %s", strings.Join(ipblock.Properties.Ips, ",")) 66 67 d.Set("ips", strings.Join(ipblock.Properties.Ips, ",")) 68 d.Set("location", ipblock.Properties.Location) 69 d.Set("size", ipblock.Properties.Size) 70 71 return nil 72 } 73 74 func resourceProfitBricksIPBlockDelete(d *schema.ResourceData, meta interface{}) error { 75 resp := profitbricks.ReleaseIpBlock(d.Id()) 76 if resp.StatusCode > 299 { 77 return fmt.Errorf("An error occured while releasing an ipblock ID: %s %s", d.Id(), string(resp.Body)) 78 } 79 80 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 81 if err != nil { 82 return err 83 } 84 d.SetId("") 85 return nil 86 }