From 284bfcdd2032ab569d38b39976841775af0cce6f Mon Sep 17 00:00:00 2001 From: nikfot Date: Thu, 3 Oct 2024 15:20:04 +0300 Subject: [PATCH] Add datastores list under d/datastore_cluster * 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 --- ...667e3f1844e81a4a36aedc74ba7ca0f1706595076b | 1 + .../data_source_vsphere_datastore_cluster.go | 26 ++++++++++++ ...a_source_vsphere_datastore_cluster_test.go | 41 +++++++++++++++++++ .../docs/d/datastore_cluster.html.markdown | 2 + 4 files changed, 70 insertions(+) create mode 100644 vsphere/0fd9f4de6e078301eef78c667e3f1844e81a4a36aedc74ba7ca0f1706595076b diff --git a/vsphere/0fd9f4de6e078301eef78c667e3f1844e81a4a36aedc74ba7ca0f1706595076b b/vsphere/0fd9f4de6e078301eef78c667e3f1844e81a4a36aedc74ba7ca0f1706595076b new file mode 100644 index 000000000..0bd2d97b2 --- /dev/null +++ b/vsphere/0fd9f4de6e078301eef78c667e3f1844e81a4a36aedc74ba7ca0f1706595076b @@ -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"}}} diff --git a/vsphere/data_source_vsphere_datastore_cluster.go b/vsphere/data_source_vsphere_datastore_cluster.go index e7fcddd31..8f659b071 100644 --- a/vsphere/data_source_vsphere_datastore_cluster.go +++ b/vsphere/data_source_vsphere_datastore_cluster.go @@ -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 { @@ -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 } diff --git a/vsphere/data_source_vsphere_datastore_cluster_test.go b/vsphere/data_source_vsphere_datastore_cluster_test.go index 5e7eb974c..cd22dc3a6 100644 --- a/vsphere/data_source_vsphere_datastore_cluster_test.go +++ b/vsphere/data_source_vsphere_datastore_cluster_test.go @@ -5,6 +5,7 @@ package vsphere import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper" @@ -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(` @@ -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"), + ) +} diff --git a/website/docs/d/datastore_cluster.html.markdown b/website/docs/d/datastore_cluster.html.markdown index 21f452555..9da31fcf1 100644 --- a/website/docs/d/datastore_cluster.html.markdown +++ b/website/docs/d/datastore_cluster.html.markdown @@ -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