forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: added support for public gateways datasource
- Loading branch information
Showing
5 changed files
with
295 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,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() | ||
} |
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,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"{ | ||
}`) | ||
} |
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,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. |