github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/reservations_gen.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  // AUTO-GENERATED CODE. DO NOT EDIT.
    16  package gcp
    17  
    18  import (
    19  	"context"
    20  	"log"
    21  	"strings"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  
    25  	"google.golang.org/api/compute/v1"
    26  )
    27  
    28  var reservationsAllowEmptyValues = []string{""}
    29  
    30  var reservationsAdditionalFields = map[string]interface{}{}
    31  
    32  type ReservationsGenerator struct {
    33  	GCPService
    34  }
    35  
    36  // Run on reservationsList and create for each TerraformResource
    37  func (g ReservationsGenerator) createResources(ctx context.Context, reservationsList *compute.ReservationsListCall, zone string) []terraformutils.Resource {
    38  	resources := []terraformutils.Resource{}
    39  	if err := reservationsList.Pages(ctx, func(page *compute.ReservationList) error {
    40  		for _, obj := range page.Items {
    41  			resources = append(resources, terraformutils.NewResource(
    42  				zone+"/"+obj.Name,
    43  				zone+"/"+obj.Name,
    44  				"google_compute_reservation",
    45  				g.ProviderName,
    46  				map[string]string{
    47  					"name":    obj.Name,
    48  					"project": g.GetArgs()["project"].(string),
    49  					"region":  g.GetArgs()["region"].(compute.Region).Name,
    50  					"zone":    zone,
    51  				},
    52  				reservationsAllowEmptyValues,
    53  				reservationsAdditionalFields,
    54  			))
    55  		}
    56  		return nil
    57  	}); err != nil {
    58  		log.Println(err)
    59  	}
    60  	return resources
    61  }
    62  
    63  // Generate TerraformResources from GCP API,
    64  // from each reservations create 1 TerraformResource
    65  // Need reservations name as ID for terraform resource
    66  func (g *ReservationsGenerator) InitResources() error {
    67  	ctx := context.Background()
    68  	computeService, err := compute.NewService(ctx)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	for _, zoneLink := range g.GetArgs()["region"].(compute.Region).Zones {
    74  		t := strings.Split(zoneLink, "/")
    75  		zone := t[len(t)-1]
    76  		reservationsList := computeService.Reservations.List(g.GetArgs()["project"].(string), zone)
    77  		g.Resources = append(g.Resources, g.createResources(ctx, reservationsList, zone)...)
    78  	}
    79  
    80  	return nil
    81  
    82  }