github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_virtual_endpoint_gateway.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  	"github.com/IBM/vpc-go-sdk/vpcv1"
    24  )
    25  
    26  // VPEGenerator ...
    27  type VPEGenerator struct {
    28  	IBMService
    29  }
    30  
    31  func (g VPEGenerator) createVPEGatewayResources(gatewayID, gatewayName string) terraformutils.Resource {
    32  	resources := terraformutils.NewSimpleResource(
    33  		gatewayID,
    34  		normalizeResourceName(gatewayName, false),
    35  		"ibm_is_virtual_endpoint_gateway",
    36  		"ibm",
    37  		[]string{})
    38  	return resources
    39  }
    40  
    41  func (g VPEGenerator) createVPEGatewayIPResources(gatewayID, gatewayIPID, gatewayIPName string, dependsOn []string) terraformutils.Resource {
    42  	resources := terraformutils.NewResource(
    43  		fmt.Sprintf("%s/%s", gatewayID, gatewayIPID),
    44  		normalizeResourceName(gatewayIPName, false),
    45  		"ibm_is_virtual_endpoint_gateway_ip",
    46  		"ibm",
    47  		map[string]string{},
    48  		[]string{},
    49  		map[string]interface{}{
    50  			"depends_on": dependsOn,
    51  		})
    52  	return resources
    53  }
    54  
    55  // InitResources ...
    56  func (g *VPEGenerator) InitResources() error {
    57  	region := g.Args["region"].(string)
    58  	apiKey := os.Getenv("IC_API_KEY")
    59  	if apiKey == "" {
    60  		return fmt.Errorf("No API key set")
    61  	}
    62  
    63  	vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region)
    64  	vpcoptions := &vpcv1.VpcV1Options{
    65  		URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl),
    66  		Authenticator: &core.IamAuthenticator{
    67  			ApiKey: apiKey,
    68  		},
    69  	}
    70  	vpcclient, err := vpcv1.NewVpcV1(vpcoptions)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	start := ""
    76  	allrecs := []vpcv1.EndpointGateway{}
    77  	for {
    78  		listEndpointGatewaysOptions := &vpcv1.ListEndpointGatewaysOptions{}
    79  		if start != "" {
    80  			listEndpointGatewaysOptions.Start = &start
    81  		}
    82  		if rg := g.Args["resource_group"].(string); rg != "" {
    83  			rg, err = GetResourceGroupID(apiKey, rg, region)
    84  			if err != nil {
    85  				return fmt.Errorf("Error Fetching Resource Group Id %s", err)
    86  			}
    87  			listEndpointGatewaysOptions.ResourceGroupID = &rg
    88  		}
    89  		gateways, response, err := vpcclient.ListEndpointGateways(listEndpointGatewaysOptions)
    90  		if err != nil {
    91  			return fmt.Errorf("Error Fetching endpoint gateways %s\n%s", err, response)
    92  		}
    93  		start = GetNext(gateways.Next)
    94  		allrecs = append(allrecs, gateways.EndpointGateways...)
    95  		if start == "" {
    96  			break
    97  		}
    98  	}
    99  
   100  	for _, gateway := range allrecs {
   101  		var dependsOn []string
   102  		start := ""
   103  		allrecs := []vpcv1.ReservedIP{}
   104  		dependsOn = append(dependsOn,
   105  			"ibm_is_virtual_endpoint_gateway."+terraformutils.TfSanitize(*gateway.Name))
   106  		g.Resources = append(g.Resources, g.createVPEGatewayResources(*gateway.ID, *gateway.Name))
   107  		listEndpointGatewayIpsOptions := &vpcv1.ListEndpointGatewayIpsOptions{
   108  			EndpointGatewayID: gateway.ID,
   109  		}
   110  		if start != "" {
   111  			listEndpointGatewayIpsOptions.Start = &start
   112  		}
   113  		ips, response, err := vpcclient.ListEndpointGatewayIps(listEndpointGatewayIpsOptions)
   114  		if err != nil {
   115  			return fmt.Errorf("Error Fetching endpoint gateway ips %s\n%s", err, response)
   116  		}
   117  		start = GetNext(ips.Next)
   118  		allrecs = append(allrecs, ips.Ips...)
   119  		if start == "" {
   120  			break
   121  		}
   122  		for _, ip := range allrecs {
   123  			g.Resources = append(g.Resources, g.createVPEGatewayIPResources(*gateway.ID, *ip.ID, *ip.Name, dependsOn))
   124  		}
   125  	}
   126  	return nil
   127  }