github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_instance_template.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  // InstanceTemplateGenerator ...
    27  type InstanceTemplateGenerator struct {
    28  	IBMService
    29  }
    30  
    31  func (g InstanceTemplateGenerator) createInstanceTemplateResources(templateID, templateName string) terraformutils.Resource {
    32  	resources := terraformutils.NewSimpleResource(
    33  		templateID,
    34  		normalizeResourceName(templateName, false),
    35  		"ibm_is_instance_template",
    36  		"ibm",
    37  		[]string{})
    38  	return resources
    39  }
    40  
    41  // InitResources ...
    42  func (g *InstanceTemplateGenerator) InitResources() error {
    43  	region := g.Args["region"].(string)
    44  	apiKey := os.Getenv("IC_API_KEY")
    45  	if apiKey == "" {
    46  		return fmt.Errorf("no API key set")
    47  	}
    48  
    49  	vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region)
    50  	vpcoptions := &vpcv1.VpcV1Options{
    51  		URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl),
    52  		Authenticator: &core.IamAuthenticator{
    53  			ApiKey: apiKey,
    54  		},
    55  	}
    56  	vpcclient, err := vpcv1.NewVpcV1(vpcoptions)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	options := &vpcv1.ListInstanceTemplatesOptions{}
    61  	templates, response, err := vpcclient.ListInstanceTemplates(options)
    62  	if err != nil {
    63  		return fmt.Errorf("Error Fetching Instance Templates %s\n%s", err, response)
    64  	}
    65  
    66  	for _, template := range templates.Templates {
    67  		instemp := template.(*vpcv1.InstanceTemplate)
    68  		g.Resources = append(g.Resources, g.createInstanceTemplateResources(*instemp.ID, *instemp.Name))
    69  	}
    70  	return nil
    71  }