github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_ike_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  // IkeGenerator ...
    28  type IkeGenerator struct {
    29  	IBMService
    30  }
    31  
    32  func (g IkeGenerator) createIkeResources(ikeID, ikeName string) terraformutils.Resource {
    33  	resources := terraformutils.NewSimpleResource(
    34  		ikeID,
    35  		normalizeResourceName(ikeName, false),
    36  		"ibm_is_ike_policy",
    37  		"ibm",
    38  		[]string{})
    39  	return resources
    40  }
    41  
    42  // InitResources ...
    43  func (g *IkeGenerator) 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.IkePolicy
    63  	for {
    64  		options := &vpcv1.ListIkePoliciesOptions{}
    65  		if start != "" {
    66  			options.Start = &start
    67  		}
    68  		policies, response, err := vpcclient.ListIkePolicies(options)
    69  		if err != nil {
    70  			return fmt.Errorf("Error Fetching IKE Policies %s\n%s", err, response)
    71  		}
    72  		start = GetNext(policies.Next)
    73  		allrecs = append(allrecs, policies.IkePolicies...)
    74  		if start == "" {
    75  			break
    76  		}
    77  	}
    78  
    79  	for _, policy := range allrecs {
    80  		g.Resources = append(g.Resources, g.createIkeResources(*policy.ID, *policy.Name))
    81  	}
    82  	return nil
    83  }