github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_public_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  	"log"
    20  	"os"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  	"github.com/IBM/go-sdk-core/v4/core"
    24  	"github.com/IBM/vpc-go-sdk/vpcv1"
    25  )
    26  
    27  // PublicGatewayGenerator ...
    28  type PublicGatewayGenerator struct {
    29  	IBMService
    30  }
    31  
    32  func (g PublicGatewayGenerator) createPublicGatewayResources(publicGatewayID, publicGatewayName string) terraformutils.Resource {
    33  	resources := terraformutils.NewSimpleResource(
    34  		publicGatewayID,
    35  		normalizeResourceName(publicGatewayName, false),
    36  		"ibm_is_public_gateway",
    37  		"ibm",
    38  		[]string{})
    39  	return resources
    40  }
    41  
    42  // InitResources ...
    43  func (g *PublicGatewayGenerator) InitResources() error {
    44  	region := g.Args["region"].(string)
    45  	apiKey := os.Getenv("IC_API_KEY")
    46  	if apiKey == "" {
    47  		log.Fatal("No API key set")
    48  	}
    49  
    50  	vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region)
    51  	vpcoptions := &vpcv1.VpcV1Options{
    52  		URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl),
    53  		Authenticator: &core.IamAuthenticator{
    54  			ApiKey: apiKey,
    55  		},
    56  	}
    57  	vpcclient, err := vpcv1.NewVpcV1(vpcoptions)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	start := ""
    62  	var allrecs []vpcv1.PublicGateway
    63  	for {
    64  		options := &vpcv1.ListPublicGatewaysOptions{}
    65  		if start != "" {
    66  			options.Start = &start
    67  		}
    68  		if rg := g.Args["resource_group"].(string); rg != "" {
    69  			rg, err = GetResourceGroupID(apiKey, rg, region)
    70  			if err != nil {
    71  				return fmt.Errorf("Error Fetching Resource Group Id %s", err)
    72  			}
    73  			options.ResourceGroupID = &rg
    74  		}
    75  		pgs, response, err := vpcclient.ListPublicGateways(options)
    76  		if err != nil {
    77  			return fmt.Errorf("Error Fetching Public Gateways %s\n%s", err, response)
    78  		}
    79  		start = GetNext(pgs.Next)
    80  		allrecs = append(allrecs, pgs.PublicGateways...)
    81  		if start == "" {
    82  			break
    83  		}
    84  	}
    85  
    86  	for _, pg := range allrecs {
    87  		g.Resources = append(g.Resources, g.createPublicGatewayResources(*pg.ID, *pg.Name))
    88  	}
    89  	return nil
    90  }