Add floating IP resource

This commit is contained in:
Julien Vey 2015-02-05 08:23:46 +01:00 committed by Jon Perritt
parent aae87816f6
commit 9aa9c90248
3 changed files with 250 additions and 0 deletions

View File

@ -68,6 +68,7 @@ func Provider() terraform.ResourceProvider {
"openstack_lb_vip_v1": resourceLBVipV1(),
"openstack_networking_network_v2": resourceNetworkingNetworkV2(),
"openstack_networking_subnet_v2": resourceNetworkingSubnetV2(),
"openstack_networking_floatingip_v2": resourceNetworkingFloatingIPV2(),
"openstack_objectstorage_container_v1": resourceObjectStorageContainerV1(),
},

View File

@ -0,0 +1,160 @@
package openstack
import (
"fmt"
"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,
Delete: resourceNetworkFloatingIPV2Delete,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: envDefaultFunc("OS_REGION_NAME"),
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"pool": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
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))
}
floatingIP, err := floatingips.Create(networkClient, floatingips.CreateOpts{
FloatingNetworkID: poolID,
}).Extract()
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 {
return fmt.Errorf("Error retrieving floatingIP: %s", err)
}
d.Set("region", d.Get("region").(string))
d.Set("address", floatingIP.FloatingIP)
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
}
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
}

View File

@ -0,0 +1,89 @@
package openstack
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
)
func TestAccNetworkingV2FloatingIP_basic(t *testing.T) {
var floatingIP floatingips.FloatingIP
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2FloatingIP_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2FloatingIPExists(t, "openstack_networking_floatingip_v2.foo", &floatingIP),
),
},
},
})
}
func testAccCheckNetworkingV2FloatingIPDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkClient, err := config.networkingV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckNetworkingV2FloatingIPDestroy) Error creating OpenStack floating IP: %s", err)
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "openstack_networking_floatingip_v2" {
continue
}
_, err := floatingips.Get(networkClient, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("FloatingIP still exists")
}
}
return nil
}
func testAccCheckNetworkingV2FloatingIPExists(t *testing.T, n string, kp *floatingips.FloatingIP) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
networkClient, err := config.networkingV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckNetworkingV2FloatingIPExists) Error creating OpenStack networking client: %s", err)
}
found, err := floatingips.Get(networkClient, rs.Primary.ID).Extract()
if err != nil {
return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("FloatingIP not found")
}
*kp = *found
return nil
}
}
var testAccNetworkingV2FloatingIP_basic = fmt.Sprintf(`
resource "openstack_networking_floatingip_v2" "foo" {
region = "%s"
pool = "PublicNetwork-01"
}`,
OS_REGION_NAME)