github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/instances.go (about)

     1  // Copyright 2018 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 gcp
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  	"strings"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  
    24  	"google.golang.org/api/compute/v1"
    25  )
    26  
    27  var instancesAllowEmptyValues = []string{"labels."}
    28  
    29  var instancesAdditionalFields = map[string]interface{}{}
    30  
    31  type InstancesGenerator struct {
    32  	GCPService
    33  }
    34  
    35  // Run on instancesList and create for each TerraformResource
    36  func (g InstancesGenerator) createResources(ctx context.Context, instancesList *compute.InstancesListCall, zone string) []terraformutils.Resource {
    37  	resources := []terraformutils.Resource{}
    38  	if err := instancesList.Pages(ctx, func(page *compute.InstanceList) error {
    39  		for _, obj := range page.Items {
    40  			if strings.HasPrefix(obj.Name, "gke-") {
    41  				continue
    42  			}
    43  			resource := terraformutils.NewResource(
    44  				obj.Name,
    45  				obj.Name,
    46  				"google_compute_instance",
    47  				g.ProviderName,
    48  				map[string]string{
    49  					"name":    obj.Name,
    50  					"project": g.GetArgs()["project"].(string),
    51  					"zone":    zone,
    52  					"disk.#":  "0",
    53  				},
    54  				instancesAllowEmptyValues,
    55  				instancesAdditionalFields,
    56  			)
    57  			resource.IgnoreKeys = append(resource.IgnoreKeys, "^boot_disk.[0-9].initialize_params\\.(.*)")
    58  			resources = append(resources, resource)
    59  		}
    60  		return nil
    61  	}); err != nil {
    62  		log.Println(err)
    63  	}
    64  	return resources
    65  }
    66  
    67  // Generate TerraformResources from GCP API,
    68  // from each instances create 1 TerraformResource
    69  // Need instances name as ID for terraform resource
    70  func (g *InstancesGenerator) InitResources() error {
    71  	ctx := context.Background()
    72  	computeService, err := compute.NewService(ctx)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	for _, zoneLink := range g.GetArgs()["region"].(compute.Region).Zones {
    78  		t := strings.Split(zoneLink, "/")
    79  		zone := t[len(t)-1]
    80  		instancesList := computeService.Instances.List(g.GetArgs()["project"].(string), zone)
    81  		g.Resources = append(g.Resources, g.createResources(ctx, instancesList, zone)...)
    82  	}
    83  	return nil
    84  }