Skip to content

Commit

Permalink
fixes #345: introduce data-source for hosts in cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
eslutsky committed May 17, 2022
1 parent 3e6bc6e commit 13c834a
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/data-sources/cluster_hosts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "ovirt_cluster_hosts Data Source - terraform-provider-ovirt"
subcategory: ""
description: |-
A set of all hosts of a Cluster.
---

# ovirt_cluster_hosts (Data Source)

A set of all hosts of a Cluster.

## Example Usage

```terraform
data "ovirt_cluster_hosts" "list" {
cluster_id = var.cluster_id
}
output "attachment_set" {
value = data.ovirt_cluster_hosts.list
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cluster_id` (String) oVirt cluster ID in the Data Center.

### Read-Only

- `hosts` (Set of Object) (see [below for nested schema](#nestedatt--hosts))
- `id` (String) The ID of this resource.

<a id="nestedatt--hosts"></a>
### Nested Schema for `hosts`

Read-Only:

- `id` (String)
- `status` (String)


7 changes: 7 additions & 0 deletions examples/data-sources/ovirt_cluster_hosts/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "ovirt_cluster_hosts" "list" {
cluster_id = var.cluster_id
}

output "attachment_set" {
value = data.ovirt_cluster_hosts.list
}
22 changes: 22 additions & 0 deletions examples/data-sources/ovirt_cluster_hosts/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terraform {
required_providers {
ovirt = {
source = "ovirt/ovirt"
}
}

required_version = ">= 0.15"
}

provider "ovirt" {
url = var.url
username = var.username
password = var.password
tls_ca_bundle = var.tls_ca_bundle
tls_system = var.tls_system
tls_ca_dirs = var.tls_ca_dirs
tls_ca_files = var.tls_ca_files
tls_insecure = var.tls_insecure
mock = var.mock
}

44 changes: 44 additions & 0 deletions examples/data-sources/ovirt_cluster_hosts/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
variable "storagedomain_id" {
type = string
description = "ID of the storage domain to create the disk on."
}

variable "cluster_id" {
type = string
}

variable "username" {
type = string
}
variable "password" {
type = string
}
variable "url" {
type = string
}
variable "tls_ca_files" {
type = list(string)
default = []
}
variable "tls_ca_dirs" {
type = list(string)
default = []
}
variable "tls_insecure" {
type = bool
default = false
}
variable "tls_ca_bundle" {
type = string
default = ""
}
variable "tls_system" {
type = bool
default = true
description = "Take TLS CA certificates from system root. Does not work on Windows."
}

variable "mock" {
type = bool
default = true
}
74 changes: 74 additions & 0 deletions ovirt/data_source_ovirt_cluster_hosts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ovirt

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func (p *provider) clusterHostsDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: p.clusterHostsDataSourceRead,
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: "oVirt cluster ID in the Data Center.",
ValidateDiagFunc: validateUUID,
},
"hosts": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the host.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "status of the host.",
},
},
},
},
},
Description: `A set of all hosts of a Cluster.`,
}
}

func (p *provider) clusterHostsDataSourceRead(
ctx context.Context,
data *schema.ResourceData,
_ interface{},
) diag.Diagnostics {
client := p.client.WithContext(ctx)
clusterID := data.Get("cluster_id").(string)
allHosts, err := client.ListHosts()

if err != nil {
return errorToDiags("list all hosts", err)
}

hosts := make([]map[string]interface{}, 0)

for _, host := range allHosts {
if string(host.ClusterID()) == clusterID {
hostMap := make(map[string]interface{}, 0)
hostMap["id"] = host.ID()
hostMap["status"] = host.Status()
hosts = append(hosts, hostMap)
}
}

if err := data.Set("hosts", hosts); err != nil {
return errorToDiags("set hosts", err)
}

data.SetId(clusterID)

return nil
}
51 changes: 51 additions & 0 deletions ovirt/data_source_ovirt_cluster_hosts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ovirt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestClusterHostsDataSource(t *testing.T) {
p := newProvider(newTestLogger(t))

config :=
fmt.Sprintf(`
provider "ovirt" {
mock = true
}
data "ovirt_cluster_hosts" "list" {
cluster_id = "%s"
}
output "hosts_list" {
value = data.ovirt_cluster_hosts.list
}`,
p.getTestHelper().GetClusterID())

resource.UnitTest(t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
func(s *terraform.State) error {
v := s.RootModule().Outputs["hosts_list"].Value.(map[string]interface{})
hosts, ok := v["hosts"]
if !ok {
return fmt.Errorf("missing key 'hosts' in output")
}
hostSize := len(hosts.([]interface{}))
if hostSize != 1 {
return fmt.Errorf("expected 1 hosts, but got only %d", hostSize)
}
return nil
},
),
},
},
})
}
1 change: 1 addition & 0 deletions ovirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (p *provider) getProvider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"ovirt_disk_attachments": p.diskAttachmentsDataSource(),
"ovirt_cluster_hosts": p.clusterHostsDataSource(),
},
}
}
Expand Down

0 comments on commit 13c834a

Please sign in to comment.