2015-02-05 08:23:46 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-24 13:59:55 +01:00
|
|
|
"log"
|
2015-02-05 08:23:46 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"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,
|
|
|
|
Default: "",
|
|
|
|
},
|
2015-02-05 08:23:46 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkFloatingIPV2Create(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
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)
|
|
|
|
floatingIP, err := floatingips.Create(networkClient, createOpts).Extract()
|
2015-02-05 08:23:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error allocating floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(floatingIP.ID)
|
|
|
|
|
|
|
|
return resourceNetworkFloatingIPV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
floatingIP, err := floatingips.Get(networkClient, d.Id()).Extract()
|
|
|
|
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)
|
|
|
|
networkClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
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)
|
|
|
|
|
|
|
|
_, err = floatingips.Update(networkClient, d.Id(), updateOpts).Extract()
|
|
|
|
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)
|
|
|
|
networkClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = floatingips.Delete(networkClient, d.Id()).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting floating IP: %s", err)
|
|
|
|
}
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkID(d *schema.ResourceData, meta interface{}, networkName string) (string, error) {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := networks.ListOpts{Name: networkName}
|
|
|
|
pager := networks.List(networkClient, opts)
|
|
|
|
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)
|
|
|
|
networkClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating OpenStack network client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := networks.ListOpts{ID: networkID}
|
|
|
|
pager := networks.List(networkClient, opts)
|
|
|
|
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
|
|
|
|
}
|