From 4a7cd240638b68a5177b7c05294c3ac3ab45fdd0 Mon Sep 17 00:00:00 2001 From: Pierre Lafievre Date: Fri, 1 Apr 2022 18:49:01 +0200 Subject: [PATCH] F #227: add cluster datasource --- opennebula/data_opennebula_cluster.go | 85 +++++++++++++++++++++++++++ opennebula/provider.go | 1 + website/docs/d/cluster.html.markdown | 30 ++++++++++ 3 files changed, 116 insertions(+) create mode 100644 opennebula/data_opennebula_cluster.go create mode 100644 website/docs/d/cluster.html.markdown diff --git a/opennebula/data_opennebula_cluster.go b/opennebula/data_opennebula_cluster.go new file mode 100644 index 000000000..b6c8dec5d --- /dev/null +++ b/opennebula/data_opennebula_cluster.go @@ -0,0 +1,85 @@ +package opennebula + +import ( + "fmt" + "strconv" + + "github.com/OpenNebula/one/src/oca/go/src/goca" + clusterSc "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/cluster" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataOpennebulaCluster() *schema.Resource { + return &schema.Resource{ + Read: datasourceOpennebulaClusterRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Optional: true, + Description: "Name of the cluster", + }, + "tags": tagsSchema(), + }, + } +} + +func clusterFilter(d *schema.ResourceData, meta interface{}) (*clusterSc.Cluster, error) { + + controller := meta.(*goca.Controller) + + clusters, err := controller.Clusters().Info(false) + if err != nil { + return nil, err + } + + // filter clusters with user defined criterias + name, nameOk := d.GetOk("name") + tagsInterface, tagsOk := d.GetOk("tags") + tags := tagsInterface.(map[string]interface{}) + + match := make([]*clusterSc.Cluster, 0, 1) + for i, cluster := range clusters.Clusters { + + if nameOk && cluster.Name != name { + continue + } + + if tagsOk && !matchTags(cluster.Template.Template, tags) { + continue + } + + match = append(match, &clusters.Clusters[i]) + } + + // check filtering results + if len(match) == 0 { + return nil, fmt.Errorf("no cluster match the tags") + } else if len(match) > 1 { + return nil, fmt.Errorf("several clusters match the tags") + } + + return match[0], nil +} + +func datasourceOpennebulaClusterRead(d *schema.ResourceData, meta interface{}) error { + + cluster, err := clusterFilter(d, meta) + if err != nil { + return err + } + + tplPairs := pairsToMap(cluster.Template.Template) + + d.SetId(strconv.FormatInt(int64(cluster.ID), 10)) + d.Set("name", cluster.Name) + + if len(tplPairs) > 0 { + err := d.Set("tags", tplPairs) + if err != nil { + return err + } + } + + return nil +} diff --git a/opennebula/provider.go b/opennebula/provider.go index 46739e2ca..f023a6e25 100644 --- a/opennebula/provider.go +++ b/opennebula/provider.go @@ -36,6 +36,7 @@ func Provider() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ + "opennebula_cluster": dataOpennebulaCluster(), "opennebula_group": dataOpennebulaGroup(), "opennebula_image": dataOpennebulaImage(), "opennebula_security_group": dataOpennebulaSecurityGroup(), diff --git a/website/docs/d/cluster.html.markdown b/website/docs/d/cluster.html.markdown new file mode 100644 index 000000000..c59148da3 --- /dev/null +++ b/website/docs/d/cluster.html.markdown @@ -0,0 +1,30 @@ +--- +layout: "opennebula" +page_title: "OpenNebula: opennebula_cluster" +sidebar_current: "docs-opennebula-datasource-cluster" +description: |- + Get the cluster information for a given name. +--- + +# opennebula_cluster + +Use this data source to retrieve the cluster information from it's name or tags. + +## Example Usage + +```hcl +data "opennebula_cluster" "ExistingCluster" { + name = "My_Cluster" +} +``` + +## Argument Reference + +* `name` - (Optional) The OpenNebula cluster to retrieve information for. +* `tags` - (Optional) Tags associated to the cluster. + +## Attribute Reference + +* `id` - ID of the cluster. +* `name` - The OpenNebula cluster name. +* `tags` - Tags of the cluster (Key = Value).