github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/profitbricks/resource_profitbricks_loadbalancer.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 ) 8 9 func resourceProfitBricksLoadbalancer() *schema.Resource { 10 return &schema.Resource{ 11 Create: resourceProfitBricksLoadbalancerCreate, 12 Read: resourceProfitBricksLoadbalancerRead, 13 Update: resourceProfitBricksLoadbalancerUpdate, 14 Delete: resourceProfitBricksLoadbalancerDelete, 15 Schema: map[string]*schema.Schema{ 16 17 "name": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 }, 21 22 "ip": &schema.Schema{ 23 Type: schema.TypeString, 24 Optional: true, 25 }, 26 "dhcp": &schema.Schema{ 27 Type: schema.TypeBool, 28 Optional: true, 29 }, 30 "datacenter_id": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 }, 34 "nic_id": &schema.Schema{ 35 Type: schema.TypeString, 36 Required: true, 37 }, 38 }, 39 } 40 } 41 42 func resourceProfitBricksLoadbalancerCreate(d *schema.ResourceData, meta interface{}) error { 43 lb := profitbricks.Loadbalancer{ 44 Properties: profitbricks.LoadbalancerProperties{ 45 Name: d.Get("name").(string), 46 }, 47 } 48 49 lb = profitbricks.CreateLoadbalancer(d.Get("datacenter_id").(string), lb) 50 51 if lb.StatusCode > 299 { 52 return fmt.Errorf("Error occured while creating a loadbalancer %s", lb.Response) 53 } 54 err := waitTillProvisioned(meta, lb.Headers.Get("Location")) 55 56 if err != nil { 57 return err 58 } 59 60 d.SetId(lb.Id) 61 62 nic := profitbricks.AssociateNic(d.Get("datacenter_id").(string), d.Id(), d.Get("nic_id").(string)) 63 64 if nic.StatusCode > 299 { 65 return fmt.Errorf("Error occured while deleting a balanced nic: %s", nic.Response) 66 } 67 err = waitTillProvisioned(meta, nic.Headers.Get("Location")) 68 if err != nil { 69 return err 70 } 71 72 return resourceProfitBricksLoadbalancerRead(d, meta) 73 } 74 75 func resourceProfitBricksLoadbalancerRead(d *schema.ResourceData, meta interface{}) error { 76 lb := profitbricks.GetLoadbalancer(d.Get("datacenter_id").(string), d.Id()) 77 78 if lb.StatusCode > 299 { 79 if lb.StatusCode == 404 { 80 d.SetId("") 81 return nil 82 } 83 return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), lb.Response) 84 } 85 86 d.Set("name", lb.Properties.Name) 87 d.Set("ip", lb.Properties.Ip) 88 d.Set("dhcp", lb.Properties.Dhcp) 89 90 return nil 91 } 92 93 func resourceProfitBricksLoadbalancerUpdate(d *schema.ResourceData, meta interface{}) error { 94 properties := profitbricks.LoadbalancerProperties{} 95 if d.HasChange("name") { 96 _, new := d.GetChange("name") 97 properties.Name = new.(string) 98 } 99 if d.HasChange("ip") { 100 _, new := d.GetChange("ip") 101 properties.Ip = new.(string) 102 } 103 if d.HasChange("dhcp") { 104 _, new := d.GetChange("dhcp") 105 properties.Dhcp = new.(bool) 106 } 107 108 if d.HasChange("nic_id") { 109 old, new := d.GetChange("dhcp") 110 111 resp := profitbricks.DeleteBalancedNic(d.Get("datacenter_id").(string), d.Id(), old.(string)) 112 if resp.StatusCode > 299 { 113 return fmt.Errorf("Error occured while deleting a balanced nic: %s", string(resp.Body)) 114 } 115 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 116 if err != nil { 117 return err 118 } 119 120 nic := profitbricks.AssociateNic(d.Get("datacenter_id").(string), d.Id(), new.(string)) 121 if nic.StatusCode > 299 { 122 return fmt.Errorf("Error occured while deleting a balanced nic: %s", nic.Response) 123 } 124 err = waitTillProvisioned(meta, nic.Headers.Get("Location")) 125 if err != nil { 126 return err 127 } 128 } 129 130 return resourceProfitBricksLoadbalancerRead(d, meta) 131 } 132 133 func resourceProfitBricksLoadbalancerDelete(d *schema.ResourceData, meta interface{}) error { 134 resp := profitbricks.DeleteLoadbalancer(d.Get("datacenter_id").(string), d.Id()) 135 136 if resp.StatusCode > 299 { 137 return fmt.Errorf("Error occured while deleting a loadbalancer: %s", string(resp.Body)) 138 } 139 140 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 141 if err != nil { 142 return err 143 } 144 145 d.SetId("") 146 return nil 147 }