Skip to content

Commit

Permalink
Add disk stats for datastore
Browse files Browse the repository at this point in the history
Add total capacity and free space for datastore.
This way more criteria can be added to datastore
selection

Add list of datastores with free space and total
capacity.

Signed-off-by: nikfot <nik_fot@hotmail.gr>
  • Loading branch information
nikfot committed May 5, 2023
1 parent ccb5a7e commit 82c6d6e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Binary file added terraform-provider-vsphere
Binary file not shown.
10 changes: 10 additions & 0 deletions vsphere/data_source_vsphere_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func dataSourceVSphereDatastore() *schema.Resource {
Description: "The managed object ID of the datacenter the datastore is in. This is not required when using ESXi directly, or if there is only one datacenter in your infrastructure.",
Optional: true,
},
"stats": {
Type: schema.TypeMap,
Description: "The usage stats of the datastore, include total capacity and free space in bytes.",
Optional: true,
},
},
}
}
Expand All @@ -48,5 +53,10 @@ func dataSourceVSphereDatastoreRead(d *schema.ResourceData, meta interface{}) er
}

d.SetId(ds.Reference().Value)
props, err := datastore.Properties(ds)
if err != nil {
return fmt.Errorf("error getting properties for datastore ID %q: %s", ds.Reference().Value, err)
}
d.Set("stats", map[string]string{"capacity": fmt.Sprintf("%v", props.Summary.Capacity), "free": fmt.Sprintf("%v", props.Summary.FreeSpace)})
return nil
}
71 changes: 71 additions & 0 deletions vsphere/data_source_vsphere_datastore_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package vsphere

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datastore"
"github.com/vmware/govmomi/object"
)

func dataSourceVSphereDatastoreStats() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereDatastoreStatsRead,

Schema: map[string]*schema.Schema{
"datacenter_id": {
Type: schema.TypeString,
Description: "The managed object ID of the datacenter to get datastores from.",
Required: true,
},
"free_space": {
Type: schema.TypeMap,
Description: "The free space of the datastores.",
Optional: true,
},
"capacity": {
Type: schema.TypeMap,
Description: "The capacity of the datastores.",
Optional: true,
},
},
}
}

func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).vimClient
var dc *object.Datacenter
if dcID, ok := d.GetOk("datacenter_id"); ok {
var err error
dc, err = datacenterFromID(client, dcID.(string))
if err != nil {
return fmt.Errorf("cannot locate datacenter: %s", err)
}
}
dss, err := datastore.List(client)
if err != nil {
return fmt.Errorf("error listing datastores: %s", err)
}
for i := range dss {
ds, err := datastore.FromPath(client, dss[i].Name(), dc)
if err != nil {
return fmt.Errorf("error fetching datastore: %s", err)
}
props, err := datastore.Properties(ds)
if err != nil {
return fmt.Errorf("error getting properties for datastore ID %q: %s", ds.Reference().Value, err)
}
cap := d.Get("capacity").(map[string]interface{})
cap[dss[i].Name()] = fmt.Sprintf("%v", props.Summary.Capacity)
d.Set("capacity", cap)
fr := d.Get("free_space").(map[string]interface{})
fr[dss[i].Name()] = fmt.Sprintf("%v", props.Summary.FreeSpace)
d.Set("free_space", fr)
}
d.SetId(fmt.Sprintf("%s_stats", dc.Reference().Value))

return nil
}
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func Provider() *schema.Provider {
"vsphere_datacenter": dataSourceVSphereDatacenter(),
"vsphere_datastore": dataSourceVSphereDatastore(),
"vsphere_datastore_cluster": dataSourceVSphereDatastoreCluster(),
"vsphere_datastore_stats": dataSourceVSphereDatastoreStats(),
"vsphere_distributed_virtual_switch": dataSourceVSphereDistributedVirtualSwitch(),
"vsphere_dynamic": dataSourceVSphereDynamic(),
"vsphere_folder": dataSourceVSphereFolder(),
Expand Down

0 comments on commit 82c6d6e

Please sign in to comment.