github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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.TypeList, 30 Elem: &schema.Schema{Type: schema.TypeString}, 31 Computed: true, 32 }, 33 }, 34 } 35 } 36 37 func resourceProfitBricksIPBlockCreate(d *schema.ResourceData, meta interface{}) error { 38 ipblock := profitbricks.IpBlock{ 39 Properties: profitbricks.IpBlockProperties{ 40 Size: d.Get("size").(int), 41 Location: d.Get("location").(string), 42 }, 43 } 44 45 ipblock = profitbricks.ReserveIpBlock(ipblock) 46 47 if ipblock.StatusCode > 299 { 48 return fmt.Errorf("An error occured while reserving an ip block: %s", ipblock.Response) 49 } 50 err := waitTillProvisioned(meta, ipblock.Headers.Get("Location")) 51 if err != nil { 52 return err 53 } 54 d.SetId(ipblock.Id) 55 56 return resourceProfitBricksIPBlockRead(d, meta) 57 } 58 59 func resourceProfitBricksIPBlockRead(d *schema.ResourceData, meta interface{}) error { 60 ipblock := profitbricks.GetIpBlock(d.Id()) 61 62 if ipblock.StatusCode > 299 { 63 if ipblock.StatusCode == 404 { 64 d.SetId("") 65 return nil 66 } 67 return fmt.Errorf("An error occured while fetching an ip block ID %s %s", d.Id(), ipblock.Response) 68 } 69 70 log.Printf("[INFO] IPS: %s", strings.Join(ipblock.Properties.Ips, ",")) 71 72 d.Set("ips", ipblock.Properties.Ips) 73 d.Set("location", ipblock.Properties.Location) 74 d.Set("size", ipblock.Properties.Size) 75 76 return nil 77 } 78 79 func resourceProfitBricksIPBlockDelete(d *schema.ResourceData, meta interface{}) error { 80 resp := profitbricks.ReleaseIpBlock(d.Id()) 81 if resp.StatusCode > 299 { 82 return fmt.Errorf("An error occured while releasing an ipblock ID: %s %s", d.Id(), string(resp.Body)) 83 } 84 85 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 86 if err != nil { 87 return err 88 } 89 d.SetId("") 90 return nil 91 }