-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters