Skip to content

Commit

Permalink
Add in-cluster datastores in d/datastore_stats
Browse files Browse the repository at this point in the history
* A full list of all datastores is fetched including
datastores in datastore clusters
* All datastores are displayed under the same capacity
and free capacity lists

Signed-off-by: nikfot <[email protected]>
  • Loading branch information
nikfot committed Oct 3, 2024
1 parent 925fc60 commit 73e7e3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion vsphere/data_source_vsphere_datastore_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package vsphere

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datastore"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/storagepod"
"github.com/vmware/govmomi/object"
)

Expand Down Expand Up @@ -36,6 +38,7 @@ func dataSourceVSphereDatastoreStats() *schema.Resource {
}

func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.Background()
client := meta.(*Client).vimClient
var dc *object.Datacenter
if dcID, ok := d.GetOk("datacenter_id"); ok {
Expand All @@ -49,6 +52,23 @@ func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{
if err != nil {
return fmt.Errorf("error listing datastores: %s", err)
}
storagePods, err := storagepod.List(client)
if err != nil {
return fmt.Errorf("error retrieving storage pods: %s", err)
}
for s := range storagePods {
childDatastores, err := storagePods[s].Children(ctx)
if err != nil {
return fmt.Errorf("error retrieving storage pod children: %s", err)
}
for c := range childDatastores {
ds, err := datastore.FromID(client, childDatastores[c].Reference().Value)
if err != nil {
return fmt.Errorf("error retrieving datastore: %s", err)
}
dss = append(dss, ds)
}
}
for i := range dss {
ds, err := datastore.FromPath(client, dss[i].Name(), dc)
if err != nil {
Expand All @@ -66,6 +86,5 @@ func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{
d.Set("free_space", fr)
}
d.SetId(fmt.Sprintf("%s_stats", dc.Reference().Value))

return nil
}
2 changes: 1 addition & 1 deletion vsphere/data_source_vsphere_datastore_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func testAccDataSourceVSphereDatastoreStatsPreCheck(t *testing.T) {
t.Skip("set TF_VAR_VSPHERE_DATACENTER to run vsphere_datastore_stats acceptance tests")
}
if os.Getenv("VSPHERE_USER") == "" {
t.Skip("set TF_VAR_VSPHERE_DATACENTER to run vsphere_datastore_stats acceptance tests")
t.Skip("set TF_VAR_VSPHERE_USER to run vsphere_datastore_stats acceptance tests")
}
if os.Getenv("VSPHERE_PASSWORD") == "" {
t.Skip("set TF_VAR_VSPHERE_PASSWORD to run vsphere_datastore_stats acceptance tests")
Expand Down

0 comments on commit 73e7e3d

Please sign in to comment.