github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/vultr/dns_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 vultr
    16  
    17  import (
    18  	"context"
    19  	"strconv"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/vultr/govultr"
    23  )
    24  
    25  type DNSDomainGenerator struct {
    26  	VultrService
    27  }
    28  
    29  func (g *DNSDomainGenerator) loadDNSDomains(client *govultr.Client) ([]govultr.DNSDomain, error) {
    30  	domainList, err := client.DNSDomain.List(context.Background())
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	for _, domain := range domainList {
    35  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    36  			domain.Domain,
    37  			domain.Domain,
    38  			"vultr_dns_domain",
    39  			"vultr",
    40  			[]string{}))
    41  	}
    42  	return domainList, nil
    43  }
    44  
    45  func (g *DNSDomainGenerator) loadDNSRecords(client *govultr.Client, domain string) error {
    46  	recordList, err := client.DNSRecord.List(context.Background(), domain)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	for _, record := range recordList {
    51  		g.Resources = append(g.Resources, terraformutils.NewResource(
    52  			strconv.Itoa(record.RecordID),
    53  			strconv.Itoa(record.RecordID),
    54  			"vultr_dns_record",
    55  			"vultr",
    56  			map[string]string{"domain": domain},
    57  			[]string{},
    58  			map[string]interface{}{}))
    59  	}
    60  	return nil
    61  }
    62  
    63  func (g *DNSDomainGenerator) InitResources() error {
    64  	client := g.generateClient()
    65  	domainList, err := g.loadDNSDomains(client)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	for _, domain := range domainList {
    70  		err := g.loadDNSRecords(client, domain.Domain)
    71  		if err != nil {
    72  			return err
    73  		}
    74  	}
    75  	return nil
    76  }