Skip to content

Commit

Permalink
add support for additional linode resources
Browse files Browse the repository at this point in the history
  • Loading branch information
aqche committed Nov 24, 2019
1 parent aecb1ce commit ccd2a79
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 13 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ $ terraformer import plan generated/google/my-project/terraformer/plan.json

Terraformer by default separates each resource into a file, which is put into a given service directory.

The default path for resource files is `{output}/{provider}/{service}/{resource}.tf` and can vary for each provider.
The default path for resource files is `{output}/{provider}/{service}/{resource}.tf` and can vary for each provider.

It's possible to adjust the generated structure by:
1. Using `--compact` parameter to group resource files within a single service into one `resources.tf` file
Expand Down Expand Up @@ -692,14 +692,21 @@ List of supported Linode resources:

* `domain`
* `linode_domain`
* `linode_domain_record`
* `image`
* `linode_image`
* `instance`
* `linode_instance`
* `nodebalancer`
* `linode_nodebalancer`
* `linode_nodebalancer_config`
* `linode_nodebalancer_node`
* `rdns`
* `linode_rdns`
* `sshkey`
* `linode_sshkey`
* `stackscript`
* `linode_stackscript`
* `token`
* `linode_token`
* `volume`
Expand Down
38 changes: 32 additions & 6 deletions providers/linode/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,51 @@ type DomainGenerator struct {
LinodeService
}

func (g DomainGenerator) createResources(domainList []linodego.Domain) []terraform_utils.Resource {
var resources []terraform_utils.Resource
func (g *DomainGenerator) loadDomains(client linodego.Client) ([]linodego.Domain, error) {
domainList, err := client.ListDomains(context.Background(), nil)
if err != nil {
return nil, err
}
for _, domain := range domainList {
resources = append(resources, terraform_utils.NewSimpleResource(
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
strconv.Itoa(domain.ID),
strconv.Itoa(domain.ID),
"linode_domain",
"linode",
[]string{}))
}
return resources
return domainList, nil
}

func (g *DomainGenerator) loadDomainRecords(client linodego.Client, domainID int) error {
domainRecordList, err := client.ListDomainRecords(context.Background(), domainID, nil)
if err != nil {
return err
}
for _, domainRecord := range domainRecordList {
g.Resources = append(g.Resources, terraform_utils.NewResource(
strconv.Itoa(domainRecord.ID),
strconv.Itoa(domainRecord.ID),
"linode_domain_record",
"linode",
map[string]string{"domain_id": strconv.Itoa(domainID)},
[]string{},
map[string]interface{}{}))
}
return nil
}

func (g *DomainGenerator) InitResources() error {
client := g.generateClient()
output, err := client.ListDomains(context.Background(), nil)
domainList, err := g.loadDomains(client)
if err != nil {
return err
}
g.Resources = g.createResources(output)
for _, domain := range domainList {
err := g.loadDomainRecords(client, domain.ID)
if err != nil {
return err
}
}
return nil
}
2 changes: 2 additions & 0 deletions providers/linode/linode_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func (p *LinodeProvider) GetSupportedService() map[string]terraform_utils.Servic
"image": &ImageGenerator{},
"instance": &InstanceGenerator{},
"nodebalancer": &NodeBalancerGenerator{},
"rdns": &RDNSGenerator{},
"sshkey": &SSHKeyGenerator{},
"stackscript": &StackScriptGenerator{},
"token": &TokenGenerator{},
"volume": &VolumeGenerator{},
}
Expand Down
65 changes: 59 additions & 6 deletions providers/linode/nodebalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,78 @@ type NodeBalancerGenerator struct {
LinodeService
}

func (g NodeBalancerGenerator) createResources(nodeBalancerList []linodego.NodeBalancer) []terraform_utils.Resource {
var resources []terraform_utils.Resource
func (g *NodeBalancerGenerator) loadNodeBalancers(client linodego.Client) ([]linodego.NodeBalancer, error) {
nodeBalancerList, err := client.ListNodeBalancers(context.Background(), nil)
if err != nil {
return nil, err
}
for _, nodeBalancer := range nodeBalancerList {
resources = append(resources, terraform_utils.NewSimpleResource(
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
strconv.Itoa(nodeBalancer.ID),
strconv.Itoa(nodeBalancer.ID),
"linode_nodebalancer",
"linode",
[]string{}))
}
return resources
return nodeBalancerList, nil
}

func (g *NodeBalancerGenerator) loadNodeBalancerConfigs(client linodego.Client, nodebalancerID int) ([]linodego.NodeBalancerConfig, error) {
nodeBalancerConfigList, err := client.ListNodeBalancerConfigs(context.Background(), nodebalancerID, nil)
if err != nil {
return nil, err
}
for _, nodeBalancerConfig := range nodeBalancerConfigList {
g.Resources = append(g.Resources, terraform_utils.NewResource(
strconv.Itoa(nodeBalancerConfig.ID),
strconv.Itoa(nodeBalancerConfig.ID),
"linode_nodebalancer_config",
"linode",
map[string]string{"nodebalancer_id": strconv.Itoa(nodebalancerID)},
[]string{},
map[string]interface{}{}))
}
return nodeBalancerConfigList, nil
}

func (g *NodeBalancerGenerator) loadNodeBalancerNodes(client linodego.Client, nodebalancerID int, nodebalancerConfigID int) error {
nodeBalancerNodeList, err := client.ListNodeBalancerNodes(context.Background(), nodebalancerID, nodebalancerConfigID, nil)
if err != nil {
return err
}
for _, nodeBalancerNode := range nodeBalancerNodeList {
g.Resources = append(g.Resources, terraform_utils.NewResource(
strconv.Itoa(nodeBalancerNode.ID),
strconv.Itoa(nodeBalancerNode.ID),
"linode_nodebalancer_node",
"linode",
map[string]string{
"nodebalancer_id": strconv.Itoa(nodebalancerID),
"config_id": strconv.Itoa(nodebalancerConfigID),
},
[]string{},
map[string]interface{}{}))
}
return nil
}

func (g *NodeBalancerGenerator) InitResources() error {
client := g.generateClient()
output, err := client.ListNodeBalancers(context.Background(), nil)
nodeBalancerList, err := g.loadNodeBalancers(client)
if err != nil {
return err
}
g.Resources = g.createResources(output)
for _, nodeBalancer := range nodeBalancerList {
nodeBalancerConfigList, err := g.loadNodeBalancerConfigs(client, nodeBalancer.ID)
if err != nil {
return err
}
for _, nodeBalancerConfig := range nodeBalancerConfigList {
err := g.loadNodeBalancerNodes(client, nodeBalancer.ID, nodeBalancerConfig.ID)
if err != nil {
return err
}
}
}
return nil
}
49 changes: 49 additions & 0 deletions providers/linode/rdns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package linode

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/terraform_utils"
"github.com/linode/linodego"
)

type RDNSGenerator struct {
LinodeService
}

func (g RDNSGenerator) createResources(instanceIPList []linodego.InstanceIP) []terraform_utils.Resource {
var resources []terraform_utils.Resource
for _, instanceIP := range instanceIPList {
resources = append(resources, terraform_utils.NewSimpleResource(
instanceIP.Address,
instanceIP.Address,
"linode_rdns",
"linode",
[]string{}))
}
return resources
}

func (g *RDNSGenerator) InitResources() error {
client := g.generateClient()
output, err := client.ListIPAddresses(context.Background(), nil)
if err != nil {
return err
}
g.Resources = g.createResources(output)
return nil
}
53 changes: 53 additions & 0 deletions providers/linode/stackscript.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package linode

import (
"context"
"strconv"

"github.com/GoogleCloudPlatform/terraformer/terraform_utils"
"github.com/linode/linodego"
)

type StackScriptGenerator struct {
LinodeService
}

func (g StackScriptGenerator) createResources(stackscriptList []linodego.Stackscript) []terraform_utils.Resource {
var resources []terraform_utils.Resource
for _, stackscript := range stackscriptList {
// Avoid importing all community stackscripts
if !stackscript.IsPublic {
resources = append(resources, terraform_utils.NewSimpleResource(
strconv.Itoa(stackscript.ID),
strconv.Itoa(stackscript.ID),
"linode_stackscript",
"linode",
[]string{}))
}
}
return resources
}

func (g *StackScriptGenerator) InitResources() error {
client := g.generateClient()
output, err := client.ListStackscripts(context.Background(), nil)
if err != nil {
return err
}
g.Resources = g.createResources(output)
return nil
}

0 comments on commit ccd2a79

Please sign in to comment.