From 28583f502bcfbdaaaaf3465de29d865645a3c524 Mon Sep 17 00:00:00 2001 From: nikfot Date: Thu, 3 Oct 2024 15:20:04 +0300 Subject: [PATCH] feat: add datastores list under d/datastore_cluster * Enhance d/datastore_cluster to return a list with the names of data stores under the specific cluster * Add the attribute datastores under the datastore_cluster Signed-off-by: nikfot --- .../data_source_vsphere_datastore_cluster.go | 26 ++++++++++++ ...a_source_vsphere_datastore_cluster_test.go | 41 +++++++++++++++++++ .../docs/d/datastore_cluster.html.markdown | 2 + 3 files changed, 69 insertions(+) diff --git a/vsphere/data_source_vsphere_datastore_cluster.go b/vsphere/data_source_vsphere_datastore_cluster.go index e7fcddd31..8f659b071 100644 --- a/vsphere/data_source_vsphere_datastore_cluster.go +++ b/vsphere/data_source_vsphere_datastore_cluster.go @@ -4,9 +4,11 @@ package vsphere import ( + "context" "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datastore" ) func dataSourceVSphereDatastoreCluster() *schema.Resource { @@ -24,15 +26,39 @@ func dataSourceVSphereDatastoreCluster() *schema.Resource { Optional: true, Description: "The managed object ID of the datacenter the cluster is located in. Not required if using an absolute path.", }, + "datastores": { + Type: schema.TypeSet, + Computed: true, + Description: "The names of datastores in the datastorecluster.", + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } func dataSourceVSphereDatastoreClusterRead(d *schema.ResourceData, meta interface{}) error { + ctx := context.Background() + client := meta.(*Client).vimClient pod, err := resourceVSphereDatastoreClusterGetPodFromPath(meta, d.Get("name").(string), d.Get("datacenter_id").(string)) if err != nil { return fmt.Errorf("error loading datastore cluster: %s", err) } d.SetId(pod.Reference().Value) + dsNames := []string{} + podDatastores, err := pod.Children(ctx) + if err != nil { + return fmt.Errorf("error retrieving datastores in datastore cluster: %s", err) + } + for d := range podDatastores { + ds, err := datastore.FromID(client, podDatastores[d].Reference().Value) + if err != nil { + return fmt.Errorf("error retrieving datastore: %s", err) + } + dsNames = append(dsNames, ds.Name()) + } + err = d.Set("datastores", dsNames) + if err != nil { + return fmt.Errorf("cannot set datastores: %s", err) + } return nil } diff --git a/vsphere/data_source_vsphere_datastore_cluster_test.go b/vsphere/data_source_vsphere_datastore_cluster_test.go index 5e7eb974c..cd22dc3a6 100644 --- a/vsphere/data_source_vsphere_datastore_cluster_test.go +++ b/vsphere/data_source_vsphere_datastore_cluster_test.go @@ -5,6 +5,7 @@ package vsphere import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper" @@ -55,6 +56,27 @@ func TestAccDataSourceVSphereDatastoreCluster_absolutePathNoDatacenter(t *testin }, }) } +func TestAccDataSourceVSphereDatastoreCluster_getDatastores(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + RunSweepers() + testAccPreCheck(t) + testAccResourceVSphereDatastoreClusterPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceVSphereDatastoreClusterGetDatastores(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair( + "data.vsphere_datastore_cluster.datastore_cluster_data", "id", + "vsphere_datastore_cluster.datastore_cluster", "id", + ), + ), + }, + }, + }) +} func testAccDataSourceVSphereDatastoreClusterConfigBasic() string { return fmt.Sprintf(` @@ -90,3 +112,22 @@ data "vsphere_datastore_cluster" "datastore_cluster_data" { testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()), ) } + +func testAccDataSourceVSphereDatastoreClusterGetDatastores() string { + return fmt.Sprintf(` +resource "vsphere_datastore_cluster" "datastore_cluster" { + name = "%s" + datacenter_id = "%s" +} + +data "vsphere_datastore_cluster" "datastore_cluster_data" { + name = "${vsphere_datastore_cluster.datastore_cluster.name}" + datacenter_id = "${vsphere_datastore_cluster.datastore_cluster.datacenter_id}" +} + +output "found_datastores" { + value = "${length(data.vsphere_datastore_cluster.datastore_cluster_data.datastores) >= 1 ? "true" : "false" }" +} +`, os.Getenv("TF_VAR_VSPHERE_DATASTORE_CLUSTER_NAME"), os.Getenv("TF_VAR_VSPHERE_DATACENTER_ID"), + ) +} diff --git a/website/docs/d/datastore_cluster.html.markdown b/website/docs/d/datastore_cluster.html.markdown index 21f452555..9da31fcf1 100644 --- a/website/docs/d/datastore_cluster.html.markdown +++ b/website/docs/d/datastore_cluster.html.markdown @@ -44,6 +44,8 @@ The following arguments are supported: This can be omitted if the search path used in `name` is an absolute path. For default datacenters, use the id attribute from an empty `vsphere_datacenter` data source. +* `datastores` - (Optional) The names of the datastores included in the specific + cluster. [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider