Skip to content

Commit

Permalink
feat: 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 authored and tenthirtyam committed Oct 16, 2024
1 parent d135500 commit 55b667b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 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 datastores in datastore cluster: %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
}
14 changes: 7 additions & 7 deletions vsphere/data_source_vsphere_datastore_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func TestAccDataSourceVSphereDatastoreStats_basic(t *testing.T) {
Config: testAccDataSourceVSphereDatastoreStatsConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.vsphere_datastore_stats.datastore_stats", "datacenter_id", os.Getenv("VSPHERE_DATACENTER"),
"data.vsphere_datastore_stats.datastore_stats", "datacenter_id", os.Getenv("TF_VAR_VSPHERE_DATACENTER"),
),
resource.TestCheckResourceAttr(
"data.vsphere_datastore_stats.datastore_stats", "id", fmt.Sprintf("%s_stats", os.Getenv("VSPHERE_DATACENTER")),
"data.vsphere_datastore_stats.datastore_stats", "id", fmt.Sprintf("%s_stats", os.Getenv("TF_VAR_VSPHERE_DATACENTER")),
),
testCheckOutputBool("found_free_space", "true"),
testCheckOutputBool("found_capacity", "true"),
Expand All @@ -40,13 +40,13 @@ func TestAccDataSourceVSphereDatastoreStats_basic(t *testing.T) {
}

func testAccDataSourceVSphereDatastoreStatsPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_DATACENTER") == "" {
if os.Getenv("TF_VAR_VSPHERE_DATACENTER") == "" {
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")
if os.Getenv("TF_VAR_VSPHERE_USER") == "" {
t.Skip("set TF_VAR_VSPHERE_USER to run vsphere_datastore_stats acceptance tests")
}
if os.Getenv("VSPHERE_PASSWORD") == "" {
if os.Getenv("TF_VAR_VSPHERE_PASSWORD") == "" {
t.Skip("set TF_VAR_VSPHERE_PASSWORD to run vsphere_datastore_stats acceptance tests")
}
}
Expand Down Expand Up @@ -82,5 +82,5 @@ output "capacity_values_exist" {
free >= 1
])
}
`, os.Getenv("VSPHERE_DATACENTER"))
`, os.Getenv("TF_VAR_VSPHERE_DATACENTER"))
}

0 comments on commit 55b667b

Please sign in to comment.