forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacenter_helper.go
49 lines (43 loc) · 1.42 KB
/
datacenter_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
package vsphere
import (
"fmt"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)
// getDatacenter gets the higher-level datacenter object for the datacenter
// name supplied by dc.
//
// The default datacenter is denoted by using an empty string. When working
// with ESXi directly, the default datacenter is always selected.
func getDatacenter(c *govmomi.Client, dc string) (*object.Datacenter, error) {
finder := find.NewFinder(c.Client, true)
t := c.ServiceContent.About.ApiType
switch t {
case "HostAgent":
return finder.DefaultDatacenter(context.TODO())
case "VirtualCenter":
if dc != "" {
return finder.Datacenter(context.TODO(), dc)
}
return finder.DefaultDatacenter(context.TODO())
}
return nil, fmt.Errorf("unsupported ApiType: %s", t)
}
// datacenterFromID locates a Datacenter by its managed object reference ID.
func datacenterFromID(client *govmomi.Client, id string) (*object.Datacenter, error) {
finder := find.NewFinder(client.Client, false)
ref := types.ManagedObjectReference{
Type: "Datacenter",
Value: id,
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
ds, err := finder.ObjectReference(ctx, ref)
if err != nil {
return nil, fmt.Errorf("could not find datacenter with id: %s: %s", id, err)
}
return ds.(*object.Datacenter), nil
}