github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/subnetworks_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  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  
    24  	"google.golang.org/api/compute/v1"
    25  )
    26  
    27  var subnetworksAllowEmptyValues = []string{""}
    28  
    29  var subnetworksAdditionalFields = map[string]interface{}{}
    30  
    31  type SubnetworksGenerator struct {
    32  	GCPService
    33  }
    34  
    35  // Run on subnetworksList and create for each TerraformResource
    36  func (g SubnetworksGenerator) createResources(ctx context.Context, subnetworksList *compute.SubnetworksListCall) []terraformutils.Resource {
    37  	resources := []terraformutils.Resource{}
    38  	if err := subnetworksList.Pages(ctx, func(page *compute.SubnetworkList) error {
    39  		for _, obj := range page.Items {
    40  			resources = append(resources, terraformutils.NewResource(
    41  				obj.Name,
    42  				obj.Name,
    43  				"google_compute_subnetwork",
    44  				g.ProviderName,
    45  				map[string]string{
    46  					"name":    obj.Name,
    47  					"project": g.GetArgs()["project"].(string),
    48  					"region":  g.GetArgs()["region"].(compute.Region).Name,
    49  				},
    50  				subnetworksAllowEmptyValues,
    51  				subnetworksAdditionalFields,
    52  			))
    53  		}
    54  		return nil
    55  	}); err != nil {
    56  		log.Println(err)
    57  	}
    58  	return resources
    59  }
    60  
    61  // Generate TerraformResources from GCP API,
    62  // from each subnetworks create 1 TerraformResource
    63  // Need subnetworks name as ID for terraform resource
    64  func (g *SubnetworksGenerator) InitResources() error {
    65  	ctx := context.Background()
    66  	computeService, err := compute.NewService(ctx)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	subnetworksList := computeService.Subnetworks.List(g.GetArgs()["project"].(string), g.GetArgs()["region"].(compute.Region).Name)
    72  	g.Resources = g.createResources(ctx, subnetworksList)
    73  
    74  	return nil
    75  
    76  }