Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add in-cluster datastores in d/datastore_stats #2273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
}
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"))
}