github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/networks_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 networksAllowEmptyValues = []string{""} 28 29 var networksAdditionalFields = map[string]interface{}{} 30 31 type NetworksGenerator struct { 32 GCPService 33 } 34 35 // Run on networksList and create for each TerraformResource 36 func (g NetworksGenerator) createResources(ctx context.Context, networksList *compute.NetworksListCall) []terraformutils.Resource { 37 resources := []terraformutils.Resource{} 38 if err := networksList.Pages(ctx, func(page *compute.NetworkList) error { 39 for _, obj := range page.Items { 40 resources = append(resources, terraformutils.NewResource( 41 obj.Name, 42 obj.Name, 43 "google_compute_network", 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 networksAllowEmptyValues, 51 networksAdditionalFields, 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 networks create 1 TerraformResource 63 // Need networks name as ID for terraform resource 64 func (g *NetworksGenerator) InitResources() error { 65 ctx := context.Background() 66 computeService, err := compute.NewService(ctx) 67 if err != nil { 68 return err 69 } 70 71 networksList := computeService.Networks.List(g.GetArgs()["project"].(string)) 72 g.Resources = g.createResources(ctx, networksList) 73 74 return nil 75 76 }