github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/dataproc.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 21 "google.golang.org/api/compute/v1" 22 "google.golang.org/api/dataproc/v1" 23 24 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 25 ) 26 27 var dataprocAllowEmptyValues = []string{""} 28 29 var dataprocAdditionalFields = map[string]interface{}{} 30 31 type DataprocGenerator struct { 32 GCPService 33 } 34 35 // Run on DataprocClusterList and create for each TerraformResource 36 func (g DataprocGenerator) createClusterResources(ctx context.Context, clusterList *dataproc.ProjectsRegionsClustersListCall) []terraformutils.Resource { 37 resources := []terraformutils.Resource{} 38 if err := clusterList.Pages(ctx, func(page *dataproc.ListClustersResponse) error { 39 for _, cluster := range page.Clusters { 40 resource := terraformutils.NewResource( 41 cluster.ClusterName, 42 cluster.ClusterName, 43 "google_dataproc_cluster", 44 g.ProviderName, 45 map[string]string{ 46 "name": cluster.ClusterName, 47 "project": g.GetArgs()["project"].(string), 48 "region": g.GetArgs()["region"].(compute.Region).Name, 49 }, 50 dataprocAllowEmptyValues, 51 dataprocAdditionalFields, 52 ) 53 resource.IgnoreKeys = append(resource.IgnoreKeys, "^cluster_config.[0-9].delete_autogen_bucket$") 54 resources = append(resources, resource) 55 } 56 return nil 57 }); err != nil { 58 log.Println(err) 59 } 60 return resources 61 } 62 63 /* 64 // Run on DataprocJobList and create for each TerraformResource 65 func (g DataprocGenerator) createJobResources(jobList *dataproc.ProjectsRegionsJobsListCall, ctx context.Context) []terraformutils.Resource { 66 resources := []terraformutils.Resource{} 67 if err := jobList.Pages(ctx, func(page *dataproc.ListJobsResponse) error { 68 for _, job := range page.Jobs { 69 resources = append(resources, terraformutils.NewResource( 70 job.Reference.JobId, 71 job.Reference.JobId, 72 "google_dataproc_job", 73 g.ProviderName, 74 map[string]string{ 75 "project": g.GetArgs()["project"].(string), 76 "region": g.GetArgs()["region"].(compute.Region).Name, 77 }, 78 dataprocAllowEmptyValues, 79 dataprocAdditionalFields, 80 )) 81 } 82 return nil 83 }); err != nil { 84 log.Fatal(err) 85 } 86 return resources 87 } 88 */ 89 90 // Generate TerraformResources from GCP API, 91 // from each DataprocGenerator create 1 TerraformResource 92 // Need DataprocGenerator name as ID for terraform resource 93 func (g *DataprocGenerator) InitResources() error { 94 ctx := context.Background() 95 dataprocService, err := dataproc.NewService(ctx) 96 if err != nil { 97 return err 98 } 99 100 clusterList := dataprocService.Projects.Regions.Clusters.List(g.GetArgs()["project"].(string), g.GetArgs()["region"].(compute.Region).Name) 101 g.Resources = g.createClusterResources(ctx, clusterList) 102 103 // jobList := dataprocService.Projects.Regions.Jobs.List(g.GetArgs()["project"].(string), g.GetArgs()["region"]) 104 // g.Resources = append(g.Resources, g.createJobResources(jobList, ctx)...) 105 106 return nil 107 }