github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/digitalocean/domain.go (about) 1 // Copyright 2019 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 digitalocean 16 17 import ( 18 "context" 19 "strconv" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 "github.com/digitalocean/godo" 23 ) 24 25 type DomainGenerator struct { 26 DigitalOceanService 27 } 28 29 func (g *DomainGenerator) loadDomains(ctx context.Context, client *godo.Client) ([]godo.Domain, error) { 30 list := []godo.Domain{} 31 32 // create options. initially, these will be blank 33 opt := &godo.ListOptions{} 34 for { 35 domains, resp, err := client.Domains.List(ctx, opt) 36 if err != nil { 37 return nil, err 38 } 39 40 for _, domain := range domains { 41 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 42 domain.Name, 43 domain.Name, 44 "digitalocean_domain", 45 "digitalocean", 46 []string{})) 47 list = append(list, domain) 48 } 49 50 // if we are at the last page, break out the for loop 51 if resp.Links == nil || resp.Links.IsLastPage() { 52 break 53 } 54 55 page, err := resp.Links.CurrentPage() 56 if err != nil { 57 return nil, err 58 } 59 60 // set the page we want for the next request 61 opt.Page = page + 1 62 } 63 64 return list, nil 65 } 66 67 func (g *DomainGenerator) loadRecords(ctx context.Context, client *godo.Client, domain string) error { 68 // create options. initially, these will be blank 69 opt := &godo.ListOptions{} 70 for { 71 records, resp, err := client.Domains.Records(ctx, domain, opt) 72 if err != nil { 73 return err 74 } 75 76 for _, record := range records { 77 g.Resources = append(g.Resources, terraformutils.NewResource( 78 strconv.Itoa(record.ID), 79 strconv.Itoa(record.ID), 80 "digitalocean_record", 81 "digitalocean", 82 map[string]string{"domain": domain}, 83 []string{}, 84 map[string]interface{}{})) 85 } 86 87 // if we are at the last page, break out the for loop 88 if resp.Links == nil || resp.Links.IsLastPage() { 89 break 90 } 91 92 page, err := resp.Links.CurrentPage() 93 if err != nil { 94 return err 95 } 96 97 // set the page we want for the next request 98 opt.Page = page + 1 99 } 100 101 return nil 102 } 103 104 func (g *DomainGenerator) InitResources() error { 105 client := g.generateClient() 106 domains, err := g.loadDomains(context.TODO(), client) 107 if err != nil { 108 return err 109 } 110 for _, domain := range domains { 111 err := g.loadRecords(context.TODO(), client, domain.Name) 112 if err != nil { 113 return err 114 } 115 } 116 return nil 117 }