2016-06-09 22:19:52 +02:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
2016-09-04 04:54:05 +02:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers"
|
2016-06-09 22:19:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceLoadBalancerV2() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceLoadBalancerV2Create,
|
|
|
|
Read: resourceLoadBalancerV2Read,
|
|
|
|
Update: resourceLoadBalancerV2Update,
|
|
|
|
Delete: resourceLoadBalancerV2Delete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
|
|
|
},
|
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vip_subnet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-01 11:25:18 +01:00
|
|
|
"vip_port_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-06-09 22:19:52 +02:00
|
|
|
"admin_state_up": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
2016-07-13 03:08:16 +02:00
|
|
|
Default: true,
|
2016-06-09 22:19:52 +02:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"flavor": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"provider": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLoadBalancerV2Create(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
adminStateUp := d.Get("admin_state_up").(bool)
|
|
|
|
createOpts := loadbalancers.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
VipSubnetID: d.Get("vip_subnet_id").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
VipAddress: d.Get("vip_address").(string),
|
|
|
|
AdminStateUp: &adminStateUp,
|
|
|
|
Flavor: d.Get("flavor").(string),
|
|
|
|
Provider: d.Get("provider").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
|
|
|
lb, err := loadbalancers.Create(networkingClient, createOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack LoadBalancer: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] LoadBalancer ID: %s", lb.ID)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for Openstack LoadBalancer (%s) to become available.", lb.ID)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING_CREATE"},
|
|
|
|
Target: []string{"ACTIVE"},
|
|
|
|
Refresh: waitForLoadBalancerActive(networkingClient, lb.ID),
|
2016-06-11 06:17:21 +02:00
|
|
|
Timeout: 20 * time.Minute,
|
2016-06-09 22:19:52 +02:00
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(lb.ID)
|
|
|
|
|
|
|
|
return resourceLoadBalancerV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLoadBalancerV2Read(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lb, err := loadbalancers.Get(networkingClient, d.Id()).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return CheckDeleted(d, err, "LoadBalancerV2")
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:41 +02:00
|
|
|
log.Printf("[DEBUG] Retrieved OpenStack LBaaSV2 LoadBalancer %s: %+v", d.Id(), lb)
|
2016-06-09 22:19:52 +02:00
|
|
|
|
|
|
|
d.Set("name", lb.Name)
|
|
|
|
d.Set("description", lb.Description)
|
|
|
|
d.Set("vip_subnet_id", lb.VipSubnetID)
|
|
|
|
d.Set("tenant_id", lb.TenantID)
|
|
|
|
d.Set("vip_address", lb.VipAddress)
|
2016-11-01 11:25:18 +01:00
|
|
|
d.Set("vip_port_id", lb.VipPortID)
|
2016-06-09 22:19:52 +02:00
|
|
|
d.Set("admin_state_up", lb.AdminStateUp)
|
|
|
|
d.Set("flavor", lb.Flavor)
|
|
|
|
d.Set("provider", lb.Provider)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLoadBalancerV2Update(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateOpts loadbalancers.UpdateOpts
|
|
|
|
if d.HasChange("name") {
|
|
|
|
updateOpts.Name = d.Get("name").(string)
|
|
|
|
}
|
|
|
|
if d.HasChange("description") {
|
|
|
|
updateOpts.Description = d.Get("description").(string)
|
|
|
|
}
|
|
|
|
if d.HasChange("admin_state_up") {
|
|
|
|
asu := d.Get("admin_state_up").(bool)
|
|
|
|
updateOpts.AdminStateUp = &asu
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating OpenStack LBaaSV2 LoadBalancer %s with options: %+v", d.Id(), updateOpts)
|
|
|
|
|
|
|
|
_, err = loadbalancers.Update(networkingClient, d.Id(), updateOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack LBaaSV2 LoadBalancer: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceLoadBalancerV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLoadBalancerV2Delete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"ACTIVE", "PENDING_DELETE"},
|
|
|
|
Target: []string{"DELETED"},
|
|
|
|
Refresh: waitForLoadBalancerDelete(networkingClient, d.Id()),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
2016-10-16 04:57:41 +02:00
|
|
|
return fmt.Errorf("Error deleting OpenStack LBaaSV2 LoadBalancer: %s", err)
|
2016-06-09 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForLoadBalancerActive(networkingClient *gophercloud.ServiceClient, lbID string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
lb, err := loadbalancers.Get(networkingClient, lbID).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:41 +02:00
|
|
|
log.Printf("[DEBUG] OpenStack LBaaSV2 LoadBalancer: %+v", lb)
|
2016-06-09 22:19:52 +02:00
|
|
|
if lb.ProvisioningStatus == "ACTIVE" {
|
|
|
|
return lb, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return lb, lb.ProvisioningStatus, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForLoadBalancerDelete(networkingClient *gophercloud.ServiceClient, lbID string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
2016-10-16 04:57:41 +02:00
|
|
|
log.Printf("[DEBUG] Attempting to delete OpenStack LBaaSV2 LoadBalancer %s", lbID)
|
2016-06-09 22:19:52 +02:00
|
|
|
|
|
|
|
lb, err := loadbalancers.Get(networkingClient, lbID).Extract()
|
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2016-10-16 04:57:41 +02:00
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack LBaaSV2 LoadBalancer %s", lbID)
|
2016-06-09 22:19:52 +02:00
|
|
|
return lb, "DELETED", nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
return lb, "ACTIVE", err
|
2016-06-09 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Openstack LoadBalancerV2: %+v", lb)
|
|
|
|
err = loadbalancers.Delete(networkingClient, lbID).ExtractErr()
|
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2016-10-16 04:57:41 +02:00
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack LBaaSV2 LoadBalancer %s", lbID)
|
2016-06-09 22:19:52 +02:00
|
|
|
return lb, "DELETED", nil
|
|
|
|
}
|
2016-10-16 04:57:41 +02:00
|
|
|
|
|
|
|
if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
|
|
|
|
if errCode.Actual == 409 {
|
|
|
|
log.Printf("[DEBUG] OpenStack LBaaSV2 LoadBalancer (%s) is still in use.", lbID)
|
|
|
|
return lb, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 06:57:25 +02:00
|
|
|
return lb, "ACTIVE", err
|
2016-06-09 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:41 +02:00
|
|
|
log.Printf("[DEBUG] OpenStack LBaaSV2 LoadBalancer (%s) still active.", lbID)
|
2016-06-09 22:19:52 +02:00
|
|
|
return lb, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
}
|