From 7e64e27142b4069d4980198e9450ed8cb971d0f5 Mon Sep 17 00:00:00 2001 From: VaishnaviGopal Date: Fri, 17 Sep 2021 23:48:28 +0530 Subject: [PATCH] support for ibm directlink and transit gateway resources added (#1049) Co-authored-by: Sergey Lanzman --- docs/ibmcloud.md | 10 ++- providers/ibm/ibm_dl.go | 141 ++++++++++++++++++++++++++++++++++ providers/ibm/ibm_provider.go | 2 + providers/ibm/ibm_tg.go | 116 ++++++++++++++++++++++++++++ 4 files changed, 266 insertions(+), 3 deletions(-) create mode 100644 providers/ibm/ibm_dl.go create mode 100644 providers/ibm/ibm_tg.go diff --git a/docs/ibmcloud.md b/docs/ibmcloud.md index d7bbff54b4..1e516c1027 100644 --- a/docs/ibmcloud.md +++ b/docs/ibmcloud.md @@ -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` @@ -131,6 +138,3 @@ List of supported IBM Cloud resources: * `ibm_vpe_gateway` * `ibm_is_virtual_endpoint_gateway` * `ibm_is_virtual_endpoint_gateway_ip` - - - diff --git a/providers/ibm/ibm_dl.go b/providers/ibm/ibm_dl.go new file mode 100644 index 0000000000..fd4d8c0765 --- /dev/null +++ b/providers/ibm/ibm_dl.go @@ -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 +} diff --git a/providers/ibm/ibm_provider.go b/providers/ibm/ibm_provider.go index daf0026d84..e4560068ea 100644 --- a/providers/ibm/ibm_provider.go +++ b/providers/ibm/ibm_provider.go @@ -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{}, } } diff --git a/providers/ibm/ibm_tg.go b/providers/ibm/ibm_tg.go new file mode 100644 index 0000000000..e0d4977036 --- /dev/null +++ b/providers/ibm/ibm_tg.go @@ -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 +}