Skip to content

Commit

Permalink
Merge pull request #184 from niuzhenguo/fip
Browse files Browse the repository at this point in the history
Remove unneeded pool related API from FIP
  • Loading branch information
niuzhenguo authored Aug 16, 2019
2 parents 57b94cf + 5cd3f65 commit 75d7c30
Showing 1 changed file with 11 additions and 75 deletions.
86 changes: 11 additions & 75 deletions huaweicloud/resource_huaweicloud_networking_floatingip_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/networks"
"github.com/huaweicloud/golangsdk/pagination"
)

const (
PoolID = "0a2228f2-7f8a-45f1-8e09-9039e1d09975"
PoolName = "admin_external_net"
)

func resourceNetworkingFloatingIPV2() *schema.Resource {
Expand Down Expand Up @@ -45,6 +49,9 @@ func resourceNetworkingFloatingIPV2() *schema.Resource {
Optional: true,
ForceNew: true,
Default: "admin_external_net",
ValidateFunc: validation.StringInSlice([]string{
"admin_external_net",
}, true),
},
"port_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -78,16 +85,9 @@ func resourceNetworkFloatingIPV2Create(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating HuaweiCloud 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))
}
createOpts := FloatingIPCreateOpts{
floatingips.CreateOpts{
FloatingNetworkID: poolID,
FloatingNetworkID: PoolID,
PortID: d.Get("port_id").(string),
TenantID: d.Get("tenant_id").(string),
FixedIP: d.Get("fixed_ip").(string),
Expand Down Expand Up @@ -136,12 +136,8 @@ func resourceNetworkFloatingIPV2Read(d *schema.ResourceData, meta interface{}) e
d.Set("address", floatingIP.FloatingIP)
d.Set("port_id", floatingIP.PortID)
d.Set("fixed_ip", floatingIP.FixedIP)
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)
d.Set("tenant_id", floatingIP.TenantID)
d.Set("pool", PoolName)

d.Set("region", GetRegion(d, config))

Expand Down Expand Up @@ -197,66 +193,6 @@ func resourceNetworkFloatingIPV2Delete(d *schema.ResourceData, meta interface{})
return nil
}

func getNetworkID(d *schema.ResourceData, meta interface{}, networkName string) (string, error) {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return "", fmt.Errorf("Error creating HuaweiCloud network client: %s", err)
}

opts := networks.ListOpts{Name: networkName}
pager := networks.List(networkingClient, 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)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return "", fmt.Errorf("Error creating HuaweiCloud network client: %s", err)
}

opts := networks.ListOpts{ID: networkID}
pager := networks.List(networkingClient, 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
}

func waitForFloatingIPActive(networkingClient *golangsdk.ServiceClient, fId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
f, err := floatingips.Get(networkingClient, fId).Extract()
Expand Down

0 comments on commit 75d7c30

Please sign in to comment.