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 committed Oct 4, 2024
1 parent 925fc60 commit e1fcf80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"SoapClient":{"cookies":[{"Name":"vmware_soap_session","Value":"f787776a8331f57ab523125cdb861d72522184c8","Path":"","Domain":"","Expires":"0001-01-01T00:00:00Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null}],"url":{"Scheme":"https","Opaque":"","User":null,"Host":"vcenter-test.internal.upstreamsystems.com","Path":"/sdk","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":""},"insecure":true,"version":"8.0.3.0","useJSON":false},"ServiceContent":{"rootFolder":{"type":"Folder","value":"group-d1"},"propertyCollector":{"type":"PropertyCollector","value":"propertyCollector"},"viewManager":{"type":"ViewManager","value":"ViewManager"},"about":{"name":"VMware vCenter Server","fullName":"VMware vCenter Server 7.0.3 build-22357613","vendor":"VMware, Inc.","version":"7.0.3","patchLevel":"01700","build":"22357613","localeVersion":"INTL","localeBuild":"000","osType":"linux-x64","productLineId":"vpx","apiType":"VirtualCenter","apiVersion":"7.0.3.0","instanceUuid":"5b012bc9-2892-4ca1-94bb-c8b429729af7","licenseProductName":"VMware VirtualCenter Server","licenseProductVersion":"7.0"},"setting":{"type":"OptionManager","value":"VpxSettings"},"userDirectory":{"type":"UserDirectory","value":"UserDirectory"},"sessionManager":{"type":"SessionManager","value":"SessionManager"},"authorizationManager":{"type":"AuthorizationManager","value":"AuthorizationManager"},"serviceManager":{"type":"ServiceManager","value":"ServiceMgr"},"perfManager":{"type":"PerformanceManager","value":"PerfMgr"},"scheduledTaskManager":{"type":"ScheduledTaskManager","value":"ScheduledTaskManager"},"alarmManager":{"type":"AlarmManager","value":"AlarmManager"},"eventManager":{"type":"EventManager","value":"EventManager"},"taskManager":{"type":"TaskManager","value":"TaskManager"},"extensionManager":{"type":"ExtensionManager","value":"ExtensionManager"},"customizationSpecManager":{"type":"CustomizationSpecManager","value":"CustomizationSpecManager"},"guestCustomizationManager":{"type":"VirtualMachineGuestCustomizationManager","value":"GuestCustomizationManager"},"customFieldsManager":{"type":"CustomFieldsManager","value":"CustomFieldsManager"},"diagnosticManager":{"type":"DiagnosticManager","value":"DiagMgr"},"licenseManager":{"type":"LicenseManager","value":"LicenseManager"},"searchIndex":{"type":"SearchIndex","value":"SearchIndex"},"fileManager":{"type":"FileManager","value":"FileManager"},"datastoreNamespaceManager":{"type":"DatastoreNamespaceManager","value":"DatastoreNamespaceManager"},"virtualDiskManager":{"type":"VirtualDiskManager","value":"virtualDiskManager"},"snmpSystem":{"type":"HostSnmpSystem","value":"SnmpSystem"},"vmProvisioningChecker":{"type":"VirtualMachineProvisioningChecker","value":"ProvChecker"},"vmCompatibilityChecker":{"type":"VirtualMachineCompatibilityChecker","value":"CompatChecker"},"ovfManager":{"type":"OvfManager","value":"OvfManager"},"ipPoolManager":{"type":"IpPoolManager","value":"IpPoolManager"},"dvSwitchManager":{"type":"DistributedVirtualSwitchManager","value":"DVSManager"},"hostProfileManager":{"type":"HostProfileManager","value":"HostProfileManager"},"clusterProfileManager":{"type":"ClusterProfileManager","value":"ClusterProfileManager"},"complianceManager":{"type":"ProfileComplianceManager","value":"MoComplianceManager"},"localizationManager":{"type":"LocalizationManager","value":"LocalizationManager"},"storageResourceManager":{"type":"StorageResourceManager","value":"StorageResourceManager"},"guestOperationsManager":{"type":"GuestOperationsManager","value":"guestOperationsManager"},"overheadMemoryManager":{"type":"OverheadMemoryManager","value":"OverheadMemoryManager"},"certificateManager":{"type":"CertificateManager","value":"certificateManager"},"ioFilterManager":{"type":"IoFilterManager","value":"IoFilterManager"},"vStorageObjectManager":{"type":"VcenterVStorageObjectManager","value":"VStorageObjectManager"},"hostSpecManager":{"type":"HostSpecificationManager","value":"HostSpecificationManager"},"cryptoManager":{"type":"CryptoManagerKmip","value":"CryptoManager"},"healthUpdateManager":{"type":"HealthUpdateManager","value":"HealthUpdateManager"},"failoverClusterConfigurator":{"type":"FailoverClusterConfigurator","value":"FailoverClusterConfigurator"},"failoverClusterManager":{"type":"FailoverClusterManager","value":"FailoverClusterManager"},"tenantManager":{"type":"TenantTenantManager","value":"TenantManager-22357613"},"siteInfoManager":{"type":"SiteInfoManager","value":"SiteInfoManager"},"storageQueryManager":{"type":"StorageQueryManager","value":"StorageQueryManager"}}}
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
}
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 e1fcf80

Please sign in to comment.