-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 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
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 | ||
} |
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,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). |