Skip to content

Commit

Permalink
F #227: add cluster datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Apr 29, 2022
1 parent e68c998 commit 4a7cd24
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
85 changes: 85 additions & 0 deletions opennebula/data_opennebula_cluster.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions opennebula/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/cluster.html.markdown
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit 4a7cd24

Please sign in to comment.