Skip to content

Commit

Permalink
Added directlink provider datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinky Bhargava authored and hkantare committed Dec 30, 2020
1 parent f7d9bb3 commit 861a6ef
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 10 deletions.
27 changes: 25 additions & 2 deletions examples/ibm-direct-link-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ Run `terraform destroy` when you don't need these resources.

## Direct Link Provider Resources

Direct Link Connect gateway resource :

resource ibm_dl_provider_gateway test_dl_gateway {
bgp_asn = var.bgp_asn
bgp_ibm_cidr = var.bgp_ibm_cidr
bgp_cer_cidr = var.bgp_cer_cidr
name = var.name
speed_mbps = var.speed_mbps
port = data.ibm_dl_provider_ports.test_ds_dl_ports.ports[0].port_id
customer_account_id = var.customerAccID
}


## Direct Link Provider Data Sources
Expand All @@ -35,7 +46,11 @@ List ports :
data "ibm_dl_provider_ports" "test_ds_dl_ports" {
}
```

List all Direct Link Connect gateways created by this provider :
```hcl
data "ibm_dl_provider_gateways" "test_ibm_dl_provider_gws"{
}
```

## Examples

Expand All @@ -54,7 +69,15 @@ data "ibm_dl_provider_ports" "test_ds_dl_ports" {
|------|---------|
| ibm | n/a |

## Inputs

## Inputs

| Name | Description | Type | Required |
|------|-------------|------|---------|
| bgp\_asn | The BGP ASN of the Gateway to be created. | `string` | yes |
| name | The unique user-defined name for this gateway. | `string` | yes |
| speed\_mbps | Gateway speed in megabits per second. | `integer` | yes |
| bgp\_cer_cidr | BGP customer edge router CIDR. | `string` | no |
| bgp\_ibm_cidr | BGP IBM CIDR. | `string` | no |
| customerAccID | Customer IBM Cloud account ID for the new gateway. A gateway object containing the pending create request will become available in the specified account. | `string` | yes |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
3 changes: 3 additions & 0 deletions examples/ibm-direct-link-provider/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ resource ibm_dl_provider_gateway test_dl_gateway {
port = data.ibm_dl_provider_ports.test_ds_dl_ports.ports[0].port_id
customer_account_id = var.customerAccID
}
data "ibm_dl_provider_gateways" "test_ibm_dl_provider_gws" {

}
215 changes: 215 additions & 0 deletions ibm/data_source_ibm_dl_provider_gateways.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package ibm

import (
dlProviderV2 "github.com/IBM/networking-go-sdk/directlinkproviderv2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"time"
)

const (
dlProviderGateways = "gateways"
dlProviderGatewaysID = "id"
)

func dataSourceIBMDirectLinkProviderGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMDirectLinkProviderGatewaysRead,

Schema: map[string]*schema.Schema{

dlProviderGateways: {

Type: schema.TypeList,
Description: "Collection of direct link provider ports",
Computed: true,
Elem: &schema.Resource{

Schema: map[string]*schema.Schema{
dlProviderGatewaysID: {
Type: schema.TypeString,
Computed: true,
Description: "Id of the data source gateways",
},
dlBgpAsn: {
Type: schema.TypeInt,
Computed: true,
Description: "BGP ASN",
},

dlBgpCerCidr: {
Type: schema.TypeString,
Computed: true,
Description: "BGP customer edge router CIDR",
},
dlBgpIbmAsn: {
Type: schema.TypeInt,
Computed: true,
Description: "IBM BGP ASN",
},
dlBgpIbmCidr: {
Type: schema.TypeString,
Computed: true,
Description: "BGP IBM CIDR",
},
dlBgpStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Gateway BGP status",
},
customerAccountID: {
Type: schema.TypeString,
Computed: true,
Description: "Customer IBM Cloud account ID for the new gateway. A gateway object containing the pending create request will become available in the specified account.",
},
dlCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time resource was created",
},
dlCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN (Cloud Resource Name) of this gateway",
},

dlName: {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this gateway",
},
dlOperationalStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Gateway operational status",
},
dlChangeRequest: {
Type: schema.TypeString,
Computed: true,
Description: "Changes pending approval for provider managed Direct Link gateways",
},
dlPort: {
Type: schema.TypeString,
Computed: true,
Description: "Gateway port",
},
dlProviderAPIManaged: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether gateway was created through a provider portal",
},
dlResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Gateway resource group",
},
dlSpeedMbps: {
Type: schema.TypeInt,
Computed: true,
Description: "Gateway speed in megabits per second",
},
dlType: {
Type: schema.TypeString,
Computed: true,
Description: "Gateway type",
},
dlVlan: {
Type: schema.TypeInt,
Computed: true,
Description: "VLAN allocated for this gateway",
},
},
},
},
},
}
}

func dataSourceIBMDirectLinkProviderGatewaysRead(d *schema.ResourceData, meta interface{}) error {
directLinkProvider, err := directlinkProviderClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []dlProviderV2.ProviderGateway{}
for {
listProviderGatewaysOptions := directLinkProvider.NewListProviderGatewaysOptions()
if start != "" {
listProviderGatewaysOptions.Start = &start
}

providerGateways, resp, err := directLinkProvider.ListProviderGateways(listProviderGatewaysOptions)
if err != nil {
log.Println("[WARN] Error listing dl provider gateways", providerGateways, resp, err)
return err
}
start = GetNext(providerGateways.Next)
allrecs = append(allrecs, providerGateways.Gateways...)
if start == "" {
break
}
}
gatewayCollections := make([]map[string]interface{}, 0)
for _, instance := range allrecs {
gatewayCollection := map[string]interface{}{}

if instance.ID != nil {
gatewayCollection[dlProviderGatewaysID] = *instance.ID
}
if instance.Name != nil {
gatewayCollection[dlName] = *instance.Name
}
if instance.Crn != nil {
gatewayCollection[dlCrn] = *instance.Crn
}
if instance.BgpAsn != nil {
gatewayCollection[dlBgpAsn] = *instance.BgpAsn
}
if instance.BgpIbmCidr != nil {
gatewayCollection[dlBgpIbmCidr] = *instance.BgpIbmCidr
}
if instance.BgpIbmAsn != nil {
gatewayCollection[dlBgpIbmAsn] = *instance.BgpIbmAsn
}

if instance.BgpCerCidr != nil {
gatewayCollection[dlBgpCerCidr] = *instance.BgpCerCidr
}

if instance.ProviderApiManaged != nil {
gatewayCollection[dlProviderAPIManaged] = *instance.ProviderApiManaged
}
if instance.Type != nil {
gatewayCollection[dlType] = *instance.Type
}
if instance.SpeedMbps != nil {
gatewayCollection[dlSpeedMbps] = *instance.SpeedMbps
}
if instance.OperationalStatus != nil {
gatewayCollection[dlOperationalStatus] = *instance.OperationalStatus
}
if instance.BgpStatus != nil {
gatewayCollection[dlBgpStatus] = *instance.BgpStatus
}
if instance.Vlan != nil {
gatewayCollection[dlVlan] = *instance.Vlan
}

if instance.Port != nil {
gatewayCollection[dlPort] = *instance.Port.ID
}

if instance.CreatedAt != nil {
gatewayCollection[dlCreatedAt] = instance.CreatedAt.String()
}

gatewayCollections = append(gatewayCollections, gatewayCollection)
}
d.SetId(dataSourceIBMDirectLinkProviderGatewaysReadID(d))
d.Set(dlProviderGateways, gatewayCollections)
return nil
}

func dataSourceIBMDirectLinkProviderGatewaysReadID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
32 changes: 32 additions & 0 deletions ibm/data_source_ibm_dl_provider_gateways_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ibm

import (
"fmt"
"testing"

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

func TestAccIBMDLProviderGWsDataSource_basic(t *testing.T) {
name := "dl_provider_gws"
resName := "data.ibm_dl_provider_gateways.test_dl_provider_gws"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMDLProviderGWsDataSourceConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resName, "id"),
),
},
},
})
}

func testAccCheckIBMDLProviderGWsDataSourceConfig(name string) string {
return fmt.Sprintf(`
data "ibm_dl_provider_gateways" "test_%s" {
}
`, name)
}
17 changes: 9 additions & 8 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,15 @@ func Provider() terraform.ResourceProvider {

// Added for Direct Link

"ibm_dl_gateways": dataSourceIBMDLGateways(),
"ibm_dl_offering_speeds": dataSourceIBMDLOfferingSpeeds(),
"ibm_dl_port": dataSourceIBMDirectLinkPort(),
"ibm_dl_ports": dataSourceIBMDirectLinkPorts(),
"ibm_dl_gateway": dataSourceIBMDLGateway(),
"ibm_dl_locations": dataSourceIBMDLLocations(),
"ibm_dl_routers": dataSourceIBMDLRouters(),
"ibm_dl_provider_ports": dataSourceIBMDirectLinkProviderPorts(),
"ibm_dl_gateways": dataSourceIBMDLGateways(),
"ibm_dl_offering_speeds": dataSourceIBMDLOfferingSpeeds(),
"ibm_dl_port": dataSourceIBMDirectLinkPort(),
"ibm_dl_ports": dataSourceIBMDirectLinkPorts(),
"ibm_dl_gateway": dataSourceIBMDLGateway(),
"ibm_dl_locations": dataSourceIBMDLLocations(),
"ibm_dl_routers": dataSourceIBMDLRouters(),
"ibm_dl_provider_ports": dataSourceIBMDirectLinkProviderPorts(),
"ibm_dl_provider_gateways": dataSourceIBMDirectLinkProviderGateways(),

//Added for Transit Gateway
"ibm_tg_gateway": dataSourceIBMTransitGateway(),
Expand Down
43 changes: 43 additions & 0 deletions website/docs/d/dl_provider_gateways.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: "ibm"
page_title: "IBM : dl_provider_gateways"
sidebar_current: "docs-ibm-datasource-dl-provider-gateways"
description: |-
Manages IBM Cloud Infrastructure Direct Link Provider Gateway.
---

# ibm\_dl_provider_gateways

Import the details of an existing IBM Cloud Infrastructure direct link provider gateway 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_dl_provider_gateways" "ds_dlproviderGateways" {
}
```

## Attribute Reference

The following attributes are exported:

* `gateways` - List of all Direct Link provider gateways in the IBM Cloud Infrastructure.
* `bgp_asn` - Customer BGP ASN.
* `created_at` - The date and time resource was created.
* `crn` - The CRN (Cloud Resource Name) of this gateway.
* `global` - Gateways with global routing (true) can connect to networks outside their associated region.
* `id` - The unique identifier of this gateway.
* `name` - The unique user-defined name for this gateway.
* `operational_status` - Gateway operational status.
* `resource_group` - Resource group identifier.
* `speed_mbps` - Gateway speed in megabits per second.
* `type` - Gateway type.
* `bgp_cer_cidr` - BGP customer edge router CIDR.
* `bgp_ibm_asn` - IBM BGP ASN.
* `bgp_ibm_cidr` - BGP IBM CIDR.
* `bgp_status` - Gateway BGP status.
* `port` - Port Identifier.
* `provider_api_managed` - Indicates whether gateway was created through a provider portal. If true, gateway can only be changed or deleted through the corresponding provider portal.
* `vlan` - VLAN allocated for this gateway. Only set for type=connect gateways created directly through the IBM portal.

3 changes: 3 additions & 0 deletions website/ibm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@
<li<%= sidebar_current("docs-ibm-datasource-dl-provider-ports") %>>
<a href="/docs/providers/ibm/d/dl_provider_ports.html">dl_provider_ports</a>
</li>
<li<%= sidebar_current("docs-ibm-datasource-ibm-dl-provider-gateways") %>>
<a href="/docs/providers/ibm/d/dl_provider_gateways.html">dl_provider_gateways</a>
</li>
</ul>
</li>
<li<%= sidebar_current("docs-ibm-datasource-dns") %>>
Expand Down

0 comments on commit 861a6ef

Please sign in to comment.