github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/memoryStore.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/compute/v1" 23 "google.golang.org/api/redis/v1" 24 25 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 26 ) 27 28 var redisAllowEmptyValues = []string{""} 29 30 var redisAdditionalFields = map[string]interface{}{} 31 32 type MemoryStoreGenerator struct { 33 GCPService 34 } 35 36 // Run on redisInstancesList and create for each TerraformResource 37 func (g MemoryStoreGenerator) createResources(ctx context.Context, redisInstancesList *redis.ProjectsLocationsInstancesListCall) []terraformutils.Resource { 38 resources := []terraformutils.Resource{} 39 if err := redisInstancesList.Pages(ctx, func(page *redis.ListInstancesResponse) error { 40 for _, obj := range page.Instances { 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_redis_instance", 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 redisAllowEmptyValues, 54 redisAdditionalFields, 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 // from each redis create 1 TerraformResource 66 // Need Redis name as ID for terraform resource 67 func (g *MemoryStoreGenerator) InitResources() error { 68 ctx := context.Background() 69 redisService, err := redis.NewService(ctx) 70 if err != nil { 71 return err 72 } 73 74 redisInstancesList := redisService.Projects.Locations.Instances.List("projects/" + g.GetArgs()["project"].(string) + "/locations/" + g.GetArgs()["region"].(compute.Region).Name) 75 76 g.Resources = g.createResources(ctx, redisInstancesList) 77 return nil 78 }