github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/cloudFunctions.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  	"google.golang.org/api/cloudfunctions/v1"
    23  	"google.golang.org/api/compute/v1"
    24  
    25  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    26  )
    27  
    28  var cloudFunctionsAllowEmptyValues = []string{""}
    29  
    30  type CloudFunctionsGenerator struct {
    31  	GCPService
    32  }
    33  
    34  // Run on CloudFunctionsList and create for each TerraformResource
    35  func (g CloudFunctionsGenerator) createResources(ctx context.Context, functionsList *cloudfunctions.ProjectsLocationsFunctionsListCall) []terraformutils.Resource {
    36  	resources := []terraformutils.Resource{}
    37  	if err := functionsList.Pages(ctx, func(page *cloudfunctions.ListFunctionsResponse) error {
    38  		for _, functions := range page.Functions {
    39  			t := strings.Split(functions.Name, "/")
    40  			name := t[len(t)-1]
    41  			resources = append(resources, terraformutils.NewSimpleResource(
    42  				g.GetArgs()["project"].(string)+"/"+g.GetArgs()["region"].(compute.Region).Name+"/"+name,
    43  				g.GetArgs()["region"].(compute.Region).Name+"_"+name,
    44  				"google_cloudfunctions_function",
    45  				g.ProviderName,
    46  				cloudFunctionsAllowEmptyValues,
    47  			))
    48  		}
    49  		return nil
    50  	}); err != nil {
    51  		log.Println(err)
    52  	}
    53  	return resources
    54  }
    55  
    56  // Generate TerraformResources from GCP API,
    57  // from each CloudFunctions create 1 TerraformResource
    58  // Need CloudFunctions name as ID for terraform resource
    59  func (g *CloudFunctionsGenerator) InitResources() error {
    60  	ctx := context.Background()
    61  	cloudfunctionsService, err := cloudfunctions.NewService(ctx)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	functionsList := cloudfunctionsService.Projects.Locations.Functions.List("projects/" + g.GetArgs()["project"].(string) + "/locations/" + g.GetArgs()["region"].(compute.Region).Name)
    67  
    68  	g.Resources = g.createResources(ctx, functionsList)
    69  	return nil
    70  }