Skip to content

Commit

Permalink
Add the start of the xenorchestra implementation
Browse files Browse the repository at this point in the history
(cherry picked from commit 03999bc)
  • Loading branch information
ddelnano committed Mar 31, 2021
1 parent 30e5d07 commit dd4d630
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
44 changes: 44 additions & 0 deletions cmd/provider_cmd_xenorchestra.go
Original file line number Diff line number Diff line change
@@ -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{}
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command
newCmdLogzioImporter,
newCmdCommercetoolsImporter,
newCmdMikrotikImporter,
newCmdXenorchestraImporter,
newCmdGmailfilterImporter,
}
}
Expand Down Expand Up @@ -108,6 +109,7 @@ func providerGenerators() map[string]func() terraformutils.ProviderGenerator {
newLogzioProvider,
newCommercetoolsProvider,
newMikrotikProvider,
newXenorchestraProvider,
newGmailfilterProvider,
} {
list[providerGen().GetName()] = providerGen
Expand Down
48 changes: 48 additions & 0 deletions providers/xenorchestra/acls.go
Original file line number Diff line number Diff line change
@@ -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
}
91 changes: 91 additions & 0 deletions providers/xenorchestra/xenorchestra_provider.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions providers/xenorchestra/xenorchestra_service.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit dd4d630

Please sign in to comment.