github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/schedulerJobs.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  	cloudscheduler "google.golang.org/api/cloudscheduler/v1beta1"
    23  	"google.golang.org/api/compute/v1"
    24  
    25  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    26  )
    27  
    28  var schedulerJobsAllowEmptyValues = []string{""}
    29  
    30  var schedulerJobsAdditionalFields = map[string]interface{}{}
    31  
    32  type SchedulerJobsGenerator struct {
    33  	GCPService
    34  }
    35  
    36  // Run on SchedulerJobsList and create for each TerraformResource
    37  func (g SchedulerJobsGenerator) createResources(ctx context.Context, jobsList *cloudscheduler.ProjectsLocationsJobsListCall) []terraformutils.Resource {
    38  	resources := []terraformutils.Resource{}
    39  	if err := jobsList.Pages(ctx, func(page *cloudscheduler.ListJobsResponse) error {
    40  		for _, obj := range page.Jobs {
    41  			t := strings.Split(obj.Name, "/")
    42  			name := t[len(t)-1]
    43  			resources = append(resources, terraformutils.NewResource(
    44  				obj.Name,
    45  				name,
    46  				"google_cloud_scheduler_job",
    47  				g.ProviderName,
    48  				map[string]string{
    49  					"name":    name,
    50  					"project": g.GetArgs()["project"].(string),
    51  					"region":  g.GetArgs()["region"].(compute.Region).Name,
    52  				},
    53  				schedulerJobsAllowEmptyValues,
    54  				schedulerJobsAdditionalFields,
    55  			))
    56  		}
    57  		return nil
    58  	}); err != nil {
    59  		log.Println(err)
    60  	}
    61  	return resources
    62  }
    63  
    64  // Generate TerraformResources from GCP API,
    65  func (g *SchedulerJobsGenerator) InitResources() error {
    66  	ctx := context.Background()
    67  	cloudSchedulerService, err := cloudscheduler.NewService(ctx)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	jobsList := cloudSchedulerService.Projects.Locations.Jobs.List("projects/" + g.GetArgs()["project"].(string) + "/locations/" + g.GetArgs()["region"].(compute.Region).Name)
    73  
    74  	g.Resources = g.createResources(ctx, jobsList)
    75  	return nil
    76  }