2015-01-14 19:55:17 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2015-01-26 23:45:05 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-01-14 19:55:17 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBPoolV1() *schema.Resource {
|
2015-01-14 19:55:17 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceLBPoolV1Create,
|
|
|
|
Read: resourceLBPoolV1Read,
|
|
|
|
Update: resourceLBPoolV1Update,
|
|
|
|
Delete: resourceLBPoolV1Delete,
|
2015-01-14 19:55:17 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-01-26 18:54:07 +01:00
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DefaultFunc: envDefaultFunc("OS_REGION_NAME"),
|
|
|
|
},
|
2015-01-14 19:55:17 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"subnet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"lb_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"monitor_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBPoolV1Create(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:17 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:55:17 +01:00
|
|
|
|
|
|
|
createOpts := pools.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Protocol: d.Get("protocol").(string),
|
|
|
|
SubnetID: d.Get("subnet_id").(string),
|
|
|
|
LBMethod: d.Get("lb_method").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Requesting lb pool creation")
|
2015-01-26 18:54:07 +01:00
|
|
|
p, err := pools.Create(networkingClient, createOpts).Extract()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack LB pool: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] LB Pool ID: %s", p.ID)
|
|
|
|
|
|
|
|
d.SetId(p.ID)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
if mIDs := resourcePoolMonitorIDsV1(d); mIDs != nil {
|
2015-01-14 19:55:17 +01:00
|
|
|
for _, mID := range mIDs {
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err := pools.AssociateMonitor(networkingClient, p.ID, mID).Extract()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error associating monitor (%s) with OpenStack LB pool (%s): %s", mID, p.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceLBPoolV1Read(d, meta)
|
2015-01-14 19:55:17 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBPoolV1Read(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:17 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:55:17 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
p, err := pools.Get(networkingClient, d.Id()).Extract()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving OpenStack LB Pool: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retreived OpenStack LB Pool %s: %+v", d.Id(), p)
|
|
|
|
|
2015-01-26 19:58:19 +01:00
|
|
|
d.Set("region", d.Get("region").(string))
|
2015-01-14 19:55:17 +01:00
|
|
|
d.Set("name", p.Name)
|
|
|
|
d.Set("protocol", p.Protocol)
|
|
|
|
d.Set("subnet_id", p.SubnetID)
|
|
|
|
d.Set("lb_method", p.LBMethod)
|
|
|
|
|
2015-01-27 02:28:01 +01:00
|
|
|
if t, exists := d.GetOk("tenant_id"); exists && t != "" {
|
|
|
|
d.Set("tenant_id", p.TenantID)
|
2015-01-14 19:55:17 +01:00
|
|
|
} else {
|
|
|
|
d.Set("tenant_id", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("monitor_ids", p.MonitorIDs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBPoolV1Update(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:17 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:55:17 +01:00
|
|
|
|
|
|
|
var updateOpts pools.UpdateOpts
|
|
|
|
if d.HasChange("name") {
|
|
|
|
updateOpts.Name = d.Get("name").(string)
|
|
|
|
}
|
|
|
|
if d.HasChange("lb_method") {
|
|
|
|
updateOpts.LBMethod = d.Get("lb_method").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating OpenStack LB Pool %s with options: %+v", d.Id(), updateOpts)
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err = pools.Update(networkingClient, d.Id(), updateOpts).Extract()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack LB Pool: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("monitor_ids") {
|
|
|
|
oldMIDsRaw, newMIDsRaw := d.GetChange("security_groups")
|
|
|
|
oldMIDsSet, newMIDsSet := oldMIDsRaw.(*schema.Set), newMIDsRaw.(*schema.Set)
|
|
|
|
monitorsToAdd := newMIDsSet.Difference(oldMIDsSet)
|
|
|
|
monitorsToRemove := oldMIDsSet.Difference(newMIDsSet)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Monitors to add: %v", monitorsToAdd)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Monitors to remove: %v", monitorsToRemove)
|
|
|
|
|
|
|
|
for _, m := range monitorsToAdd.List() {
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err := pools.AssociateMonitor(networkingClient, d.Id(), m.(string)).Extract()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error associating monitor (%s) with OpenStack server (%s): %s", m.(string), d.Id(), err)
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Associated monitor (%s) with pool (%s)", m.(string), d.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range monitorsToRemove.List() {
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err := pools.DisassociateMonitor(networkingClient, d.Id(), m.(string)).Extract()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error disassociating monitor (%s) from OpenStack server (%s): %s", m.(string), d.Id(), err)
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Disassociated monitor (%s) from pool (%s)", m.(string), d.Id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceLBPoolV1Read(d, meta)
|
2015-01-14 19:55:17 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBPoolV1Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:17 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:55:17 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
err = pools.Delete(networkingClient, d.Id()).ExtractErr()
|
2015-01-14 19:55:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack LB Pool: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourcePoolMonitorIDsV1(d *schema.ResourceData) []string {
|
2015-01-14 19:55:17 +01:00
|
|
|
mIDsRaw := d.Get("monitor_ids").(*schema.Set)
|
|
|
|
mIDs := make([]string, mIDsRaw.Len())
|
|
|
|
for i, raw := range mIDsRaw.List() {
|
|
|
|
mIDs[i] = raw.(string)
|
|
|
|
}
|
|
|
|
return mIDs
|
|
|
|
}
|