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 b81a879 commit 47b8fdf
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
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 47b8fdf

Please sign in to comment.