Skip to content

Commit

Permalink
Add datastores list under d/datastore_cluster
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nikfot committed Oct 3, 2024
1 parent 925fc60 commit 284bfcd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"SoapClient":{"cookies":[{"Name":"vmware_soap_session","Value":"0f7458cffb02add2ce2b26279997c849f24d47a1","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"}}}
26 changes: 26 additions & 0 deletions vsphere/data_source_vsphere_datastore_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
41 changes: 41 additions & 0 deletions vsphere/data_source_vsphere_datastore_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vsphere

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper"
Expand Down Expand Up @@ -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(`
Expand Down Expand Up @@ -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"),
)
}
2 changes: 2 additions & 0 deletions website/docs/d/datastore_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 284bfcd

Please sign in to comment.