github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_dl.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  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/IBM/go-sdk-core/v4/core"
    23  	dlProviderV2 "github.com/IBM/networking-go-sdk/directlinkproviderv2"
    24  	dl "github.com/IBM/networking-go-sdk/directlinkv1"
    25  )
    26  
    27  // DLGenerator ...
    28  type DLGenerator struct {
    29  	IBMService
    30  }
    31  
    32  func (g DLGenerator) createDirectLinkGatewayResources(gatewayID, gatewayName string) terraformutils.Resource {
    33  	resource := terraformutils.NewSimpleResource(
    34  		gatewayID,
    35  		normalizeResourceName(gatewayName, false),
    36  		"ibm_dl_gateway",
    37  		"ibm",
    38  		[]string{})
    39  	return resource
    40  }
    41  
    42  func (g DLGenerator) createDirectLinkVirtualConnectionResources(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_dl_virtual_connection",
    47  		"ibm",
    48  		map[string]string{},
    49  		[]string{},
    50  		map[string]interface{}{
    51  			"depends_on": dependsOn,
    52  		})
    53  	return resource
    54  }
    55  
    56  func (g DLGenerator) createDirectLinkProviderGatewayResources(providerGatewayID, providerGatewayName string) terraformutils.Resource {
    57  	resource := terraformutils.NewSimpleResource(
    58  		providerGatewayID,
    59  		normalizeResourceName(providerGatewayName, false),
    60  		"ibm_dl_provider_gateway",
    61  		"ibm",
    62  		[]string{})
    63  	return resource
    64  }
    65  
    66  // InitResources ...
    67  func (g *DLGenerator) InitResources() error {
    68  	apiKey := os.Getenv("IC_API_KEY")
    69  	if apiKey == "" {
    70  		return fmt.Errorf("no API key set")
    71  	}
    72  	dlURL := "https://directlink.cloud.ibm.com/v1"
    73  	directlinkOptions := &dl.DirectLinkV1Options{
    74  		URL: envFallBack([]string{"IBMCLOUD_DL_API_ENDPOINT"}, dlURL),
    75  		Authenticator: &core.IamAuthenticator{
    76  			ApiKey: apiKey,
    77  		},
    78  		Version: CreateVersionDate(),
    79  	}
    80  	dlclient, err := dl.NewDirectLinkV1(directlinkOptions)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	listGatewaysOptions := &dl.ListGatewaysOptions{}
    86  	gateways, response, err := dlclient.ListGateways(listGatewaysOptions)
    87  	if err != nil {
    88  		return fmt.Errorf("Error Fetching Direct Link Gateways %s\n%s", err, response)
    89  	}
    90  	if gateways.Gateways != nil {
    91  		for _, gateway := range gateways.Gateways {
    92  			g.Resources = append(g.Resources, g.createDirectLinkGatewayResources(*gateway.ID, *gateway.Name))
    93  			resourceName := g.Resources[len(g.Resources)-1:][0].ResourceName
    94  			var dependsOn []string
    95  			dependsOn = append(dependsOn, "ibm_dl_gateway."+resourceName)
    96  			listGatewayVirtualConnectionsOptions := &dl.ListGatewayVirtualConnectionsOptions{
    97  				GatewayID: gateway.ID,
    98  			}
    99  			connections, response, err := dlclient.ListGatewayVirtualConnections(listGatewayVirtualConnectionsOptions)
   100  			if err != nil {
   101  				return fmt.Errorf("Error Fetching Direct Link Virtual connections %s\n%s", err, response)
   102  			}
   103  			for _, connection := range connections.VirtualConnections {
   104  				g.Resources = append(g.Resources, g.createDirectLinkVirtualConnectionResources(*gateway.ID, *connection.ID, *connection.Name, dependsOn))
   105  			}
   106  		}
   107  	}
   108  
   109  	dlproviderURL := "https://directlink.cloud.ibm.com/provider/v2"
   110  	dlproviderOptions := &dlProviderV2.DirectLinkProviderV2Options{
   111  		URL: envFallBack([]string{"IBMCLOUD_DL_PROVIDER_API_ENDPOINT"}, dlproviderURL),
   112  		Authenticator: &core.IamAuthenticator{
   113  			ApiKey: apiKey,
   114  		},
   115  		Version: CreateVersionDate(),
   116  	}
   117  	dlproviderclient, err := dlProviderV2.NewDirectLinkProviderV2(dlproviderOptions)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	start := ""
   122  	allrecs := []dlProviderV2.ProviderGateway{}
   123  	for {
   124  		listProviderGatewaysOptions := &dlProviderV2.ListProviderGatewaysOptions{}
   125  		if start != "" {
   126  			listProviderGatewaysOptions.Start = &start
   127  		}
   128  
   129  		providerGateways, resp, err := dlproviderclient.ListProviderGateways(listProviderGatewaysOptions)
   130  		if err != nil {
   131  			return fmt.Errorf("Error Listing Direct Link Provider Gateways %s\n%s", err, resp)
   132  		}
   133  		start = GetNext(providerGateways.Next)
   134  		allrecs = append(allrecs, providerGateways.Gateways...)
   135  		if start == "" {
   136  			break
   137  		}
   138  	}
   139  	for _, providerGateway := range allrecs {
   140  		g.Resources = append(g.Resources, g.createDirectLinkProviderGatewayResources(*providerGateway.ID, *providerGateway.Name))
   141  	}
   142  	return nil
   143  }