Skip to content

Commit

Permalink
DS: VPC Floating IP
Browse files Browse the repository at this point in the history
  • Loading branch information
umarali-nagoor committed Aug 13, 2020
1 parent 3b885d5 commit 0371f94
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-plugin-sdk v1.6.0 h1:Um5hsAL7kKsfTHtan8lybY/d03F2bHu4fjRB1H6Ag4U=
github.com/hashicorp/terraform-plugin-sdk v1.6.0/go.mod h1:H5QLx/uhwfxBZ59Bc5SqT19M4i+fYt7LZjHTpbLZiAg=
github.com/hashicorp/terraform-plugin-sdk v1.15.0 h1:bmYnTT7MqNXlUHDc7pT8E6uKT2g/upjlRLypJFK1OQU=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/vault v0.10.4/go.mod h1:KfSyffbKxoVyspOdlaGVjIuwLobi07qD1bAbosPMpP0=
Expand Down
190 changes: 190 additions & 0 deletions ibm/data_source_ibm_is_floating_ip.go
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)

}
81 changes: 81 additions & 0 deletions ibm/data_source_ibm_is_floating_ip_test.go
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)
}
1 change: 1 addition & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func Provider() terraform.ResourceProvider {
"ibm_iam_user_profile": dataSourceIBMIAMUserProfile(),
"ibm_iam_service_id": dataSourceIBMIAMServiceID(),
"ibm_iam_service_policy": dataSourceIBMIAMServicePolicy(),
"ibm_is_floating_ip": dataSourceIBMISFloatingIP(),
"ibm_is_image": dataSourceIBMISImage(),
"ibm_is_images": dataSourceIBMISImages(),
"ibm_is_instance_profile": dataSourceIBMISInstanceProfile(),
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/is_floating_ip.html.markdown
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.

0 comments on commit 0371f94

Please sign in to comment.