-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #345: introduce data-source for hosts in cluster
- Loading branch information
Showing
7 changed files
with
244 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,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) | ||
|
||
|
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,7 @@ | ||
data "ovirt_cluster_hosts" "list" { | ||
cluster_id = var.cluster_id | ||
} | ||
|
||
output "attachment_set" { | ||
value = data.ovirt_cluster_hosts.list | ||
} |
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,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 | ||
} | ||
|
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
}, | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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