Skip to content

Commit

Permalink
feature: added support for public gateways datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
uibm authored and hkantare committed Apr 14, 2021
1 parent 07548fa commit 9967782
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/ibm-is-ng/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ resource "ibm_is_public_gateway" "publicgateway1" {
zone = var.zone1
}

data "ibm_is_public_gateway" "testacc_dspgw"{
name = ibm_is_public_gateway.publicgateway1.name
}

data "ibm_is_public_gateways" "publicgateways"{
}

data "ibm_is_vpc" "vpc1" {
name = ibm_is_vpc.vpc1.name
}
Expand Down
199 changes: 199 additions & 0 deletions ibm/data_source_ibm_is_public_gateways.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package ibm

import (
"fmt"
"log"
"time"

"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
isPublicGateways = "public_gateways"
)

func dataSourceIBMISPublicGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISPublicGatewaysRead,

Schema: map[string]*schema.Schema{
isPublicGateways: {
Type: schema.TypeList,
Description: "List of public gateways",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Public gateway id",
},
isPublicGatewayName: {
Type: schema.TypeString,
Computed: true,
Description: "Public gateway Name",
},

isPublicGatewayFloatingIP: {
Type: schema.TypeMap,
Computed: true,
Description: "Public gateway floating IP",
},

isPublicGatewayStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Public gateway instance status",
},

isPublicGatewayResourceGroup: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Public gateway resource group info",
},

isPublicGatewayVPC: {
Type: schema.TypeString,
Computed: true,
Description: "Public gateway VPC info",
},

isPublicGatewayZone: {
Type: schema.TypeString,
Computed: true,
Description: "Public gateway zone info",
},

isPublicGatewayTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: resourceIBMVPCHash,
Description: "Service tags for the public gateway instance",
},

ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},

ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},

ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},

ResourceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the resource",
},

ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
},
},
},
},
}
}

func dataSourceIBMISPublicGatewaysRead(d *schema.ResourceData, meta interface{}) error {
err := publicGatewaysGet(d, meta, name)
if err != nil {
return err
}
return nil
}

func publicGatewaysGet(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
rgroup := ""
if rg, ok := d.GetOk(isPublicGatewayResourceGroup); ok {
rgroup = rg.(string)
}
start := ""
allrecs := []vpcv1.PublicGateway{}
for {
listPublicGatewaysOptions := &vpcv1.ListPublicGatewaysOptions{}
if start != "" {
listPublicGatewaysOptions.Start = &start
}
if rgroup != "" {
listPublicGatewaysOptions.ResourceGroupID = &rgroup
}
publicgws, response, err := sess.ListPublicGateways(listPublicGatewaysOptions)
if err != nil {
return fmt.Errorf("Error Fetching public gateways %s\n%s", err, response)
}
start = GetNext(publicgws.Next)
allrecs = append(allrecs, publicgws.PublicGateways...)
if start == "" {
break
}
}
publicgwInfo := make([]map[string]interface{}, 0)
for _, publicgw := range allrecs {
id := *publicgw.ID
l := map[string]interface{}{
"id": id,
isPublicGatewayName: *publicgw.Name,
isPublicGatewayStatus: *publicgw.Status,
isPublicGatewayZone: *publicgw.Zone.Name,
isPublicGatewayVPC: *publicgw.VPC.ID,

ResourceName: *publicgw.Name,
ResourceCRN: *publicgw.CRN,
ResourceStatus: *publicgw.Status,
}
if publicgw.FloatingIP != nil {
floatIP := map[string]interface{}{
"id": *publicgw.FloatingIP.ID,
isPublicGatewayFloatingIPAddress: *publicgw.FloatingIP.Address,
}
l[isPublicGatewayFloatingIP] = floatIP
}
tags, err := GetTagsUsingCRN(meta, *publicgw.CRN)
if err != nil {
log.Printf(
"Error on get of vpc public gateway (%s) tags: %s", *publicgw.ID, err)
}
l[isPublicGatewayTags] = tags
controller, err := getBaseController(meta)
if err != nil {
return err
}
l[ResourceControllerURL] = controller + "/vpc-ext/network/publicGateways"
if publicgw.ResourceGroup != nil {
l[isPublicGatewayResourceGroup] = *publicgw.ResourceGroup.ID
l[ResourceGroupName] = *publicgw.ResourceGroup.Name
}
publicgwInfo = append(publicgwInfo, l)
}
d.SetId(dataSourceIBMISPublicGatewaysID(d))
d.Set(isPublicGateways, publicgwInfo)
return nil
}

// dataSourceIBMISPublicGatewaysID returns a reasonable ID for a Public Gateway list.
func dataSourceIBMISPublicGatewaysID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
53 changes: 53 additions & 0 deletions ibm/data_source_ibm_is_public_gateways_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package ibm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIBMISPublicGatewaysDatasource_basic(t *testing.T) {
var publicgw string
vpcname := fmt.Sprintf("tfpgw-vpc-%d", acctest.RandIntRange(10, 100))
name1 := fmt.Sprintf("tfpgw-name-%d", acctest.RandIntRange(10, 100))
zone := "us-south-1"

resName := "data.ibm_is_public_gateways.test1"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIBMISPublicGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISPublicGatewayDataSourceConfig(vpcname, name1, zone),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISPublicGatewayExists("ibm_is_public_gateway.testacc_public_gateway", publicgw),
resource.TestCheckResourceAttr(
"ibm_is_public_gateway.testacc_public_gateway", "name", name1),
resource.TestCheckResourceAttr(
"ibm_is_public_gateway.testacc_public_gateway", "zone", zone),
),
},
{
Config: testAccCheckIBMISPublicGatewaysDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resName, "public_gateways.0.id"),
resource.TestCheckResourceAttrSet(resName, "public_gateways.0.name"),
resource.TestCheckResourceAttrSet(resName, "public_gateways.0.zone"),
),
},
},
})
}

func testAccCheckIBMISPublicGatewaysDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_is_public_gateways" "test1"{
}`)
}
1 change: 1 addition & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func Provider() *schema.Provider {
"ibm_is_lb_profiles": dataSourceIBMISLbProfiles(),
"ibm_is_lbs": dataSourceIBMISLBS(),
"ibm_is_public_gateway": dataSourceIBMISPublicGateway(),
"ibm_is_public_gateways": dataSourceIBMISPublicGateways(),
"ibm_is_region": dataSourceIBMISRegion(),
"ibm_is_ssh_key": dataSourceIBMISSSHKey(),
"ibm_is_subnet": dataSourceIBMISSubnet(),
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/is_public_gateways.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
subcategory: "VPC infrastructure"
layout: "ibm"
page_title: "IBM : public_gateways"
description: |-
Manages IBM Public Gateways.
---

# ibm\_is_public_gateways

Import the details of an existing IBM Cloud Infrastructure Public Gateways 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_public_gateways" "testacc_dspgw"{
}
```

## Attribute Reference

The following attributes are exported:
* `public_gateways` - List of all Public Gateways in the IBM Cloud Infrastructure region.
* `id` - The id of the public gateway.
* `status` - The status of the gateway.
* `vpc` - The vpc id of gateway.
* `zone` - The gateway zone name.
* `tags` - Tags associated with the Public gateway.
* `floating_ip` - A nested block describing the floating IP of this gateway.
Nested `floating_ip` blocks have the following structure:
* `id` - ID of the floating ip bound to the public gateway.
* `address` - IP address of the floating ip bound to the public gateway.

0 comments on commit 9967782

Please sign in to comment.