github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/clouddns.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 "fmt" 20 "log" 21 "strings" 22 23 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 24 25 "google.golang.org/api/dns/v1" 26 ) 27 28 var cloudDNSAllowEmptyValues = []string{} 29 30 var cloudDNSAdditionalFields = map[string]interface{}{} 31 32 type CloudDNSGenerator struct { 33 GCPService 34 } 35 36 func (g CloudDNSGenerator) createZonesResources(ctx context.Context, svc *dns.Service, project string) []terraformutils.Resource { 37 resources := []terraformutils.Resource{} 38 managedZonesListCall := svc.ManagedZones.List(project) 39 err := managedZonesListCall.Pages(ctx, func(listDNS *dns.ManagedZonesListResponse) error { 40 for _, zone := range listDNS.ManagedZones { 41 resources = append(resources, terraformutils.NewResource( 42 zone.Name, 43 zone.Name, 44 "google_dns_managed_zone", 45 g.ProviderName, 46 map[string]string{ 47 "name": zone.Name, 48 "project": project, 49 }, 50 cloudDNSAllowEmptyValues, 51 cloudDNSAdditionalFields, 52 )) 53 records := g.createRecordsResources(ctx, svc, project, zone.Name) 54 resources = append(resources, records...) 55 } 56 return nil 57 }) 58 if err != nil { 59 log.Println(err) 60 return []terraformutils.Resource{} 61 } 62 return resources 63 } 64 func (g CloudDNSGenerator) createRecordsResources(ctx context.Context, svc *dns.Service, project, zoneName string) []terraformutils.Resource { 65 resources := []terraformutils.Resource{} 66 managedRecordsListCall := svc.ResourceRecordSets.List(project, zoneName) 67 err := managedRecordsListCall.Pages(ctx, func(listDNS *dns.ResourceRecordSetsListResponse) error { 68 for _, record := range listDNS.Rrsets { 69 resources = append(resources, terraformutils.NewResource( 70 fmt.Sprintf("%s/%s/%s", zoneName, record.Name, record.Type), 71 zoneName+"_"+strings.TrimSuffix(record.Name+"-"+record.Type, "."), 72 "google_dns_record_set", 73 g.ProviderName, 74 map[string]string{ 75 "name": record.Name, 76 "managed_zone": zoneName, 77 "type": record.Type, 78 "project": project, 79 }, 80 cloudDNSAllowEmptyValues, 81 cloudDNSAdditionalFields, 82 )) 83 } 84 return nil 85 }) 86 if err != nil { 87 log.Println(err) 88 return []terraformutils.Resource{} 89 } 90 return resources 91 } 92 93 // Generate TerraformResources from GCP API, 94 // create terraform resource for each zone + each record 95 func (g *CloudDNSGenerator) InitResources() error { 96 project := g.GetArgs()["project"].(string) 97 ctx := context.Background() 98 svc, err := dns.NewService(ctx) 99 if err != nil { 100 return err 101 } 102 103 g.Resources = g.createZonesResources(ctx, svc, project) 104 return nil 105 } 106 107 func (g *CloudDNSGenerator) PostConvertHook() error { 108 for i, resourceRecord := range g.Resources { 109 if resourceRecord.InstanceInfo.Type == "google_dns_managed_zone" { 110 continue 111 } 112 item := resourceRecord.Item 113 zoneID := item["managed_zone"].(string) 114 for _, resourceZone := range g.Resources { 115 if resourceZone.InstanceInfo.Type != "google_dns_managed_zone" { 116 continue 117 } 118 if zoneID == resourceZone.InstanceState.ID { 119 g.Resources[i].Item["managed_zone"] = "google_dns_managed_zone." + resourceZone.ResourceName + ".name" 120 name := g.Resources[i].Item["name"].(string) 121 name = strings.ReplaceAll(name, resourceZone.Item["dns_name"].(string), "") 122 g.Resources[i].Item["name"] = name + "google_dns_managed_zone." + resourceZone.ResourceName + ".dns_name" 123 } 124 } 125 } 126 return nil 127 }