github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/private_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/privatedns/mgmt/2018-09-01/privatedns" 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 PrivateDNSGenerator struct { 29 AzureService 30 } 31 32 func (g *PrivateDNSGenerator) listRecordSets(resourceGroupName string, privateZoneName 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 := privatedns.NewRecordSetsClient(subscriptionID) 37 RecordSetsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer) 38 39 recordSetIterator, err := RecordSetsClient.ListComplete(ctx, resourceGroupName, privateZoneName, 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/privateDnsZones/CNAME" 47 recordTypeSplitted := strings.Split(*recordSet.Type, "/") 48 recordType := recordTypeSplitted[len(recordTypeSplitted)-1] 49 typeResourceNameMap := map[string]string{ 50 "A": "azurerm_private_dns_a_record", 51 "AAAA": "azurerm_private_dns_aaaa_record", 52 "CNAME": "azurerm_private_dns_cname_record", 53 "MX": "azurerm_private_dns_mx_record", 54 "PTR": "azurerm_private_dns_ptr_record", 55 "SRV": "azurerm_private_dns_srv_record", 56 "TXT": "azurerm_private_dns_txt_record", 57 } 58 if resName, exist := typeResourceNameMap[recordType]; exist { 59 resources = append(resources, terraformutils.NewSimpleResource( 60 *recordSet.ID, 61 *recordSet.Name, 62 resName, 63 g.ProviderName, 64 []string{})) 65 } 66 67 if err := recordSetIterator.Next(); err != nil { 68 log.Println(err) 69 break 70 } 71 72 } 73 return resources, nil 74 } 75 76 func (g *PrivateDNSGenerator) listVirtualNetworkLinks(resourceGroupName string, privateZoneName string, pageSize *int32) ([]terraformutils.Resource, error) { 77 var resources []terraformutils.Resource 78 ctx := context.Background() 79 subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID 80 VirtualNetworkLinksClient := privatedns.NewVirtualNetworkLinksClient(subscriptionID) 81 VirtualNetworkLinksClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer) 82 83 virtualNetworkLinkIterator, err := VirtualNetworkLinksClient.ListComplete(ctx, resourceGroupName, privateZoneName, pageSize) 84 if err != nil { 85 return nil, err 86 } 87 for virtualNetworkLinkIterator.NotDone() { 88 virtualNetworkLink := virtualNetworkLinkIterator.Value() 89 resources = append(resources, terraformutils.NewSimpleResource( 90 *virtualNetworkLink.ID, 91 *virtualNetworkLink.Name, 92 "azurerm_private_dns_zone_virtual_network_link", 93 g.ProviderName, 94 []string{})) 95 96 if err := virtualNetworkLinkIterator.Next(); err != nil { 97 log.Println(err) 98 break 99 } 100 101 } 102 103 return resources, nil 104 } 105 106 func (g *PrivateDNSGenerator) listAndAddForPrivateDNSZone() ([]terraformutils.Resource, error) { 107 var resources []terraformutils.Resource 108 ctx := context.Background() 109 subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID 110 PrivateDNSZonesClient := privatedns.NewPrivateZonesClient(subscriptionID) 111 PrivateDNSZonesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer) 112 113 var pageSize int32 = 50 114 115 var ( 116 dnsZoneIterator privatedns.PrivateZoneListResultIterator 117 err error 118 ) 119 if rg := g.Args["resource_group"].(string); rg != "" { 120 dnsZoneIterator, err = PrivateDNSZonesClient.ListByResourceGroupComplete(ctx, rg, &pageSize) 121 } else { 122 dnsZoneIterator, err = PrivateDNSZonesClient.ListComplete(ctx, &pageSize) 123 } 124 if err != nil { 125 return nil, err 126 } 127 for dnsZoneIterator.NotDone() { 128 zone := dnsZoneIterator.Value() 129 resources = append(resources, terraformutils.NewSimpleResource( 130 *zone.ID, 131 *zone.Name, 132 "azurerm_private_dns_zone", 133 g.ProviderName, 134 []string{})) 135 136 id, err := ParseAzureResourceID(*zone.ID) 137 if err != nil { 138 return nil, err 139 } 140 141 records, err := g.listRecordSets(id.ResourceGroup, *zone.Name, &pageSize) 142 if err != nil { 143 return nil, err 144 } 145 resources = append(resources, records...) 146 147 networkLinks, err := g.listVirtualNetworkLinks(id.ResourceGroup, *zone.Name, &pageSize) 148 if err != nil { 149 return nil, err 150 } 151 resources = append(resources, networkLinks...) 152 153 if err := dnsZoneIterator.Next(); err != nil { 154 log.Println(err) 155 return resources, err 156 } 157 } 158 159 return resources, nil 160 } 161 162 func (g *PrivateDNSGenerator) InitResources() error { 163 functions := []func() ([]terraformutils.Resource, error){ 164 g.listAndAddForPrivateDNSZone, 165 } 166 167 for _, f := range functions { 168 resources, err := f() 169 if err != nil { 170 return err 171 } 172 g.Resources = append(g.Resources, resources...) 173 } 174 175 return nil 176 }