github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/linode/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 linode
    16  
    17  import (
    18  	"context"
    19  	"strconv"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/linode/linodego"
    23  )
    24  
    25  type DomainGenerator struct {
    26  	LinodeService
    27  }
    28  
    29  func (g *DomainGenerator) loadDomains(client linodego.Client) ([]linodego.Domain, error) {
    30  	domainList, err := client.ListDomains(context.Background(), nil)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	for _, domain := range domainList {
    35  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    36  			strconv.Itoa(domain.ID),
    37  			strconv.Itoa(domain.ID),
    38  			"linode_domain",
    39  			"linode",
    40  			[]string{}))
    41  	}
    42  	return domainList, nil
    43  }
    44  
    45  func (g *DomainGenerator) loadDomainRecords(client linodego.Client, domainID int) error {
    46  	domainRecordList, err := client.ListDomainRecords(context.Background(), domainID, nil)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	for _, domainRecord := range domainRecordList {
    51  		g.Resources = append(g.Resources, terraformutils.NewResource(
    52  			strconv.Itoa(domainRecord.ID),
    53  			strconv.Itoa(domainRecord.ID),
    54  			"linode_domain_record",
    55  			"linode",
    56  			map[string]string{"domain_id": strconv.Itoa(domainID)},
    57  			[]string{},
    58  			map[string]interface{}{}))
    59  	}
    60  	return nil
    61  }
    62  
    63  func (g *DomainGenerator) InitResources() error {
    64  	client := g.generateClient()
    65  	domainList, err := g.loadDomains(client)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	for _, domain := range domainList {
    70  		err := g.loadDomainRecords(client, domain.ID)
    71  		if err != nil {
    72  			return err
    73  		}
    74  	}
    75  	return nil
    76  }