github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/dns.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 alicloud 16 17 import ( 18 "github.com/GoogleCloudPlatform/terraformer/providers/alicloud/connectivity" 19 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 20 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" 21 "github.com/aliyun/alibaba-cloud-sdk-go/services/alidns" 22 ) 23 24 // DNSGenerator Struct for generating AliCloud Elastic Compute Service 25 type DNSGenerator struct { 26 AliCloudService 27 } 28 29 func resourceFromDomain(domain alidns.DomainInDescribeDomains) terraformutils.Resource { 30 return terraformutils.NewResource( 31 domain.DomainName, // id 32 domain.DomainId+"__"+domain.DomainName, // nolint 33 "alicloud_dns", 34 "alicloud", 35 map[string]string{}, 36 []string{}, 37 map[string]interface{}{}, 38 ) 39 } 40 41 func resourceFromDomainRecord(record alidns.Record) terraformutils.Resource { 42 return terraformutils.NewResource( 43 record.RecordId, // nolint 44 record.RecordId+"__"+record.DomainName, // nolint 45 "alicloud_dns_record", 46 "alicloud", 47 map[string]string{}, 48 []string{}, 49 map[string]interface{}{}, 50 ) 51 } 52 53 func initDomains(client *connectivity.AliyunClient) ([]alidns.DomainInDescribeDomains, error) { 54 remaining := 1 55 pageNumber := 1 56 pageSize := 10 57 58 allDomains := make([]alidns.DomainInDescribeDomains, 0) 59 60 for remaining > 0 { 61 raw, err := client.WithDNSClient(func(alidnsClient *alidns.Client) (interface{}, error) { 62 request := alidns.CreateDescribeDomainsRequest() 63 request.RegionId = client.RegionID 64 request.PageSize = requests.NewInteger(pageSize) 65 request.PageNumber = requests.NewInteger(pageNumber) 66 return alidnsClient.DescribeDomains(request) 67 }) 68 if err != nil { 69 return nil, err 70 } 71 72 response := raw.(*alidns.DescribeDomainsResponse) 73 allDomains = append(allDomains, response.Domains.Domain...) 74 remaining = int(response.TotalCount) - pageNumber*pageSize 75 pageNumber++ 76 } 77 78 return allDomains, nil 79 } 80 81 func initDomainRecords(client *connectivity.AliyunClient, allDomains []alidns.DomainInDescribeDomains) ([]alidns.Record, error) { 82 allDomainRecords := make([]alidns.Record, 0) 83 84 for _, domain := range allDomains { 85 remaining := 1 86 pageNumber := 1 87 pageSize := 10 88 89 for remaining > 0 { 90 raw, err := client.WithDNSClient(func(alidnsClient *alidns.Client) (interface{}, error) { 91 request := alidns.CreateDescribeDomainRecordsRequest() 92 request.RegionId = client.RegionID 93 request.DomainName = domain.DomainName 94 request.PageSize = requests.NewInteger(pageSize) 95 request.PageNumber = requests.NewInteger(pageNumber) 96 return alidnsClient.DescribeDomainRecords(request) 97 }) 98 if err != nil { 99 return nil, err 100 } 101 102 response := raw.(*alidns.DescribeDomainRecordsResponse) 103 allDomainRecords = append(allDomainRecords, response.DomainRecords.Record...) 104 remaining = int(response.TotalCount) - pageNumber*pageSize 105 pageNumber++ 106 } 107 } 108 109 return allDomainRecords, nil 110 } 111 112 // InitResources Gets the list of all alidns domain ids and generates resources 113 func (g *DNSGenerator) InitResources() error { 114 client, err := g.LoadClientFromProfile() 115 if err != nil { 116 return err 117 } 118 119 allDomains, err := initDomains(client) 120 if err != nil { 121 return err 122 } 123 124 allDomainRecords, err := initDomainRecords(client, allDomains) 125 if err != nil { 126 return err 127 } 128 129 for _, domain := range allDomains { 130 resource := resourceFromDomain(domain) 131 g.Resources = append(g.Resources, resource) 132 } 133 134 for _, record := range allDomainRecords { 135 resource := resourceFromDomainRecord(record) 136 g.Resources = append(g.Resources, resource) 137 } 138 139 return nil 140 }