Skip to content

Commit

Permalink
support for ibm directlink and transit gateway resources added (Googl…
Browse files Browse the repository at this point in the history
…eCloudPlatform#1049)

Co-authored-by: Sergey Lanzman <[email protected]>
  • Loading branch information
VaishnaviGopal and sergeylanzman authored Sep 17, 2021
1 parent 96ddb64 commit 7e64e27
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 3 deletions.
10 changes: 7 additions & 3 deletions docs/ibmcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ List of supported IBM Cloud resources:
* `ibm_dns_glb_monitor`
* `ibm_dns_glb_pool`
* `ibm_dns_glb`
* `ibm_transit_gateway`
* `ibm_tg_gateway`
* `ibm_tg_connection`
* `ibm_direct_link`
* `ibm_dl_gateway`
* `ibm_dl_virtual_connection`
* `ibm_dl_provider_gateway`
* `ibm_container_cluster`
* `ibm_container_cluster`
* `ibm_container_worker_pool`
Expand All @@ -131,6 +138,3 @@ List of supported IBM Cloud resources:
* `ibm_vpe_gateway`
* `ibm_is_virtual_endpoint_gateway`
* `ibm_is_virtual_endpoint_gateway_ip`
141 changes: 141 additions & 0 deletions providers/ibm/ibm_dl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ibm

import (
"fmt"
"os"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/IBM/go-sdk-core/v4/core"
dlProviderV2 "github.com/IBM/networking-go-sdk/directlinkproviderv2"
dl "github.com/IBM/networking-go-sdk/directlinkv1"
)

// DLGenerator ...
type DLGenerator struct {
IBMService
}

func (g DLGenerator) createDirectLinkGatewayResources(gatewayID, gatewayName string) terraformutils.Resource {
resources := terraformutils.NewSimpleResource(
gatewayID,
gatewayName,
"ibm_dl_gateway",
"ibm",
[]string{})
return resources
}

func (g DLGenerator) createDirectLinkVirtualConnectionResources(gatewayID, connectionID, connectionName string, dependsOn []string) terraformutils.Resource {
resources := terraformutils.NewResource(
fmt.Sprintf("%s/%s", gatewayID, connectionID),
connectionName,
"ibm_dl_virtual_connection",
"ibm",
map[string]string{},
[]string{},
map[string]interface{}{
"depends_on": dependsOn,
})
return resources
}

func (g DLGenerator) createDirectLinkProviderGatewayResources(providerGatewayID, providerGatewayName string) terraformutils.Resource {
resources := terraformutils.NewSimpleResource(
providerGatewayID,
providerGatewayName,
"ibm_dl_provider_gateway",
"ibm",
[]string{})
return resources
}

// InitResources ...
func (g *DLGenerator) InitResources() error {
apiKey := os.Getenv("IC_API_KEY")
if apiKey == "" {
return fmt.Errorf("no API key set")
}
dlURL := "https://directlink.cloud.ibm.com/v1"
directlinkOptions := &dl.DirectLinkV1Options{
URL: envFallBack([]string{"IBMCLOUD_DL_API_ENDPOINT"}, dlURL),
Authenticator: &core.IamAuthenticator{
ApiKey: apiKey,
},
Version: CreateVersionDate(),
}
dlclient, err := dl.NewDirectLinkV1(directlinkOptions)
if err != nil {
return err
}

listGatewaysOptions := &dl.ListGatewaysOptions{}
gateways, response, err := dlclient.ListGateways(listGatewaysOptions)
if err != nil {
return fmt.Errorf("Error Fetching Direct Link Gateways %s\n%s", err, response)
}
for _, gateway := range gateways.Gateways {
g.Resources = append(g.Resources, g.createDirectLinkGatewayResources(*gateway.ID, *gateway.Name))
var dependsOn []string
dependsOn = append(dependsOn,
"ibm_dl_gateway."+terraformutils.TfSanitize(*gateway.Name))
listGatewayVirtualConnectionsOptions := &dl.ListGatewayVirtualConnectionsOptions{
GatewayID: gateway.ID,
}
connections, response, err := dlclient.ListGatewayVirtualConnections(listGatewayVirtualConnectionsOptions)
if err != nil {
return fmt.Errorf("Error Fetching Direct Link Virtual connections %s\n%s", err, response)
}
for _, connection := range connections.VirtualConnections {
g.Resources = append(g.Resources, g.createDirectLinkVirtualConnectionResources(*gateway.ID, *connection.ID, *connection.Name, dependsOn))
}
}

dlproviderURL := "https://directlink.cloud.ibm.com/provider/v2"
dlproviderOptions := &dlProviderV2.DirectLinkProviderV2Options{
URL: envFallBack([]string{"IBMCLOUD_DL_PROVIDER_API_ENDPOINT"}, dlproviderURL),
Authenticator: &core.IamAuthenticator{
ApiKey: apiKey,
},
Version: CreateVersionDate(),
}
dlproviderclient, err := dlProviderV2.NewDirectLinkProviderV2(dlproviderOptions)
if err != nil {
return err
}
start := ""
allrecs := []dlProviderV2.ProviderGateway{}
for {
listProviderGatewaysOptions := &dlProviderV2.ListProviderGatewaysOptions{}
if start != "" {
listProviderGatewaysOptions.Start = &start
}

providerGateways, resp, err := dlproviderclient.ListProviderGateways(listProviderGatewaysOptions)
if err != nil {
return fmt.Errorf("Error Listing Direct Link Provider Gateways %s\n%s", err, resp)
}
start = GetNext(providerGateways.Next)
allrecs = append(allrecs, providerGateways.Gateways...)
if start == "" {
break
}
}
for _, providerGateway := range allrecs {
g.Resources = append(g.Resources, g.createDirectLinkProviderGatewayResources(*providerGateway.ID, *providerGateway.Name))
}
return nil
}
2 changes: 2 additions & 0 deletions providers/ibm/ibm_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (p *IBMProvider) GetSupportedService() map[string]terraformutils.ServiceGen
"ibm_function": &CloudFunctionGenerator{},
"ibm_private_dns": &privateDNSTemplateGenerator{},
"ibm_certificate_manager": &CMGenerator{},
"ibm_direct_link": &DLGenerator{},
"ibm_transit_gateway": &TGGenerator{},
"ibm_vpe_gateway": &VPEGenerator{},
}
}
Expand Down
116 changes: 116 additions & 0 deletions providers/ibm/ibm_tg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ibm

import (
"fmt"
"os"
"time"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/IBM/go-sdk-core/v4/core"
tg "github.com/IBM/networking-go-sdk/transitgatewayapisv1"
)

// TGGenerator ...
type TGGenerator struct {
IBMService
}

func (g TGGenerator) createTransitGatewayResources(gatewayID, gatewayName string) terraformutils.Resource {
resources := terraformutils.NewSimpleResource(
gatewayID,
gatewayName,
"ibm_tg_gateway",
"ibm",
[]string{})
return resources
}

func (g TGGenerator) createTransitGatewayConnectionResources(gatewayID, connectionID, connectionName string, dependsOn []string) terraformutils.Resource {
resources := terraformutils.NewResource(
fmt.Sprintf("%s/%s", gatewayID, connectionID),
connectionName,
"ibm_tg_connection",
"ibm",
map[string]string{},
[]string{},
map[string]interface{}{
"depends_on": dependsOn,
})
return resources
}

// CreateVersionDate requires mandatory version attribute. Any date from 2019-12-13 up to the currentdate may be provided. Specify the current date to request the latest version.
func CreateVersionDate() *string {
version := time.Now().Format("2006-01-02")
return &version
}

// InitResources ...
func (g *TGGenerator) InitResources() error {
apiKey := os.Getenv("IC_API_KEY")
if apiKey == "" {
return fmt.Errorf("no API key set")
}
tgURL := "https://transit.cloud.ibm.com/v1"
transitgatewayOptions := &tg.TransitGatewayApisV1Options{
URL: envFallBack([]string{"IBMCLOUD_TG_API_ENDPOINT"}, tgURL),
Authenticator: &core.IamAuthenticator{
ApiKey: apiKey,
},
Version: CreateVersionDate(),
}

tgclient, err := tg.NewTransitGatewayApisV1(transitgatewayOptions)
if err != nil {
return err
}
start := ""
allrecs := []tg.TransitGateway{}
for {
listTransitGatewaysOptions := &tg.ListTransitGatewaysOptions{}
if start != "" {
listTransitGatewaysOptions.Start = &start
}

gateways, resp, err := tgclient.ListTransitGateways(listTransitGatewaysOptions)
if err != nil {
return fmt.Errorf("Error Listing Transit Gateways %s\n%s", err, resp)
}
start = GetNext(gateways.Next)
allrecs = append(allrecs, gateways.TransitGateways...)
if start == "" {
break
}
}
for _, gateway := range allrecs {
g.Resources = append(g.Resources, g.createTransitGatewayResources(*gateway.ID, *gateway.Name))
var dependsOn []string
dependsOn = append(dependsOn,
"ibm_tg_gateway."+terraformutils.TfSanitize(*gateway.Name))
listTransitGatewayConnectionsOptions := &tg.ListTransitGatewayConnectionsOptions{
TransitGatewayID: gateway.ID,
}
connections, response, err := tgclient.ListTransitGatewayConnections(listTransitGatewayConnectionsOptions)
if err != nil {
return fmt.Errorf("Error Listing Transit Gateway connections %s\n%s", err, response)
}
for _, connection := range connections.Connections {
g.Resources = append(g.Resources, g.createTransitGatewayConnectionResources(*gateway.ID, *connection.ID, *connection.Name, dependsOn))
}
}
return nil
}

0 comments on commit 7e64e27

Please sign in to comment.