github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/dns.go (about) 1 // Copyright 2020 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 azure 16 17 import ( 18 "context" 19 "log" 20 "strings" 21 22 "github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns" 23 "github.com/Azure/go-autorest/autorest" 24 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 25 "github.com/hashicorp/go-azure-helpers/authentication" 26 ) 27 28 type DNSGenerator struct { 29 AzureService 30 } 31 32 func (g *DNSGenerator) listRecordSets(resourceGroupName string, zoneName string, top *int32) ([]terraformutils.Resource, error) { 33 var resources []terraformutils.Resource 34 ctx := context.Background() 35 subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID 36 RecordSetsClient := dns.NewRecordSetsClient(subscriptionID) 37 RecordSetsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer) 38 39 recordSetIterator, err := RecordSetsClient.ListAllByDNSZoneComplete(ctx, resourceGroupName, zoneName, top, "") 40 if err != nil { 41 return nil, err 42 } 43 for recordSetIterator.NotDone() { 44 recordSet := recordSetIterator.Value() 45 // NOTE: 46 // Format example: "Microsoft.Network/dnszones/AAAA" 47 recordTypeSplitted := strings.Split(*recordSet.Type, "/") 48 recordType := recordTypeSplitted[len(recordTypeSplitted)-1] 49 typeResourceNameMap := map[string]string{ 50 "A": "azurerm_dns_a_record", 51 "AAAA": "azurerm_dns_aaaa_record", 52 "CAA": "azurerm_dns_caa_record", 53 "CNAME": "azurerm_dns_cname_record", 54 "MX": "azurerm_dns_mx_record", 55 "NS": "azurerm_dns_ns_record", 56 "PTR": "azurerm_dns_ptr_record", 57 "SRV": "azurerm_dns_srv_record", 58 "TXT": "azurerm_dns_txt_record", 59 } 60 if resName, exist := typeResourceNameMap[recordType]; exist { 61 resources = append(resources, terraformutils.NewSimpleResource( 62 *recordSet.ID, 63 *recordSet.Name, 64 resName, 65 g.ProviderName, 66 []string{})) 67 } 68 69 if err := recordSetIterator.Next(); err != nil { 70 log.Println(err) 71 return resources, err 72 } 73 74 } 75 return resources, nil 76 } 77 78 func (g *DNSGenerator) listAndAddForDNSZone() ([]terraformutils.Resource, error) { 79 var resources []terraformutils.Resource 80 ctx := context.Background() 81 subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID 82 DNSZonesClient := dns.NewZonesClient(subscriptionID) 83 DNSZonesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer) 84 85 var pageSize int32 = 50 86 87 var ( 88 dnsZoneIterator dns.ZoneListResultIterator 89 err error 90 ) 91 92 if rg := g.Args["resource_group"].(string); rg != "" { 93 dnsZoneIterator, err = DNSZonesClient.ListByResourceGroupComplete(ctx, rg, &pageSize) 94 } else { 95 dnsZoneIterator, err = DNSZonesClient.ListComplete(ctx, &pageSize) 96 } 97 if err != nil { 98 return nil, err 99 } 100 for dnsZoneIterator.NotDone() { 101 zone := dnsZoneIterator.Value() 102 resources = append(resources, terraformutils.NewSimpleResource( 103 *zone.ID, 104 *zone.Name, 105 "azurerm_dns_zone", 106 g.ProviderName, 107 []string{})) 108 109 id, err := ParseAzureResourceID(*zone.ID) 110 if err != nil { 111 return nil, err 112 } 113 114 records, err := g.listRecordSets(id.ResourceGroup, *zone.Name, &pageSize) 115 if err != nil { 116 return nil, err 117 } 118 resources = append(resources, records...) 119 120 if err := dnsZoneIterator.Next(); err != nil { 121 log.Println(err) 122 return resources, err 123 } 124 } 125 126 return resources, nil 127 } 128 129 func (g *DNSGenerator) InitResources() error { 130 functions := []func() ([]terraformutils.Resource, error){ 131 g.listAndAddForDNSZone, 132 } 133 134 for _, f := range functions { 135 resources, err := f() 136 if err != nil { 137 return err 138 } 139 g.Resources = append(g.Resources, resources...) 140 } 141 142 return nil 143 }