github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/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 if ipblock.StatusCode == 404 { 63 d.SetId("") 64 return nil 65 } 66 return fmt.Errorf("An error occured while fetching an ip block ID %s %s", d.Id(), ipblock.Response) 67 } 68 69 log.Printf("[INFO] IPS: %s", strings.Join(ipblock.Properties.Ips, ",")) 70 71 d.Set("ips", strings.Join(ipblock.Properties.Ips, ",")) 72 d.Set("location", ipblock.Properties.Location) 73 d.Set("size", ipblock.Properties.Size) 74 75 return nil 76 } 77 78 func resourceProfitBricksIPBlockDelete(d *schema.ResourceData, meta interface{}) error { 79 resp := profitbricks.ReleaseIpBlock(d.Id()) 80 if resp.StatusCode > 299 { 81 return fmt.Errorf("An error occured while releasing an ipblock ID: %s %s", d.Id(), string(resp.Body)) 82 } 83 84 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 85 if err != nil { 86 return err 87 } 88 d.SetId("") 89 return nil 90 }