github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/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 config := meta.(*Config) 44 profitbricks.SetAuth(config.Username, config.Password) 45 46 lb := profitbricks.Loadbalancer{ 47 Properties: profitbricks.LoadbalancerProperties{ 48 Name: d.Get("name").(string), 49 }, 50 } 51 52 lb = profitbricks.CreateLoadbalancer(d.Get("datacenter_id").(string), lb) 53 54 if lb.StatusCode > 299 { 55 return fmt.Errorf("Error occured while creating a loadbalancer %s", lb.Response) 56 } 57 err := waitTillProvisioned(meta, lb.Headers.Get("Location")) 58 59 if err != nil { 60 return err 61 } 62 63 d.SetId(lb.Id) 64 65 nic := profitbricks.AssociateNic(d.Get("datacenter_id").(string), d.Id(), d.Get("nic_id").(string)) 66 67 if nic.StatusCode > 299 { 68 return fmt.Errorf("Error occured while deleting a balanced nic: %s", nic.Response) 69 } 70 err = waitTillProvisioned(meta, nic.Headers.Get("Location")) 71 if err != nil { 72 return err 73 } 74 75 return resourceProfitBricksLoadbalancerRead(d, meta) 76 } 77 78 func resourceProfitBricksLoadbalancerRead(d *schema.ResourceData, meta interface{}) error { 79 lb := profitbricks.GetLoadbalancer(d.Get("datacenter_id").(string), d.Id()) 80 81 d.Set("name", lb.Properties.Name) 82 d.Set("ip", lb.Properties.Ip) 83 d.Set("dhcp", lb.Properties.Dhcp) 84 85 return nil 86 } 87 88 func resourceProfitBricksLoadbalancerUpdate(d *schema.ResourceData, meta interface{}) error { 89 config := meta.(*Config) 90 profitbricks.SetAuth(config.Username, config.Password) 91 92 properties := profitbricks.LoadbalancerProperties{} 93 if d.HasChange("name") { 94 _, new := d.GetChange("name") 95 properties.Name = new.(string) 96 } 97 if d.HasChange("ip") { 98 _, new := d.GetChange("ip") 99 properties.Ip = new.(string) 100 } 101 if d.HasChange("dhcp") { 102 _, new := d.GetChange("dhcp") 103 properties.Dhcp = new.(bool) 104 } 105 106 if d.HasChange("nic_id") { 107 old, new := d.GetChange("dhcp") 108 109 resp := profitbricks.DeleteBalancedNic(d.Get("datacenter_id").(string), d.Id(), old.(string)) 110 if resp.StatusCode > 299 { 111 return fmt.Errorf("Error occured while deleting a balanced nic: %s", string(resp.Body)) 112 } 113 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 114 if err != nil { 115 return err 116 } 117 118 nic := profitbricks.AssociateNic(d.Get("datacenter_id").(string), d.Id(), new.(string)) 119 if nic.StatusCode > 299 { 120 return fmt.Errorf("Error occured while deleting a balanced nic: %s", nic.Response) 121 } 122 err = waitTillProvisioned(meta, nic.Headers.Get("Location")) 123 if err != nil { 124 return err 125 } 126 } 127 128 return resourceProfitBricksLoadbalancerRead(d, meta) 129 } 130 131 func resourceProfitBricksLoadbalancerDelete(d *schema.ResourceData, meta interface{}) error { 132 config := meta.(*Config) 133 profitbricks.SetAuth(config.Username, config.Password) 134 135 resp := profitbricks.DeleteLoadbalancer(d.Get("datacenter_id").(string), d.Id()) 136 137 if resp.StatusCode > 299 { 138 return fmt.Errorf("Error occured while deleting a loadbalancer: %s", string(resp.Body)) 139 } 140 141 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 142 if err != nil { 143 return err 144 } 145 146 d.SetId("") 147 return nil 148 }