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/pools"
|
2016-06-09 22:19:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceMemberV2() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceMemberV2Create,
|
|
|
|
Read: resourceMemberV2Read,
|
|
|
|
Update: resourceMemberV2Update,
|
|
|
|
Delete: resourceMemberV2Delete,
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"weight": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < 1 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"Only numbers greater than 0 are supported values for 'weight'"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"subnet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"pool_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMemberV2Create(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
adminStateUp := d.Get("admin_state_up").(bool)
|
2016-09-04 04:54:05 +02:00
|
|
|
createOpts := pools.CreateMemberOpts{
|
2016-06-09 22:19:52 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
Address: d.Get("address").(string),
|
|
|
|
ProtocolPort: d.Get("protocol_port").(int),
|
|
|
|
Weight: d.Get("weight").(int),
|
|
|
|
AdminStateUp: &adminStateUp,
|
|
|
|
}
|
2016-09-04 04:54:05 +02:00
|
|
|
|
2016-06-09 22:19:52 +02:00
|
|
|
// Must omit if not set
|
2016-09-04 04:54:05 +02:00
|
|
|
if v, ok := d.GetOk("subnet_id"); ok {
|
|
|
|
createOpts.SubnetID = v.(string)
|
2016-06-09 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
poolID := d.Get("pool_id").(string)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2016-09-04 04:54:05 +02:00
|
|
|
member, err := pools.CreateMember(networkingClient, poolID, createOpts).Extract()
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack LBaaSV2 member: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] member ID: %s", member.ID)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for Openstack LBaaSV2 member (%s) to become available.", member.ID)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING_CREATE"},
|
|
|
|
Target: []string{"ACTIVE"},
|
|
|
|
Refresh: waitForMemberActive(networkingClient, poolID, member.ID),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(member.ID)
|
|
|
|
|
|
|
|
return resourceMemberV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMemberV2Read(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-04 04:54:05 +02:00
|
|
|
member, err := pools.GetMember(networkingClient, d.Get("pool_id").(string), d.Id()).Extract()
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return CheckDeleted(d, err, "LBV2 Member")
|
|
|
|
}
|
|
|
|
|
2016-10-11 05:46:19 +02:00
|
|
|
log.Printf("[DEBUG] Retrieved OpenStack LBaaSV2 Member %s: %+v", d.Id(), member)
|
2016-06-09 22:19:52 +02:00
|
|
|
|
|
|
|
d.Set("name", member.Name)
|
|
|
|
d.Set("weight", member.Weight)
|
|
|
|
d.Set("admin_state_up", member.AdminStateUp)
|
|
|
|
d.Set("tenant_id", member.TenantID)
|
|
|
|
d.Set("subnet_id", member.SubnetID)
|
|
|
|
d.Set("address", member.Address)
|
|
|
|
d.Set("protocol_port", member.ProtocolPort)
|
|
|
|
d.Set("id", member.ID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMemberV2Update(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-04 04:54:05 +02:00
|
|
|
var updateOpts pools.UpdateMemberOpts
|
2016-06-09 22:19:52 +02:00
|
|
|
if d.HasChange("name") {
|
|
|
|
updateOpts.Name = d.Get("name").(string)
|
|
|
|
}
|
|
|
|
if d.HasChange("weight") {
|
|
|
|
updateOpts.Weight = d.Get("weight").(int)
|
|
|
|
}
|
|
|
|
if d.HasChange("admin_state_up") {
|
|
|
|
asu := d.Get("admin_state_up").(bool)
|
|
|
|
updateOpts.AdminStateUp = &asu
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating OpenStack LBaaSV2 Member %s with options: %+v", d.Id(), updateOpts)
|
|
|
|
|
2016-09-04 04:54:05 +02:00
|
|
|
_, err = pools.UpdateMember(networkingClient, d.Get("pool_id").(string), d.Id(), updateOpts).Extract()
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack LBaaSV2 Member: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceMemberV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMemberV2Delete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
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: waitForMemberDelete(networkingClient, d.Get("pool_id").(string), d.Id()),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack LBaaSV2 Member: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForMemberActive(networkingClient *gophercloud.ServiceClient, poolID string, memberID string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
2016-09-04 04:54:05 +02:00
|
|
|
member, err := pools.GetMember(networkingClient, poolID, memberID).Extract()
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The member resource has no Status attribute, so a successful Get is the best we can do
|
|
|
|
log.Printf("[DEBUG] OpenStack LBaaSV2 Member: %+v", member)
|
|
|
|
return member, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForMemberDelete(networkingClient *gophercloud.ServiceClient, poolID string, memberID string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
log.Printf("[DEBUG] Attempting to delete OpenStack LBaaSV2 Member %s", memberID)
|
|
|
|
|
2016-09-04 04:54:05 +02:00
|
|
|
member, err := pools.GetMember(networkingClient, poolID, memberID).Extract()
|
2016-06-09 22:19:52 +02:00
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2016-06-09 22:19:52 +02:00
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack LBaaSV2 Member %s", memberID)
|
|
|
|
return member, "DELETED", nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
return member, "ACTIVE", err
|
2016-06-09 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Openstack LBaaSV2 Member: %+v", member)
|
|
|
|
err = pools.DeleteMember(networkingClient, poolID, memberID).ExtractErr()
|
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2016-06-09 22:19:52 +02:00
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack LBaaSV2 Member %s", memberID)
|
|
|
|
return member, "DELETED", nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
return member, "ACTIVE", err
|
2016-06-09 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack LBaaSV2 Member %s still active.", memberID)
|
|
|
|
return member, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
}
|