github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_vpn_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  // VPNGatewayGenerator ...
    27  type VPNGatewayGenerator struct {
    28  	IBMService
    29  }
    30  
    31  func (g VPNGatewayGenerator) createVPNGatewayResources(vpngwID, vpngwName string) terraformutils.Resource {
    32  	resources := terraformutils.NewSimpleResource(
    33  		vpngwID,
    34  		normalizeResourceName(vpngwName, false),
    35  		"ibm_is_vpn_gateway",
    36  		"ibm",
    37  		[]string{})
    38  	return resources
    39  }
    40  
    41  func (g VPNGatewayGenerator) createVPNGatewayConnectionResources(vpngwID, vpngwConnectionID, vpngwConnectionName string, dependsOn []string) terraformutils.Resource {
    42  	resources := terraformutils.NewResource(
    43  		fmt.Sprintf("%s/%s", vpngwID, vpngwConnectionID),
    44  		normalizeResourceName(vpngwConnectionName, false),
    45  		"ibm_is_vpn_gateway_connections",
    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 *VPNGatewayGenerator) 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  	start := ""
    75  	var allrecs []vpcv1.VPNGatewayIntf
    76  	for {
    77  		listVPNGatewaysOptions := &vpcv1.ListVPNGatewaysOptions{}
    78  		if start != "" {
    79  			listVPNGatewaysOptions.Start = &start
    80  		}
    81  		if rg := g.Args["resource_group"].(string); rg != "" {
    82  			rg, err = GetResourceGroupID(apiKey, rg, region)
    83  			if err != nil {
    84  				return fmt.Errorf("Error Fetching Resource Group Id %s", err)
    85  			}
    86  			listVPNGatewaysOptions.ResourceGroupID = &rg
    87  		}
    88  		vpngws, response, err := vpcclient.ListVPNGateways(listVPNGatewaysOptions)
    89  		if err != nil {
    90  			return fmt.Errorf("Error Fetching VPN Gateways %s\n%s", err, response)
    91  		}
    92  		start = GetNext(vpngws.Next)
    93  		allrecs = append(allrecs, vpngws.VPNGateways...)
    94  		if start == "" {
    95  			break
    96  		}
    97  	}
    98  
    99  	for _, gw := range allrecs {
   100  		vpngw := gw.(*vpcv1.VPNGateway)
   101  		var dependsOn []string
   102  
   103  		g.Resources = append(g.Resources, g.createVPNGatewayResources(*vpngw.ID, *vpngw.Name))
   104  		resourceName := g.Resources[len(g.Resources)-1:][0].ResourceName
   105  		dependsOn = append(dependsOn,
   106  			"ibm_is_vpn_gateway."+resourceName)
   107  		listVPNGatewayConnectionsOptions := &vpcv1.ListVPNGatewayConnectionsOptions{
   108  			VPNGatewayID: vpngw.ID,
   109  		}
   110  		vpngwConnections, response, err := vpcclient.ListVPNGatewayConnections(listVPNGatewayConnectionsOptions)
   111  		if err != nil {
   112  			return fmt.Errorf("Error Fetching VPN Gateway Connections %s\n%s", err, response)
   113  		}
   114  		for _, connection := range vpngwConnections.Connections {
   115  			vpngwConnection := connection.(*vpcv1.VPNGatewayConnection)
   116  			g.Resources = append(g.Resources, g.createVPNGatewayConnectionResources(*vpngw.ID, *vpngwConnection.ID, *vpngwConnection.Name, dependsOn))
   117  		}
   118  	}
   119  	return nil
   120  }