Skip to content

Commit

Permalink
feat: add datastores list under d/datastore_cluster (#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikfot authored Oct 16, 2024
1 parent 92d753b commit d135500
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
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 included in the datastore cluster.",
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{}
childDatastores, err := pod.Children(ctx)
if err != nil {
return fmt.Errorf("error retrieving datastores in datastore cluster: %s", err)
}
for d := range childDatastores {
ds, err := datastore.FromID(client, childDatastores[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
}
45 changes: 43 additions & 2 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 All @@ -66,8 +88,8 @@ resource "vsphere_datastore_cluster" "datastore_cluster" {
}
data "vsphere_datastore_cluster" "datastore_cluster_data" {
name = "${vsphere_datastore_cluster.datastore_cluster.name}"
datacenter_id = "${vsphere_datastore_cluster.datastore_cluster.datacenter_id}"
name = vsphere_datastore_cluster.datastore_cluster.name
datacenter_id = vsphere_datastore_cluster.datastore_cluster.datacenter_id
}
`,
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
Expand All @@ -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"),
)
}
17 changes: 15 additions & 2 deletions website/docs/d/datastore_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,22 @@ The following arguments are supported:
For default datacenters, use the id attribute from an empty
`vsphere_datacenter` data source.


[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider

## Attribute Reference

The only exported attribute from this data source is `id`, which represents the
ID of the datastore cluster.
The following attributes are exported:

* `id` - The [managed objectID][docs-about-morefs] of the vSphere datastore cluster object.

* `datastores` - (Optional) The names of the datastores included in the specific
cluster.

### Example Usage for `datastores` attribute:

```hcl
output "datastores" {
value = data.vsphere_datastore_cluster.datastore_cluster.datastores
}
```

0 comments on commit d135500

Please sign in to comment.