github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/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 d.Set("name", lb.Properties.Name) 79 d.Set("ip", lb.Properties.Ip) 80 d.Set("dhcp", lb.Properties.Dhcp) 81 82 return nil 83 } 84 85 func resourceProfitBricksLoadbalancerUpdate(d *schema.ResourceData, meta interface{}) error { 86 properties := profitbricks.LoadbalancerProperties{} 87 if d.HasChange("name") { 88 _, new := d.GetChange("name") 89 properties.Name = new.(string) 90 } 91 if d.HasChange("ip") { 92 _, new := d.GetChange("ip") 93 properties.Ip = new.(string) 94 } 95 if d.HasChange("dhcp") { 96 _, new := d.GetChange("dhcp") 97 properties.Dhcp = new.(bool) 98 } 99 100 if d.HasChange("nic_id") { 101 old, new := d.GetChange("dhcp") 102 103 resp := profitbricks.DeleteBalancedNic(d.Get("datacenter_id").(string), d.Id(), old.(string)) 104 if resp.StatusCode > 299 { 105 return fmt.Errorf("Error occured while deleting a balanced nic: %s", string(resp.Body)) 106 } 107 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 108 if err != nil { 109 return err 110 } 111 112 nic := profitbricks.AssociateNic(d.Get("datacenter_id").(string), d.Id(), new.(string)) 113 if nic.StatusCode > 299 { 114 return fmt.Errorf("Error occured while deleting a balanced nic: %s", nic.Response) 115 } 116 err = waitTillProvisioned(meta, nic.Headers.Get("Location")) 117 if err != nil { 118 return err 119 } 120 } 121 122 return resourceProfitBricksLoadbalancerRead(d, meta) 123 } 124 125 func resourceProfitBricksLoadbalancerDelete(d *schema.ResourceData, meta interface{}) error { 126 resp := profitbricks.DeleteLoadbalancer(d.Get("datacenter_id").(string), d.Id()) 127 128 if resp.StatusCode > 299 { 129 return fmt.Errorf("Error occured while deleting a loadbalancer: %s", string(resp.Body)) 130 } 131 132 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 133 if err != nil { 134 return err 135 } 136 137 d.SetId("") 138 return nil 139 }