github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_instance.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  // InstanceGenerator ...
    27  type InstanceGenerator struct {
    28  	IBMService
    29  }
    30  
    31  func (g InstanceGenerator) createInstanceResources(instanceID, instanceName, instanceImgID string) terraformutils.Resource {
    32  	resource := terraformutils.NewResource(
    33  		instanceID,
    34  		normalizeResourceName(instanceName, false),
    35  		"ibm_is_instance",
    36  		"ibm",
    37  		map[string]string{
    38  			"image": instanceImgID,
    39  		},
    40  		[]string{},
    41  		map[string]interface{}{
    42  			"keys": []string{},
    43  		})
    44  
    45  	// Deprecated parameters
    46  	resource.IgnoreKeys = append(resource.IgnoreKeys,
    47  		"^port$",
    48  	)
    49  	return resource
    50  }
    51  
    52  // InitResources ...
    53  func (g *InstanceGenerator) InitResources() error {
    54  	region := g.Args["region"].(string)
    55  	apiKey := os.Getenv("IC_API_KEY")
    56  	if apiKey == "" {
    57  		return fmt.Errorf("No API key set")
    58  	}
    59  
    60  	vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region)
    61  	vpcoptions := &vpcv1.VpcV1Options{
    62  		URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl),
    63  		Authenticator: &core.IamAuthenticator{
    64  			ApiKey: apiKey,
    65  		},
    66  	}
    67  	vpcclient, err := vpcv1.NewVpcV1(vpcoptions)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	start := ""
    72  	var allrecs []vpcv1.Instance
    73  	for {
    74  		options := &vpcv1.ListInstancesOptions{}
    75  		if start != "" {
    76  			options.Start = &start
    77  		}
    78  		if rg := g.Args["resource_group"].(string); rg != "" {
    79  			rg, err = GetResourceGroupID(apiKey, rg, region)
    80  			if err != nil {
    81  				return fmt.Errorf("Error Fetching Resource Group Id %s", err)
    82  			}
    83  			options.ResourceGroupID = &rg
    84  		}
    85  		instances, response, err := vpcclient.ListInstances(options)
    86  		if err != nil {
    87  			return fmt.Errorf("Error Fetching Instances %s\n%s", err, response)
    88  		}
    89  		start = GetNext(instances.Next)
    90  		allrecs = append(allrecs, instances.Instances...)
    91  		if start == "" {
    92  			break
    93  		}
    94  	}
    95  
    96  	for _, instance := range allrecs {
    97  		g.Resources = append(g.Resources, g.createInstanceResources(*instance.ID, *instance.Name, *instance.Image.ID))
    98  	}
    99  	return nil
   100  }