From dd4d630598f1a22e3b7e830fab2674b4121f7745 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Wed, 30 Dec 2020 20:19:57 -0800 Subject: [PATCH] Add the start of the xenorchestra implementation (cherry picked from commit 03999bce04c47c95957b2d569307c3ab9e80763f) --- README.md | 2 + cmd/provider_cmd_xenorchestra.go | 44 +++++++++ cmd/root.go | 2 + providers/xenorchestra/acls.go | 48 ++++++++++ .../xenorchestra/xenorchestra_provider.go | 91 +++++++++++++++++++ .../xenorchestra/xenorchestra_service.go | 33 +++++++ 6 files changed, 220 insertions(+) create mode 100644 cmd/provider_cmd_xenorchestra.go create mode 100644 providers/xenorchestra/acls.go create mode 100644 providers/xenorchestra/xenorchestra_provider.go create mode 100644 providers/xenorchestra/xenorchestra_service.go diff --git a/README.md b/README.md index 16d6d8dde..ad55b874e 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ A CLI tool that generates `tf`/`json` and `tfstate` files based on existing infr * [Logz.io](#use-with-logzio) * [Commercetools](#use-with-commercetools) * [Mikrotik](#use-with-mikrotik) + * [Xen Orchestra](#use-with-xenorchestra) * [GmailFilter](#use-with-gmailfilter) - [Contributing](#contributing) - [Developing](#developing) @@ -264,6 +265,7 @@ Links to download Terraform Providers: * Logz.io provider >=1.1.1 - [here](https://github.com/jonboydell/logzio_terraform_provider/) * Commercetools provider >= 0.21.0 - [here](https://github.com/labd/terraform-provider-commercetools) * Mikrotik provider >= 0.2.2 - [here](https://github.com/ddelnano/terraform-provider-mikrotik) + * Xen Orchestra provider >= 0.XX.XX - [here](https://github.com/ddelnano/terraform-provider-xenorchestra) * GmailFilter provider >= 1.0.1 - [here](https://github.com/yamamoto-febc/terraform-provider-gmailfilter) Information on provider plugins: diff --git a/cmd/provider_cmd_xenorchestra.go b/cmd/provider_cmd_xenorchestra.go new file mode 100644 index 000000000..b38c90ab6 --- /dev/null +++ b/cmd/provider_cmd_xenorchestra.go @@ -0,0 +1,44 @@ +// 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 ( + xenorchestra_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/xenorchestra" + "github.com/GoogleCloudPlatform/terraformer/terraformutils" + "github.com/spf13/cobra" +) + +func newCmdXenorchestraImporter(options ImportOptions) *cobra.Command { + cmd := &cobra.Command{ + Use: "xenorchestra", + Short: "Import current state to Terraform configuration from Xen Orchestra", + Long: "Import current state to Terraform configuration from Xen Orchestra", + RunE: func(cmd *cobra.Command, args []string) error { + provider := newXenorchestraProvider() + err := Import(provider, options, []string{}) + if err != nil { + return err + } + return nil + }, + } + + cmd.AddCommand(listCmd(newXenorchestraProvider())) + baseProviderFlags(cmd.PersistentFlags(), &options, "instance", "acl=name1:name2:name3") + return cmd +} + +func newXenorchestraProvider() terraformutils.ProviderGenerator { + return &xenorchestra_terraforming.XenorchestraProvider{} +} diff --git a/cmd/root.go b/cmd/root.go index c22ad4bc0..74eb758dc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -70,6 +70,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command newCmdLogzioImporter, newCmdCommercetoolsImporter, newCmdMikrotikImporter, + newCmdXenorchestraImporter, newCmdGmailfilterImporter, } } @@ -108,6 +109,7 @@ func providerGenerators() map[string]func() terraformutils.ProviderGenerator { newLogzioProvider, newCommercetoolsProvider, newMikrotikProvider, + newXenorchestraProvider, newGmailfilterProvider, } { list[providerGen().GetName()] = providerGen diff --git a/providers/xenorchestra/acls.go b/providers/xenorchestra/acls.go new file mode 100644 index 000000000..0ea57a5c7 --- /dev/null +++ b/providers/xenorchestra/acls.go @@ -0,0 +1,48 @@ +// 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 xenorchestra + +import ( + "github.com/GoogleCloudPlatform/terraformer/terraformutils" + "github.com/ddelnano/terraform-provider-xenorchestra/client" +) + +type AclGenerator struct { + XenorchestraService +} + +func (g AclGenerator) createResources(acls []client.Acl) []terraformutils.Resource { + var resources []terraformutils.Resource + for _, acl := range acls { + resourceName := acl.Id + resources = append(resources, terraformutils.NewSimpleResource( + acl.Id, + resourceName, + "xenorchestra_acl", + "xenorchestra", + []string{})) + } + return resources +} + +func (g *AclGenerator) InitResources() error { + client := g.generateClient() + acls, err := client.GetAcls() + + if err != nil { + return err + } + g.Resources = g.createResources(acls) + return nil +} diff --git a/providers/xenorchestra/xenorchestra_provider.go b/providers/xenorchestra/xenorchestra_provider.go new file mode 100644 index 000000000..9bc8d9f7e --- /dev/null +++ b/providers/xenorchestra/xenorchestra_provider.go @@ -0,0 +1,91 @@ +// 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 xenorchestra + +import ( + "errors" + "os" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type XenorchestraProvider struct { //nolint + terraformutils.Provider + url string + user string + password string +} + +func (p *XenorchestraProvider) Init(args []string) error { + if os.Getenv("XOA_URL") == "" { + return errors.New("set XOA_URL env var") + } + p.url = os.Getenv("XOA_URL") + + if os.Getenv("XOA_USER") == "" { + return errors.New("set XOA_USER env var") + } + p.user = os.Getenv("XOA_USER") + + if os.Getenv("XOA_PASSWORD") == "" { + return errors.New("set XOA_PASSWORD env var") + } + p.password = os.Getenv("XOA_PASSWORD") + + return nil +} + +func (p *XenorchestraProvider) GetName() string { + return "xenorchestra" +} + +func (p *XenorchestraProvider) GetProviderData(arg ...string) map[string]interface{} { + return map[string]interface{}{ + "provider": map[string]interface{}{ + "xenorchestra": map[string]interface{}{ + "url": p.url, + "username": p.user, + "password": p.password, + }, + }, + } +} + +func (XenorchestraProvider) GetResourceConnections() map[string]map[string][]string { + return map[string]map[string][]string{} +} + +func (p *XenorchestraProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { + return map[string]terraformutils.ServiceGenerator{ + "acl": &AclGenerator{}, + } +} + +func (p *XenorchestraProvider) InitService(serviceName string, verbose bool) error { + var isSupported bool + if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported { + return errors.New("xenorchestra: " + serviceName + " not supported service") + } + p.Service = p.GetSupportedService()[serviceName] + p.Service.SetName(serviceName) + p.Service.SetVerbose(verbose) + p.Service.SetProviderName(p.GetName()) + p.Service.SetArgs(map[string]interface{}{ + "url": p.url, + "username": p.user, + "password": p.password, + }) + return nil +} diff --git a/providers/xenorchestra/xenorchestra_service.go b/providers/xenorchestra/xenorchestra_service.go new file mode 100644 index 000000000..fac2c0a93 --- /dev/null +++ b/providers/xenorchestra/xenorchestra_service.go @@ -0,0 +1,33 @@ +// 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 xenorchestra + +import ( + "github.com/GoogleCloudPlatform/terraformer/terraformutils" + "github.com/ddelnano/terraform-provider-xenorchestra/client" +) + +type XenorchestraService struct { //nolint + terraformutils.Service +} + +func (m *XenorchestraService) generateClient() *client.Client { + config := client.Config{ + Url: m.Args["url"].(string), + Username: m.Args["username"].(string), + Password: m.Args["password"].(string), + } + client, _ := client.NewClient(config) + return client +}