github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_tg.go (about)

     1  // Copyright 2019 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ibm
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"time"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  	"github.com/IBM/go-sdk-core/v4/core"
    24  	tg "github.com/IBM/networking-go-sdk/transitgatewayapisv1"
    25  )
    26  
    27  // TGGenerator ...
    28  type TGGenerator struct {
    29  	IBMService
    30  }
    31  
    32  func (g TGGenerator) createTransitGatewayResources(gatewayID, gatewayName string) terraformutils.Resource {
    33  	resource := terraformutils.NewSimpleResource(
    34  		gatewayID,
    35  		normalizeResourceName(gatewayName, false),
    36  		"ibm_tg_gateway",
    37  		"ibm",
    38  		[]string{})
    39  	return resource
    40  }
    41  
    42  func (g TGGenerator) createTransitGatewayConnectionResources(gatewayID, connectionID, connectionName string, dependsOn []string) terraformutils.Resource {
    43  	resource := terraformutils.NewResource(
    44  		fmt.Sprintf("%s/%s", gatewayID, connectionID),
    45  		normalizeResourceName(connectionName, false),
    46  		"ibm_tg_connection",
    47  		"ibm",
    48  		map[string]string{},
    49  		[]string{},
    50  		map[string]interface{}{
    51  			"depends_on": dependsOn,
    52  		})
    53  	return resource
    54  }
    55  
    56  // 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.
    57  func CreateVersionDate() *string {
    58  	version := time.Now().Format("2006-01-02")
    59  	return &version
    60  }
    61  
    62  // InitResources ...
    63  func (g *TGGenerator) InitResources() error {
    64  	apiKey := os.Getenv("IC_API_KEY")
    65  	if apiKey == "" {
    66  		return fmt.Errorf("no API key set")
    67  	}
    68  	tgURL := "https://transit.cloud.ibm.com/v1"
    69  	transitgatewayOptions := &tg.TransitGatewayApisV1Options{
    70  		URL: envFallBack([]string{"IBMCLOUD_TG_API_ENDPOINT"}, tgURL),
    71  		Authenticator: &core.IamAuthenticator{
    72  			ApiKey: apiKey,
    73  		},
    74  		Version: CreateVersionDate(),
    75  	}
    76  
    77  	tgclient, err := tg.NewTransitGatewayApisV1(transitgatewayOptions)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	start := ""
    82  	allrecs := []tg.TransitGateway{}
    83  	for {
    84  		listTransitGatewaysOptions := &tg.ListTransitGatewaysOptions{}
    85  		if start != "" {
    86  			listTransitGatewaysOptions.Start = &start
    87  		}
    88  
    89  		gateways, resp, err := tgclient.ListTransitGateways(listTransitGatewaysOptions)
    90  		if err != nil {
    91  			return fmt.Errorf("Error Listing Transit Gateways %s\n%s", err, resp)
    92  		}
    93  		start = GetNext(gateways.Next)
    94  		allrecs = append(allrecs, gateways.TransitGateways...)
    95  		if start == "" {
    96  			break
    97  		}
    98  	}
    99  	for _, gateway := range allrecs {
   100  		g.Resources = append(g.Resources, g.createTransitGatewayResources(*gateway.ID, *gateway.Name))
   101  		resourceName := g.Resources[len(g.Resources)-1:][0].ResourceName
   102  		var dependsOn []string
   103  		dependsOn = append(dependsOn,
   104  			"ibm_tg_gateway."+resourceName)
   105  		listTransitGatewayConnectionsOptions := &tg.ListTransitGatewayConnectionsOptions{
   106  			TransitGatewayID: gateway.ID,
   107  		}
   108  		connections, response, err := tgclient.ListTransitGatewayConnections(listTransitGatewayConnectionsOptions)
   109  		if err != nil {
   110  			return fmt.Errorf("Error Listing Transit Gateway connections %s\n%s", err, response)
   111  		}
   112  		for _, connection := range connections.Connections {
   113  			g.Resources = append(g.Resources, g.createTransitGatewayConnectionResources(*gateway.ID, *connection.ID, *connection.Name, dependsOn))
   114  		}
   115  	}
   116  	return nil
   117  }