From 47b8fdf4d65700f6a9ed7f893b056e8c9cda03ec Mon Sep 17 00:00:00 2001 From: Evgeny Slutsky Date: Tue, 17 May 2022 10:44:23 +0300 Subject: [PATCH] fixes #345: introduce data-source for hosts in cluster --- ovirt/data_source_ovirt_cluster_hosts.go | 74 +++++++++++++++++++ ovirt/data_source_ovirt_cluster_hosts_test.go | 51 +++++++++++++ ovirt/provider.go | 1 + 3 files changed, 126 insertions(+) create mode 100644 ovirt/data_source_ovirt_cluster_hosts.go create mode 100644 ovirt/data_source_ovirt_cluster_hosts_test.go diff --git a/ovirt/data_source_ovirt_cluster_hosts.go b/ovirt/data_source_ovirt_cluster_hosts.go new file mode 100644 index 00000000..8e43d5e1 --- /dev/null +++ b/ovirt/data_source_ovirt_cluster_hosts.go @@ -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 +} diff --git a/ovirt/data_source_ovirt_cluster_hosts_test.go b/ovirt/data_source_ovirt_cluster_hosts_test.go new file mode 100644 index 00000000..c47b9dc6 --- /dev/null +++ b/ovirt/data_source_ovirt_cluster_hosts_test.go @@ -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 + }, + ), + }, + }, + }) +} diff --git a/ovirt/provider.go b/ovirt/provider.go index 5f4d914f..5adf2adb 100644 --- a/ovirt/provider.go +++ b/ovirt/provider.go @@ -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(), }, } }