-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from aqche/add_linode_support
Support Linode provider
- Loading branch information
Showing
14 changed files
with
557 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 cmd | ||
|
||
import ( | ||
linode_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/linode" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdLinodeImporter(options ImportOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "linode", | ||
Short: "Import current state to Terraform configuration from Linode", | ||
Long: "Import current state to Terraform configuration from Linode", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
provider := newLinodeProvider() | ||
err := Import(provider, options, []string{}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.AddCommand(listCmd(newLinodeProvider())) | ||
cmd.PersistentFlags().BoolVarP(&options.Connect, "connect", "c", true, "") | ||
cmd.PersistentFlags().StringSliceVarP(&options.Resources, "resources", "r", []string{}, "instance") | ||
cmd.PersistentFlags().StringVarP(&options.PathPattern, "path-pattern", "p", DefaultPathPattern, "{output}/{provider}/custom/{service}/") | ||
cmd.PersistentFlags().StringVarP(&options.PathOutput, "path-output", "o", DefaultPathOutput, "") | ||
cmd.PersistentFlags().StringVarP(&options.State, "state", "s", DefaultState, "local or bucket") | ||
cmd.PersistentFlags().StringVarP(&options.Bucket, "bucket", "b", "", "gs://terraform-state") | ||
cmd.PersistentFlags().StringSliceVarP(&options.Filter, "filter", "f", []string{}, "linode_instance=name1:name2:name3") | ||
|
||
return cmd | ||
} | ||
|
||
func newLinodeProvider() terraform_utils.ProviderGenerator { | ||
return &linode_terraforming.LinodeProvider{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 DomainGenerator struct { | ||
LinodeService | ||
} | ||
|
||
func (g DomainGenerator) createResources(domainList []linodego.Domain) []terraform_utils.Resource { | ||
var resources []terraform_utils.Resource | ||
for _, domain := range domainList { | ||
resources = append(resources, terraform_utils.NewSimpleResource( | ||
strconv.Itoa(domain.ID), | ||
strconv.Itoa(domain.ID), | ||
"linode_domain", | ||
"linode", | ||
[]string{})) | ||
} | ||
return resources | ||
} | ||
|
||
func (g *DomainGenerator) InitResources() error { | ||
client := g.generateClient() | ||
output, err := client.ListDomains(context.Background(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
g.Resources = g.createResources(output) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ImageGenerator struct { | ||
LinodeService | ||
} | ||
|
||
func (g ImageGenerator) createResources(imageList []linodego.Image) []terraform_utils.Resource { | ||
var resources []terraform_utils.Resource | ||
for _, image := range imageList { | ||
resources = append(resources, terraform_utils.NewSimpleResource( | ||
image.ID, | ||
image.ID, | ||
"linode_image", | ||
"linode", | ||
[]string{})) | ||
} | ||
return resources | ||
} | ||
|
||
func (g *ImageGenerator) InitResources() error { | ||
client := g.generateClient() | ||
output, err := client.ListImages(context.Background(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
g.Resources = g.createResources(output) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 InstanceGenerator struct { | ||
LinodeService | ||
} | ||
|
||
func (g InstanceGenerator) createResources(instanceList []linodego.Instance) []terraform_utils.Resource { | ||
var resources []terraform_utils.Resource | ||
for _, instance := range instanceList { | ||
resources = append(resources, terraform_utils.NewSimpleResource( | ||
strconv.Itoa(instance.ID), | ||
strconv.Itoa(instance.ID), | ||
"linode_instance", | ||
"linode", | ||
[]string{})) | ||
} | ||
return resources | ||
} | ||
|
||
func (g *InstanceGenerator) InitResources() error { | ||
client := g.generateClient() | ||
output, err := client.ListInstances(context.Background(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
g.Resources = g.createResources(output) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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 ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils" | ||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils/provider_wrapper" | ||
) | ||
|
||
type LinodeProvider struct { | ||
terraform_utils.Provider | ||
token string | ||
} | ||
|
||
func (p *LinodeProvider) Init(args []string) error { | ||
if os.Getenv("LINODE_TOKEN") == "" { | ||
return errors.New("set LINODE_TOKEN env var") | ||
} | ||
p.token = os.Getenv("LINODE_TOKEN") | ||
|
||
return nil | ||
} | ||
|
||
func (p *LinodeProvider) GetName() string { | ||
return "linode" | ||
} | ||
|
||
func (p *LinodeProvider) GetProviderData(arg ...string) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"provider": map[string]interface{}{ | ||
"linode": map[string]interface{}{ | ||
"version": provider_wrapper.GetProviderVersion(p.GetName()), | ||
"token": p.token, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (LinodeProvider) GetResourceConnections() map[string]map[string][]string { | ||
return map[string]map[string][]string{} | ||
} | ||
|
||
func (p *LinodeProvider) GetSupportedService() map[string]terraform_utils.ServiceGenerator { | ||
return map[string]terraform_utils.ServiceGenerator{ | ||
"domain": &DomainGenerator{}, | ||
"image": &ImageGenerator{}, | ||
"instance": &InstanceGenerator{}, | ||
"nodebalancer": &NodeBalancerGenerator{}, | ||
"sshkey": &SSHKeyGenerator{}, | ||
"token": &TokenGenerator{}, | ||
"volume": &VolumeGenerator{}, | ||
} | ||
} | ||
|
||
func (p *LinodeProvider) InitService(serviceName string) error { | ||
var isSupported bool | ||
if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported { | ||
return errors.New("linode: " + serviceName + " not supported service") | ||
} | ||
p.Service = p.GetSupportedService()[serviceName] | ||
p.Service.SetName(serviceName) | ||
p.Service.SetProviderName(p.GetName()) | ||
p.Service.SetArgs(map[string]interface{}{ | ||
"token": p.token, | ||
}) | ||
return nil | ||
} |
Oops, something went wrong.