forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hashicorp#1 from julienvey/IPmanagement
Floating IP resource
- Loading branch information
Showing
5 changed files
with
406 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
builtin/providers/openstack/resource_openstack_networking_floatingip_v2.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.