From 99677821063ec86e2cd24353daffbd3dd5145de3 Mon Sep 17 00:00:00 2001 From: Ujjwal Kumar Date: Thu, 25 Mar 2021 06:23:38 +0530 Subject: [PATCH] feature: added support for public gateways datasource --- examples/ibm-is-ng/main.tf | 7 + ibm/data_source_ibm_is_public_gateways.go | 199 ++++++++++++++++++ ...data_source_ibm_is_public_gateways_test.go | 53 +++++ ibm/provider.go | 1 + .../docs/d/is_public_gateways.html.markdown | 35 +++ 5 files changed, 295 insertions(+) create mode 100644 ibm/data_source_ibm_is_public_gateways.go create mode 100644 ibm/data_source_ibm_is_public_gateways_test.go create mode 100644 website/docs/d/is_public_gateways.html.markdown diff --git a/examples/ibm-is-ng/main.tf b/examples/ibm-is-ng/main.tf index 05832f2868c..cd081e190b7 100644 --- a/examples/ibm-is-ng/main.tf +++ b/examples/ibm-is-ng/main.tf @@ -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 } diff --git a/ibm/data_source_ibm_is_public_gateways.go b/ibm/data_source_ibm_is_public_gateways.go new file mode 100644 index 00000000000..6b3442db198 --- /dev/null +++ b/ibm/data_source_ibm_is_public_gateways.go @@ -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() +} diff --git a/ibm/data_source_ibm_is_public_gateways_test.go b/ibm/data_source_ibm_is_public_gateways_test.go new file mode 100644 index 00000000000..6b120289316 --- /dev/null +++ b/ibm/data_source_ibm_is_public_gateways_test.go @@ -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"{ + }`) +} diff --git a/ibm/provider.go b/ibm/provider.go index c085b91b939..7d3c7cb78ad 100644 --- a/ibm/provider.go +++ b/ibm/provider.go @@ -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(), diff --git a/website/docs/d/is_public_gateways.html.markdown b/website/docs/d/is_public_gateways.html.markdown new file mode 100644 index 00000000000..ff8a0ff134d --- /dev/null +++ b/website/docs/d/is_public_gateways.html.markdown @@ -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. \ No newline at end of file