2015-01-14 19:55:36 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-11-14 20:47:37 +01:00
|
|
|
"time"
|
2015-01-14 19:55:36 +01:00
|
|
|
|
2016-09-04 04:54:00 +02:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
|
2015-11-14 20:47:37 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-01-14 19:55:36 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBVipV1() *schema.Resource {
|
2015-01-14 19:55:36 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceLBVipV1Create,
|
|
|
|
Read: resourceLBVipV1Read,
|
|
|
|
Update: resourceLBVipV1Update,
|
|
|
|
Delete: resourceLBVipV1Delete,
|
2016-07-15 10:49:31 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-01-14 19:55:36 +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,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
2015-01-26 18:54:07 +01:00
|
|
|
},
|
2015-01-14 19:55:36 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"subnet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"pool_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-11-14 21:19:06 +01:00
|
|
|
Computed: true,
|
2015-01-14 19:55:36 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-11-14 21:19:06 +01:00
|
|
|
Computed: true,
|
2015-01-14 19:55:36 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-11-14 21:19:06 +01:00
|
|
|
Computed: true,
|
2015-01-14 19:55:36 +01:00
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"persistence": &schema.Schema{
|
2015-01-26 19:38:33 +01:00
|
|
|
Type: schema.TypeMap,
|
2015-01-14 19:55:36 +01:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"conn_limit": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-11-14 21:19:06 +01:00
|
|
|
Computed: true,
|
2015-01-14 19:55:36 +01:00
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-04-16 02:37:56 +02:00
|
|
|
"port_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"floating_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-01-14 19:55:36 +01:00
|
|
|
"admin_state_up": &schema.Schema{
|
2015-11-14 21:19:06 +01:00
|
|
|
Type: schema.TypeBool,
|
2015-01-14 19:55:36 +01:00
|
|
|
Optional: true,
|
2015-11-14 21:19:06 +01:00
|
|
|
Computed: true,
|
2015-01-14 19:55:36 +01:00
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBVipV1Create(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:36 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
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:36 +01:00
|
|
|
|
|
|
|
createOpts := vips.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
SubnetID: d.Get("subnet_id").(string),
|
|
|
|
Protocol: d.Get("protocol").(string),
|
|
|
|
ProtocolPort: d.Get("port").(int),
|
|
|
|
PoolID: d.Get("pool_id").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
Address: d.Get("address").(string),
|
|
|
|
Description: d.Get("description").(string),
|
2015-01-26 19:09:27 +01:00
|
|
|
Persistence: resourceVipPersistenceV1(d),
|
2015-01-14 19:55:36 +01:00
|
|
|
ConnLimit: gophercloud.MaybeInt(d.Get("conn_limit").(int)),
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:19:06 +01:00
|
|
|
asu := d.Get("admin_state_up").(bool)
|
|
|
|
createOpts.AdminStateUp = &asu
|
2015-01-14 19:55:36 +01:00
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2015-01-26 18:54:07 +01:00
|
|
|
p, err := vips.Create(networkingClient, createOpts).Extract()
|
2015-01-14 19:55:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack LB VIP: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] LB VIP ID: %s", p.ID)
|
|
|
|
|
2015-11-14 20:47:37 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for OpenStack LB VIP (%s) to become available.", p.ID)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING_CREATE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-11-14 20:47:37 +01:00
|
|
|
Refresh: waitForLBVIPActive(networkingClient, p.ID),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-16 02:37:56 +02:00
|
|
|
floatingIP := d.Get("floating_ip").(string)
|
|
|
|
if floatingIP != "" {
|
|
|
|
lbVipV1AssignFloatingIP(floatingIP, p.PortID, networkingClient)
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
d.SetId(p.ID)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceLBVipV1Read(d, meta)
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBVipV1Read(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:36 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
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:36 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
p, err := vips.Get(networkingClient, d.Id()).Extract()
|
2015-01-14 19:55:36 +01:00
|
|
|
if err != nil {
|
2015-02-10 04:27:39 +01:00
|
|
|
return CheckDeleted(d, err, "LB VIP")
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 05:46:19 +02:00
|
|
|
log.Printf("[DEBUG] Retrieved OpenStack LB VIP %s: %+v", d.Id(), p)
|
2015-01-14 19:55:36 +01:00
|
|
|
|
|
|
|
d.Set("name", p.Name)
|
|
|
|
d.Set("subnet_id", p.SubnetID)
|
|
|
|
d.Set("protocol", p.Protocol)
|
|
|
|
d.Set("port", p.ProtocolPort)
|
|
|
|
d.Set("pool_id", p.PoolID)
|
2015-04-16 02:37:56 +02:00
|
|
|
d.Set("port_id", p.PortID)
|
2015-11-14 21:19:06 +01:00
|
|
|
d.Set("tenant_id", p.TenantID)
|
|
|
|
d.Set("address", p.Address)
|
|
|
|
d.Set("description", p.Description)
|
|
|
|
d.Set("conn_limit", p.ConnLimit)
|
|
|
|
d.Set("admin_state_up", p.AdminStateUp)
|
2015-01-14 19:55:36 +01:00
|
|
|
|
2016-07-15 10:49:31 +02:00
|
|
|
// Set the persistence method being used
|
|
|
|
persistence := make(map[string]interface{})
|
|
|
|
if p.Persistence.Type != "" {
|
|
|
|
persistence["type"] = p.Persistence.Type
|
|
|
|
}
|
|
|
|
if p.Persistence.CookieName != "" {
|
|
|
|
persistence["cookie_name"] = p.Persistence.CookieName
|
|
|
|
}
|
|
|
|
d.Set("persistence", persistence)
|
|
|
|
|
2016-12-04 15:24:07 +01:00
|
|
|
d.Set("region", GetRegion(d))
|
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBVipV1Update(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:36 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
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:36 +01:00
|
|
|
|
|
|
|
var updateOpts vips.UpdateOpts
|
|
|
|
if d.HasChange("name") {
|
2016-09-04 04:54:00 +02:00
|
|
|
v := d.Get("name").(string)
|
|
|
|
updateOpts.Name = &v
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
2016-09-04 04:54:00 +02:00
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
if d.HasChange("pool_id") {
|
2016-09-04 04:54:00 +02:00
|
|
|
v := d.Get("pool_id").(string)
|
|
|
|
updateOpts.PoolID = &v
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
2016-09-04 04:54:00 +02:00
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
if d.HasChange("description") {
|
2016-09-04 04:54:00 +02:00
|
|
|
v := d.Get("description").(string)
|
|
|
|
updateOpts.Description = &v
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
2016-09-04 04:54:00 +02:00
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
if d.HasChange("conn_limit") {
|
|
|
|
updateOpts.ConnLimit = gophercloud.MaybeInt(d.Get("conn_limit").(int))
|
|
|
|
}
|
2016-09-04 04:54:00 +02:00
|
|
|
|
2015-04-16 02:37:56 +02:00
|
|
|
if d.HasChange("floating_ip") {
|
|
|
|
portID := d.Get("port_id").(string)
|
|
|
|
|
|
|
|
// Searching for a floating IP assigned to the VIP
|
|
|
|
listOpts := floatingips.ListOpts{
|
|
|
|
PortID: portID,
|
|
|
|
}
|
|
|
|
page, err := floatingips.List(networkingClient, listOpts).AllPages()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fips, err := floatingips.ExtractFloatingIPs(page)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a floating IP is found we unassign it
|
|
|
|
if len(fips) == 1 {
|
2016-10-16 20:54:36 +02:00
|
|
|
portID := ""
|
2015-04-16 02:37:56 +02:00
|
|
|
updateOpts := floatingips.UpdateOpts{
|
2016-10-16 20:54:36 +02:00
|
|
|
PortID: &portID,
|
2015-04-16 02:37:56 +02:00
|
|
|
}
|
|
|
|
if err = floatingips.Update(networkingClient, fips[0].ID, updateOpts).Err; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the updated floating IP
|
|
|
|
floatingIP := d.Get("floating_ip").(string)
|
|
|
|
if floatingIP != "" {
|
|
|
|
lbVipV1AssignFloatingIP(floatingIP, portID, networkingClient)
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 04:54:00 +02:00
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
if d.HasChange("admin_state_up") {
|
2015-11-14 21:19:06 +01:00
|
|
|
asu := d.Get("admin_state_up").(bool)
|
|
|
|
updateOpts.AdminStateUp = &asu
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-16 04:02:12 +02:00
|
|
|
// Persistence has to be included, even if it hasn't changed.
|
|
|
|
updateOpts.Persistence = resourceVipPersistenceV1(d)
|
|
|
|
|
2015-01-14 19:55:36 +01:00
|
|
|
log.Printf("[DEBUG] Updating OpenStack LB VIP %s with options: %+v", d.Id(), updateOpts)
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err = vips.Update(networkingClient, d.Id(), updateOpts).Extract()
|
2015-01-14 19:55:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack LB VIP: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceLBVipV1Read(d, meta)
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBVipV1Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:55:36 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
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:36 +01:00
|
|
|
|
2015-11-14 20:47:37 +01:00
|
|
|
stateConf := &resource.StateChangeConf{
|
2016-03-10 16:35:38 +01:00
|
|
|
Pending: []string{"ACTIVE", "PENDING_DELETE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"DELETED"},
|
2015-11-14 20:47:37 +01:00
|
|
|
Refresh: waitForLBVIPDelete(networkingClient, d.Id()),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
2015-01-14 19:55:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack LB VIP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceVipPersistenceV1(d *schema.ResourceData) *vips.SessionPersistence {
|
2015-01-14 19:55:36 +01:00
|
|
|
rawP := d.Get("persistence").(interface{})
|
|
|
|
rawMap := rawP.(map[string]interface{})
|
2015-01-26 19:38:33 +01:00
|
|
|
if len(rawMap) != 0 {
|
2015-01-26 19:09:27 +01:00
|
|
|
p := vips.SessionPersistence{}
|
|
|
|
if t, ok := rawMap["type"]; ok {
|
|
|
|
p.Type = t.(string)
|
|
|
|
}
|
|
|
|
if c, ok := rawMap["cookie_name"]; ok {
|
|
|
|
p.CookieName = c.(string)
|
|
|
|
}
|
|
|
|
return &p
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
2015-01-26 19:09:27 +01:00
|
|
|
return nil
|
2015-01-14 19:55:36 +01:00
|
|
|
}
|
2015-04-16 02:37:56 +02:00
|
|
|
|
|
|
|
func lbVipV1AssignFloatingIP(floatingIP, portID string, networkingClient *gophercloud.ServiceClient) error {
|
|
|
|
log.Printf("[DEBUG] Assigning floating IP %s to VIP %s", floatingIP, portID)
|
|
|
|
|
|
|
|
listOpts := floatingips.ListOpts{
|
|
|
|
FloatingIP: floatingIP,
|
|
|
|
}
|
|
|
|
page, err := floatingips.List(networkingClient, listOpts).AllPages()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fips, err := floatingips.ExtractFloatingIPs(page)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(fips) != 1 {
|
|
|
|
return fmt.Errorf("Unable to retrieve floating IP '%s'", floatingIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOpts := floatingips.UpdateOpts{
|
2016-10-16 20:54:36 +02:00
|
|
|
PortID: &portID,
|
2015-04-16 02:37:56 +02:00
|
|
|
}
|
|
|
|
if err = floatingips.Update(networkingClient, fips[0].ID, updateOpts).Err; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-14 20:47:37 +01:00
|
|
|
|
|
|
|
func waitForLBVIPActive(networkingClient *gophercloud.ServiceClient, vipId string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
p, err := vips.Get(networkingClient, vipId).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack LB VIP: %+v", p)
|
|
|
|
if p.Status == "ACTIVE" {
|
|
|
|
return p, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, p.Status, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForLBVIPDelete(networkingClient *gophercloud.ServiceClient, vipId string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
log.Printf("[DEBUG] Attempting to delete OpenStack LB VIP %s", vipId)
|
|
|
|
|
|
|
|
p, err := vips.Get(networkingClient, vipId).Extract()
|
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2015-11-14 20:47:37 +01:00
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack LB VIP %s", vipId)
|
|
|
|
return p, "DELETED", nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
return p, "ACTIVE", err
|
2015-11-14 20:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack LB VIP: %+v", p)
|
|
|
|
err = vips.Delete(networkingClient, vipId).ExtractErr()
|
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2015-11-14 20:47:37 +01:00
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack LB VIP %s", vipId)
|
|
|
|
return p, "DELETED", nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
return p, "ACTIVE", err
|
2015-11-14 20:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack LB VIP %s still active.", vipId)
|
|
|
|
return p, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|