github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_ipsec_policy.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  // IpsecGenerator ...
    28  type IpsecGenerator struct {
    29  	IBMService
    30  }
    31  
    32  func (g IpsecGenerator) createIpsecResources() func(ipsecID, ipsecName string) terraformutils.Resource {
    33  	names := make(map[string]struct{})
    34  	random := false
    35  	return func(ipsecID, ipsecName string) terraformutils.Resource {
    36  		names, random = getRandom(names, ipsecName, random)
    37  		resources := terraformutils.NewSimpleResource(
    38  			ipsecID,
    39  			normalizeResourceName(ipsecName, random),
    40  			"ibm_is_ipsec_policy",
    41  			"ibm",
    42  			[]string{})
    43  		return resources
    44  	}
    45  }
    46  
    47  // InitResources ...
    48  func (g *IpsecGenerator) InitResources() error {
    49  	region := g.Args["region"].(string)
    50  	apiKey := os.Getenv("IC_API_KEY")
    51  	if apiKey == "" {
    52  		log.Fatal("No API key set")
    53  	}
    54  
    55  	vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region)
    56  	vpcoptions := &vpcv1.VpcV1Options{
    57  		URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl),
    58  		Authenticator: &core.IamAuthenticator{
    59  			ApiKey: apiKey,
    60  		},
    61  	}
    62  	vpcclient, err := vpcv1.NewVpcV1(vpcoptions)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	start := ""
    67  	var allrecs []vpcv1.IPsecPolicy
    68  	for {
    69  		options := &vpcv1.ListIpsecPoliciesOptions{}
    70  		if start != "" {
    71  			options.Start = &start
    72  		}
    73  		policies, response, err := vpcclient.ListIpsecPolicies(options)
    74  		if err != nil {
    75  			return fmt.Errorf("Error Fetching IPSEC Policies %s\n%s", err, response)
    76  		}
    77  		start = GetNext(policies.Next)
    78  		allrecs = append(allrecs, policies.IpsecPolicies...)
    79  		if start == "" {
    80  			break
    81  		}
    82  	}
    83  
    84  	fnObjt := g.createIpsecResources()
    85  	for _, policy := range allrecs {
    86  		g.Resources = append(g.Resources, fnObjt(*policy.ID, *policy.Name))
    87  	}
    88  	return nil
    89  }