github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/linode/nodebalancer.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 NodeBalancerGenerator struct { 26 LinodeService 27 } 28 29 func (g *NodeBalancerGenerator) loadNodeBalancers(client linodego.Client) ([]linodego.NodeBalancer, error) { 30 nodeBalancerList, err := client.ListNodeBalancers(context.Background(), nil) 31 if err != nil { 32 return nil, err 33 } 34 for _, nodeBalancer := range nodeBalancerList { 35 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 36 strconv.Itoa(nodeBalancer.ID), 37 strconv.Itoa(nodeBalancer.ID), 38 "linode_nodebalancer", 39 "linode", 40 []string{})) 41 } 42 return nodeBalancerList, nil 43 } 44 45 func (g *NodeBalancerGenerator) loadNodeBalancerConfigs(client linodego.Client, nodebalancerID int) ([]linodego.NodeBalancerConfig, error) { 46 nodeBalancerConfigList, err := client.ListNodeBalancerConfigs(context.Background(), nodebalancerID, nil) 47 if err != nil { 48 return nil, err 49 } 50 for _, nodeBalancerConfig := range nodeBalancerConfigList { 51 g.Resources = append(g.Resources, terraformutils.NewResource( 52 strconv.Itoa(nodeBalancerConfig.ID), 53 strconv.Itoa(nodeBalancerConfig.ID), 54 "linode_nodebalancer_config", 55 "linode", 56 map[string]string{"nodebalancer_id": strconv.Itoa(nodebalancerID)}, 57 []string{}, 58 map[string]interface{}{})) 59 } 60 return nodeBalancerConfigList, nil 61 } 62 63 func (g *NodeBalancerGenerator) loadNodeBalancerNodes(client linodego.Client, nodebalancerID int, nodebalancerConfigID int) error { 64 nodeBalancerNodeList, err := client.ListNodeBalancerNodes(context.Background(), nodebalancerID, nodebalancerConfigID, nil) 65 if err != nil { 66 return err 67 } 68 for _, nodeBalancerNode := range nodeBalancerNodeList { 69 g.Resources = append(g.Resources, terraformutils.NewResource( 70 strconv.Itoa(nodeBalancerNode.ID), 71 strconv.Itoa(nodeBalancerNode.ID), 72 "linode_nodebalancer_node", 73 "linode", 74 map[string]string{ 75 "nodebalancer_id": strconv.Itoa(nodebalancerID), 76 "config_id": strconv.Itoa(nodebalancerConfigID), 77 }, 78 []string{}, 79 map[string]interface{}{})) 80 } 81 return nil 82 } 83 84 func (g *NodeBalancerGenerator) InitResources() error { 85 client := g.generateClient() 86 nodeBalancerList, err := g.loadNodeBalancers(client) 87 if err != nil { 88 return err 89 } 90 for _, nodeBalancer := range nodeBalancerList { 91 nodeBalancerConfigList, err := g.loadNodeBalancerConfigs(client, nodeBalancer.ID) 92 if err != nil { 93 return err 94 } 95 for _, nodeBalancerConfig := range nodeBalancerConfigList { 96 err := g.loadNodeBalancerNodes(client, nodeBalancer.ID, nodeBalancerConfig.ID) 97 if err != nil { 98 return err 99 } 100 } 101 } 102 return nil 103 }