-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster_compute_resource_helper.go
52 lines (45 loc) · 1.94 KB
/
cluster_compute_resource_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package clustercomputeresource
import (
"context"
"fmt"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/provider"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/taskstatus"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
)
func PropertiesFromPath(client *govmomi.Client, dc *object.Datacenter, pool string, status *taskstatus.Status) (clusterProp *mo.ClusterComputeResource, err error) {
clusteOB, err := FromPath(client, pool, dc, status)
if err != nil {
return
}
clusterProp, err = Properties(clusteOB)
return
}
// code borrowed from "github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/clustercomputeresource"
// FromPath loads a ClusterComputeResource from its path. The datacenter is
// optional if the path is specific enough to not require it.
func FromPath(client *govmomi.Client, name string, dc *object.Datacenter, status *taskstatus.Status) (*object.ClusterComputeResource, error) {
finder := find.NewFinder(client.Client, false)
if dc != nil {
status.AddToInfo(fmt.Sprintf("[DEBUG] Attempting to locate compute cluster %q in datacenter %q", name, dc.InventoryPath))
finder.SetDatacenter(dc)
} else {
status.AddToInfo(fmt.Sprintf("[DEBUG] Attempting to locate compute cluster at absolute path %q", name))
}
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
return finder.ClusterComputeResource(ctx, name)
}
// Properties is a convenience method that wraps fetching the
// ClusterComputeResource MO from its higher-level object.
func Properties(cluster *object.ClusterComputeResource) (*mo.ClusterComputeResource, error) {
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
var props mo.ClusterComputeResource
if err := cluster.Properties(ctx, cluster.Reference(), nil, &props); err != nil {
return nil, err
}
return &props, nil
}