-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b885d5
commit 0371f94
Showing
5 changed files
with
309 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/IBM/vpc-go-sdk/vpcclassicv1" | ||
"github.com/IBM/vpc-go-sdk/vpcv1" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
const ( | ||
floatingIPName = "name" | ||
floatingIPAddress = "address" | ||
floatingIPStatus = "status" | ||
floatingIPZone = "zone" | ||
floatingIPTarget = "target" | ||
floatingIPTags = "tags" | ||
) | ||
|
||
func dataSourceIBMISFloatingIP() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceIBMISFloatingIPRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
|
||
floatingIPName: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Name of the floating IP", | ||
}, | ||
|
||
floatingIPAddress: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Floating IP address", | ||
}, | ||
|
||
floatingIPStatus: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Floating IP status", | ||
}, | ||
|
||
floatingIPZone: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Zone name", | ||
}, | ||
|
||
floatingIPTarget: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Target info", | ||
}, | ||
|
||
floatingIPTags: { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: resourceIBMVPCHash, | ||
Description: "Floating IP tags", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceIBMISFloatingIPRead(d *schema.ResourceData, meta interface{}) error { | ||
userDetails, err := meta.(ClientSession).BluemixUserDetails() | ||
if err != nil { | ||
return err | ||
} | ||
floatingIPName := d.Get(isFloatingIPName).(string) | ||
if userDetails.generation == 1 { | ||
err := classicFloatingIPGet(d, meta, floatingIPName) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err := floatingIPGet(d, meta, floatingIPName) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func classicFloatingIPGet(d *schema.ResourceData, meta interface{}, name string) error { | ||
sess, err := classicVpcClient(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
start := "" | ||
allFloatingIPs := []vpcclassicv1.FloatingIP{} | ||
for { | ||
floatingIPOptions := &vpcclassicv1.ListFloatingIpsOptions{} | ||
if start != "" { | ||
floatingIPOptions.Start = &start | ||
} | ||
floatingIPs, response, err := sess.ListFloatingIps(floatingIPOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Fetching floating IPs %s\n%s", err, response) | ||
} | ||
start = GetNext(floatingIPs.Next) | ||
allFloatingIPs = append(allFloatingIPs, floatingIPs.FloatingIps...) | ||
if start == "" { | ||
break | ||
} | ||
} | ||
|
||
for _, ip := range allFloatingIPs { | ||
if *ip.Name == name { | ||
|
||
d.Set(floatingIPName, *ip.Name) | ||
d.Set(floatingIPAddress, *ip.Address) | ||
d.Set(floatingIPStatus, *ip.Status) | ||
d.Set(floatingIPZone, *ip.Zone.Name) | ||
|
||
target, ok := ip.Target.(*vpcclassicv1.FloatingIPTarget) | ||
if ok { | ||
d.Set(floatingIPTarget, target.ID) | ||
} | ||
|
||
tags, err := GetTagsUsingCRN(meta, *ip.CRN) | ||
if err != nil { | ||
fmt.Printf("Error on get of vpc Floating IP (%s) tags: %s", *ip.Address, err) | ||
} | ||
|
||
d.Set(floatingIPTags, tags) | ||
d.SetId(*ip.ID) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("No floatingIP found with name %s", name) | ||
} | ||
|
||
func floatingIPGet(d *schema.ResourceData, meta interface{}, name string) error { | ||
sess, err := vpcClient(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
start := "" | ||
allFloatingIPs := []vpcv1.FloatingIP{} | ||
for { | ||
floatingIPOptions := &vpcv1.ListFloatingIpsOptions{} | ||
if start != "" { | ||
floatingIPOptions.Start = &start | ||
} | ||
floatingIPs, response, err := sess.ListFloatingIps(floatingIPOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Fetching floating IPs %s\n%s", err, response) | ||
} | ||
start = GetNext(floatingIPs.Next) | ||
allFloatingIPs = append(allFloatingIPs, floatingIPs.FloatingIps...) | ||
if start == "" { | ||
break | ||
} | ||
} | ||
|
||
for _, ip := range allFloatingIPs { | ||
if *ip.Name == name { | ||
|
||
d.Set(floatingIPName, *ip.Name) | ||
d.Set(floatingIPAddress, *ip.Address) | ||
d.Set(floatingIPStatus, *ip.Status) | ||
d.Set(floatingIPZone, *ip.Zone.Name) | ||
|
||
target, ok := ip.Target.(*vpcv1.FloatingIPTarget) | ||
if ok { | ||
d.Set(floatingIPTarget, target.ID) | ||
} | ||
|
||
tags, err := GetTagsUsingCRN(meta, *ip.CRN) | ||
if err != nil { | ||
fmt.Printf("Error on get of vpc Floating IP (%s) tags: %s", *ip.Address, err) | ||
} | ||
|
||
d.Set(floatingIPTags, tags) | ||
d.SetId(*ip.ID) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("No floatingIP found with name %s", name) | ||
|
||
} |
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,81 @@ | ||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccIBMISFloatingIPDataSource_basic(t *testing.T) { | ||
resName := "data.ibm_is_floating_ip.test" | ||
|
||
vpcname := fmt.Sprintf("tfip-vpc-%d", acctest.RandIntRange(10, 100)) | ||
fipname := fmt.Sprintf("tfip-%d", acctest.RandIntRange(10, 100)) | ||
instancename := fmt.Sprintf("tfip-instance-%d", acctest.RandIntRange(10, 100)) | ||
subnetname := fmt.Sprintf("tfip-subnet-%d", acctest.RandIntRange(10, 100)) | ||
publicKey := strings.TrimSpace(` | ||
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR | ||
`) | ||
sshname := fmt.Sprintf("tfip-sshname-%d", acctest.RandIntRange(10, 100)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckIBMISFloatingIPDataSourceConfig(vpcname, subnetname, sshname, publicKey, instancename, fipname), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resName, "name", fipname), | ||
resource.TestCheckResourceAttr(resName, "zone", ISZoneName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMISFloatingIPDataSourceConfig(vpcname, subnetname, sshname, publicKey, instancename, fipname string) string { | ||
// status filter defaults to empty | ||
return fmt.Sprintf(` | ||
resource "ibm_is_vpc" "testacc_vpc" { | ||
name = "%s" | ||
} | ||
resource "ibm_is_subnet" "testacc_subnet" { | ||
name = "%s" | ||
vpc = ibm_is_vpc.testacc_vpc.id | ||
zone = "%s" | ||
ipv4_cidr_block = "%s" | ||
} | ||
resource "ibm_is_ssh_key" "testacc_sshkey" { | ||
name = "%s" | ||
public_key = "%s" | ||
} | ||
resource "ibm_is_instance" "testacc_instance" { | ||
name = "%s" | ||
image = "%s" | ||
profile = "%s" | ||
primary_network_interface { | ||
port_speed = "100" | ||
subnet = ibm_is_subnet.testacc_subnet.id | ||
} | ||
vpc = ibm_is_vpc.testacc_vpc.id | ||
zone = "%s" | ||
keys = [ibm_is_ssh_key.testacc_sshkey.id] | ||
} | ||
resource "ibm_is_floating_ip" "testacc_floatingip" { | ||
name = "%s" | ||
target = ibm_is_instance.testacc_instance.primary_network_interface[0].id | ||
} | ||
data "ibm_is_floating_ip" "test" { | ||
name = ibm_is_floating_ip.testacc_floatingip.name | ||
} | ||
`, vpcname, subnetname, ISZoneName, ISCIDR, sshname, publicKey, instancename, isImage, instanceProfileName, ISZoneName, fipname) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
layout: "ibm" | ||
page_title: "IBM : floating_ip" | ||
sidebar_current: "docs-ibm-datasource-floating-ip" | ||
description: |- | ||
Fechtes floating ip information. | ||
--- | ||
|
||
# ibm\_floating\_ip | ||
|
||
Import the details of vpc floating ip on IBM Cloud as a read-only data source. You can then reference the fields of the data source in other resources within the same configuration using interpolation syntax. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "ibm_is_floating_ip" "test" { | ||
name = "test-fp" | ||
} | ||
``` | ||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required, string) The name of the floating ip. | ||
|
||
## Attribute Reference | ||
|
||
The following attributes are exported: | ||
* `id` - An alphanumeric value identifying the floating ip. | ||
* `address` - Floating IP address that was created. | ||
* `status` - Provisioning status of the floating IP address. | ||
* `tags` - Tags associate with VPC. | ||
* `target` - ID of the network interface use to allocate the IP address. | ||
* `zone` - Name of the zone where to create the floating IP address. |