2015-02-05 08:23:46 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-24 13:59:55 +01:00
|
|
|
"log"
|
2015-10-31 02:14:33 +01:00
|
|
|
"time"
|
2015-02-05 08:23:46 +01:00
|
|
|
|
2015-10-31 02:14:33 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-02-05 08:23:46 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-10-31 02:14:33 +01:00
|
|
|
|
|
|
|
"github.com/rackspace/gophercloud"
|
2015-02-05 08:23:46 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
|
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
|
|
|
|
"github.com/rackspace/gophercloud/pagination"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceNetworkingFloatingIPV2() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceNetworkFloatingIPV2Create,
|
|
|
|
Read: resourceNetworkFloatingIPV2Read,
|
2015-05-03 16:00:00 +02:00
|
|
|
Update: resourceNetworkFloatingIPV2Update,
|
2015-02-05 08:23:46 +01:00
|
|
|
Delete: resourceNetworkFloatingIPV2Delete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2015-04-11 06:11:34 +02:00
|
|
|
DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"),
|
2015-02-05 08:23:46 +01:00
|
|
|
},
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"pool": &schema.Schema{
|
2015-04-01 11:24:51 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DefaultFunc: envDefaultFunc("OS_POOL_NAME"),
|
2015-02-05 08:23:46 +01:00
|
|
|
},
|
2015-05-03 16:00:00 +02:00
|
|
|
"port_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-10-07 22:15:54 +02:00
|
|
|
Computed: true,
|
2015-05-03 16:00:00 +02:00
|
|
|
},
|
2015-02-05 08:23:46 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkFloatingIPV2Create(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2015-10-31 02:14:33 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
poolID, err := getNetworkID(d, meta, d.Get("pool").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving floating IP pool name: %s", err)
|
|
|
|
}
|
|
|
|
if len(poolID) == 0 {
|
|
|
|
return fmt.Errorf("No network found with name: %s", d.Get("pool").(string))
|
|
|
|
}
|
2015-03-24 13:59:55 +01:00
|
|
|
createOpts := floatingips.CreateOpts{
|
2015-02-05 08:23:46 +01:00
|
|
|
FloatingNetworkID: poolID,
|
2015-05-03 16:00:00 +02:00
|
|
|
PortID: d.Get("port_id").(string),
|
2015-03-24 13:59:55 +01:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2015-10-31 02:14:33 +01:00
|
|
|
floatingIP, err := floatingips.Create(networkingClient, createOpts).Extract()
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error allocating floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-31 02:14:33 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for OpenStack Neutron Floating IP (%s) to become available.", floatingIP.ID)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: "ACTIVE",
|
|
|
|
Refresh: waitForFloatingIPActive(networkingClient, floatingIP.ID),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
|
2015-02-05 08:23:46 +01:00
|
|
|
d.SetId(floatingIP.ID)
|
|
|
|
|
|
|
|
return resourceNetworkFloatingIPV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2015-10-31 02:14:33 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-31 02:14:33 +01:00
|
|
|
floatingIP, err := floatingips.Get(networkingClient, d.Id()).Extract()
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
2015-02-10 04:27:39 +01:00
|
|
|
return CheckDeleted(d, err, "floating IP")
|
2015-02-05 08:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("address", floatingIP.FloatingIP)
|
2015-05-03 16:00:00 +02:00
|
|
|
d.Set("port_id", floatingIP.PortID)
|
2015-02-05 08:23:46 +01:00
|
|
|
poolName, err := getNetworkName(d, meta, floatingIP.FloatingNetworkID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving floating IP pool name: %s", err)
|
|
|
|
}
|
|
|
|
d.Set("pool", poolName)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:00:00 +02:00
|
|
|
func resourceNetworkFloatingIPV2Update(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2015-10-31 02:14:33 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-05-03 16:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateOpts floatingips.UpdateOpts
|
|
|
|
|
|
|
|
if d.HasChange("port_id") {
|
|
|
|
updateOpts.PortID = d.Get("port_id").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
|
|
|
|
|
2015-10-31 02:14:33 +01:00
|
|
|
_, err = floatingips.Update(networkingClient, d.Id(), updateOpts).Extract()
|
2015-05-03 16:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceNetworkFloatingIPV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-02-05 08:23:46 +01:00
|
|
|
func resourceNetworkFloatingIPV2Delete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2015-10-31 02:14:33 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-31 02:14:33 +01:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"ACTIVE"},
|
|
|
|
Target: "DELETED",
|
|
|
|
Refresh: waitForFloatingIPDelete(networkingClient, d.Id()),
|
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
2015-10-31 02:14:33 +01:00
|
|
|
return fmt.Errorf("Error deleting OpenStack Neutron Floating IP: %s", err)
|
2015-02-05 08:23:46 +01:00
|
|
|
}
|
2015-10-31 02:14:33 +01:00
|
|
|
|
2015-02-05 08:23:46 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkID(d *schema.ResourceData, meta interface{}, networkName string) (string, error) {
|
|
|
|
config := meta.(*Config)
|
2015-10-31 02:14:33 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := networks.ListOpts{Name: networkName}
|
2015-10-31 02:14:33 +01:00
|
|
|
pager := networks.List(networkingClient, opts)
|
2015-02-05 08:23:46 +01:00
|
|
|
networkID := ""
|
|
|
|
|
|
|
|
err = pager.EachPage(func(page pagination.Page) (bool, error) {
|
|
|
|
networkList, err := networks.ExtractNetworks(page)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range networkList {
|
|
|
|
if n.Name == networkName {
|
|
|
|
networkID = n.ID
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return networkID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkName(d *schema.ResourceData, meta interface{}, networkID string) (string, error) {
|
|
|
|
config := meta.(*Config)
|
2015-10-31 02:14:33 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := networks.ListOpts{ID: networkID}
|
2015-10-31 02:14:33 +01:00
|
|
|
pager := networks.List(networkingClient, opts)
|
2015-02-05 08:23:46 +01:00
|
|
|
networkName := ""
|
|
|
|
|
|
|
|
err = pager.EachPage(func(page pagination.Page) (bool, error) {
|
|
|
|
networkList, err := networks.ExtractNetworks(page)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range networkList {
|
|
|
|
if n.ID == networkID {
|
|
|
|
networkName = n.Name
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return networkName, err
|
|
|
|
}
|
2015-10-31 02:14:33 +01:00
|
|
|
|
|
|
|
func waitForFloatingIPActive(networkingClient *gophercloud.ServiceClient, fId string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
f, err := floatingips.Get(networkingClient, fId).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack Neutron Floating IP: %+v", f)
|
|
|
|
if f.Status == "DOWN" || f.Status == "ACTIVE" {
|
|
|
|
return f, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForFloatingIPDelete(networkingClient *gophercloud.ServiceClient, fId string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
log.Printf("[DEBUG] Attempting to delete OpenStack Floating IP %s.\n", fId)
|
|
|
|
|
|
|
|
f, err := floatingips.Get(networkingClient, fId).Extract()
|
|
|
|
if err != nil {
|
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
|
|
|
if !ok {
|
|
|
|
return f, "ACTIVE", err
|
|
|
|
}
|
|
|
|
if errCode.Actual == 404 {
|
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack Floating IP %s", fId)
|
|
|
|
return f, "DELETED", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = floatingips.Delete(networkingClient, fId).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
|
|
|
if !ok {
|
|
|
|
return f, "ACTIVE", err
|
|
|
|
}
|
|
|
|
if errCode.Actual == 404 {
|
|
|
|
log.Printf("[DEBUG] Successfully deleted OpenStack Floating IP %s", fId)
|
|
|
|
return f, "DELETED", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack Floating IP %s still active.\n", fId)
|
|
|
|
return f, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
}
|