PBM govmomi dependencies

This commit is contained in:
Balu Dontu
2017-05-22 11:20:12 -07:00
committed by System Administrator
parent 199465c3a5
commit 23ee1745d3
50 changed files with 5272 additions and 297 deletions

View File

@@ -10,8 +10,10 @@ load(
go_library(
name = "go_default_library",
srcs = [
"doc.go",
"error.go",
"finder.go",
"recurser.go",
],
tags = ["automanaged"],
deps = [

37
vendor/github.com/vmware/govmomi/find/doc.go generated vendored Normal file
View File

@@ -0,0 +1,37 @@
/*
Copyright (c) 2014-2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package find implements inventory listing and searching.
The Finder is an alternative to the object.SearchIndex FindByInventoryPath() and FindChild() methods.
SearchIndex.FindByInventoryPath requires an absolute path, whereas the Finder also supports relative paths
and patterns via path.Match.
SearchIndex.FindChild requires a parent to find the child, whereas the Finder also supports an ancestor via
recursive object traversal.
The various Finder methods accept a "path" argument, which can absolute or relative to the Folder for the object type.
The Finder supports two modes, "list" and "find". The "list" mode behaves like the "ls" command, only searching within
the immediate path. The "find" mode behaves like the "find" command, with the search starting at the immediate path but
also recursing into sub Folders relative to the Datacenter. The default mode is "list" if the given path contains a "/",
otherwise "find" mode is used.
The exception is to use a "..." wildcard with a path to find all objects recursively underneath any root object.
For example: VirtualMachineList("/DC1/...")
See also: https://github.com/vmware/govmomi/blob/master/govc/README.md#usage
*/
package find

View File

@@ -1,5 +1,5 @@
/*
Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved.
Copyright (c) 2014-2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import (
"context"
"errors"
"path"
"strings"
"github.com/vmware/govmomi/list"
"github.com/vmware/govmomi/object"
@@ -30,17 +31,18 @@ import (
)
type Finder struct {
client *vim25.Client
recurser list.Recurser
client *vim25.Client
r recurser
dc *object.Datacenter
si *object.SearchIndex
folders *object.DatacenterFolders
}
func NewFinder(client *vim25.Client, all bool) *Finder {
f := &Finder{
client: client,
recurser: list.Recurser{
si: object.NewSearchIndex(client),
r: recurser{
Collector: property.DefaultCollector(client),
All: all,
},
@@ -55,9 +57,41 @@ func (f *Finder) SetDatacenter(dc *object.Datacenter) *Finder {
return f
}
type findRelativeFunc func(ctx context.Context) (object.Reference, error)
// findRoot makes it possible to use "find" mode with a different root path.
// Example: ResourcePoolList("/dc1/host/cluster1/...")
func (f *Finder) findRoot(ctx context.Context, root *list.Element, parts []string) bool {
if len(parts) == 0 {
return false
}
ix := len(parts) - 1
if parts[ix] != "..." {
return false
}
if ix == 0 {
return true // We already have the Object for root.Path
}
// Lookup the Object for the new root.Path
rootPath := path.Join(root.Path, path.Join(parts[:ix]...))
ref, err := f.si.FindByInventoryPath(ctx, rootPath)
if err != nil || ref == nil {
// If we get an error or fail to match, fall through to find() with the original root and path
return false
}
root.Path = rootPath
root.Object = ref
return true
}
func (f *Finder) find(ctx context.Context, arg string, s *spec) ([]list.Element, error) {
isPath := strings.Contains(arg, "/")
func (f *Finder) find(ctx context.Context, fn findRelativeFunc, tl bool, arg string) ([]list.Element, error) {
root := list.Element{
Path: "/",
Object: object.NewRootFolder(f.client),
@@ -70,7 +104,7 @@ func (f *Finder) find(ctx context.Context, fn findRelativeFunc, tl bool, arg str
case "..": // Not supported; many edge case, little value
return nil, errors.New("cannot traverse up a tree")
case ".": // Relative to whatever
pivot, err := fn(ctx)
pivot, err := s.Relative(ctx)
if err != nil {
return nil, err
}
@@ -93,13 +127,17 @@ func (f *Finder) find(ctx context.Context, fn findRelativeFunc, tl bool, arg str
}
}
f.recurser.TraverseLeafs = tl
es, err := f.recurser.Recurse(ctx, root, parts)
if err != nil {
return nil, err
if s.listMode(isPath) {
if f.findRoot(ctx, &root, parts) {
parts = []string{"*"}
} else {
return f.r.List(ctx, s, root, parts)
}
}
return es, nil
s.Parents = append(s.Parents, s.Nested...)
return f.r.Find(ctx, s, root, parts)
}
func (f *Finder) datacenter() (*object.Datacenter, error) {
@@ -208,7 +246,7 @@ func (f *Finder) rootFolder(_ context.Context) (object.Reference, error) {
return object.NewRootFolder(f.client), nil
}
func (f *Finder) managedObjectList(ctx context.Context, path string, tl bool) ([]list.Element, error) {
func (f *Finder) managedObjectList(ctx context.Context, path string, tl bool, include []string) ([]list.Element, error) {
fn := f.rootFolder
if f.dc != nil {
@@ -219,7 +257,18 @@ func (f *Finder) managedObjectList(ctx context.Context, path string, tl bool) ([
path = "."
}
return f.find(ctx, fn, tl, path)
s := &spec{
Relative: fn,
Parents: []string{"ComputeResource", "ClusterComputeResource", "HostSystem", "VirtualApp", "StoragePod"},
Include: include,
}
if tl {
s.Contents = true
s.ListMode = types.NewBool(true)
}
return f.find(ctx, path, s)
}
// Element returns an Element for the given ManagedObjectReference
@@ -229,7 +278,11 @@ func (f *Finder) Element(ctx context.Context, ref types.ManagedObjectReference)
return ref, nil
}
e, err := f.find(ctx, rl, false, ".")
s := &spec{
Relative: rl,
}
e, err := f.find(ctx, "./", s)
if err != nil {
return nil, err
}
@@ -270,16 +323,21 @@ func (f *Finder) ObjectReference(ctx context.Context, ref types.ManagedObjectRef
return r, nil
}
func (f *Finder) ManagedObjectList(ctx context.Context, path string) ([]list.Element, error) {
return f.managedObjectList(ctx, path, false)
func (f *Finder) ManagedObjectList(ctx context.Context, path string, include ...string) ([]list.Element, error) {
return f.managedObjectList(ctx, path, false, include)
}
func (f *Finder) ManagedObjectListChildren(ctx context.Context, path string) ([]list.Element, error) {
return f.managedObjectList(ctx, path, true)
func (f *Finder) ManagedObjectListChildren(ctx context.Context, path string, include ...string) ([]list.Element, error) {
return f.managedObjectList(ctx, path, true, include)
}
func (f *Finder) DatacenterList(ctx context.Context, path string) ([]*object.Datacenter, error) {
es, err := f.find(ctx, f.rootFolder, false, path)
s := &spec{
Relative: f.rootFolder,
Include: []string{"Datacenter"},
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -336,7 +394,12 @@ func (f *Finder) DatacenterOrDefault(ctx context.Context, path string) (*object.
}
func (f *Finder) DatastoreList(ctx context.Context, path string) ([]*object.Datastore, error) {
es, err := f.find(ctx, f.datastoreFolder, false, path)
s := &spec{
Relative: f.datastoreFolder,
Parents: []string{"StoragePod"},
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -404,7 +467,11 @@ func (f *Finder) DatastoreOrDefault(ctx context.Context, path string) (*object.D
}
func (f *Finder) DatastoreClusterList(ctx context.Context, path string) ([]*object.StoragePod, error) {
es, err := f.find(ctx, f.datastoreFolder, false, path)
s := &spec{
Relative: f.datastoreFolder,
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -461,7 +528,11 @@ func (f *Finder) DatastoreClusterOrDefault(ctx context.Context, path string) (*o
}
func (f *Finder) ComputeResourceList(ctx context.Context, path string) ([]*object.ComputeResource, error) {
es, err := f.find(ctx, f.hostFolder, false, path)
s := &spec{
Relative: f.hostFolder,
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -523,7 +594,11 @@ func (f *Finder) ComputeResourceOrDefault(ctx context.Context, path string) (*ob
}
func (f *Finder) ClusterComputeResourceList(ctx context.Context, path string) ([]*object.ClusterComputeResource, error) {
es, err := f.find(ctx, f.hostFolder, false, path)
s := &spec{
Relative: f.hostFolder,
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -564,7 +639,13 @@ func (f *Finder) ClusterComputeResource(ctx context.Context, path string) (*obje
}
func (f *Finder) HostSystemList(ctx context.Context, path string) ([]*object.HostSystem, error) {
es, err := f.find(ctx, f.hostFolder, false, path)
s := &spec{
Relative: f.hostFolder,
Parents: []string{"ComputeResource", "ClusterComputeResource"},
Include: []string{"HostSystem"},
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -635,7 +716,11 @@ func (f *Finder) HostSystemOrDefault(ctx context.Context, path string) (*object.
}
func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.NetworkReference, error) {
es, err := f.find(ctx, f.networkFolder, false, path)
s := &spec{
Relative: f.networkFolder,
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -644,10 +729,14 @@ func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.Network
for _, e := range es {
ref := e.Object.Reference()
switch ref.Type {
case "Network", "OpaqueNetwork":
case "Network":
r := object.NewNetwork(f.client, ref)
r.InventoryPath = e.Path
ns = append(ns, r)
case "OpaqueNetwork":
r := object.NewOpaqueNetwork(f.client, ref)
r.InventoryPath = e.Path
ns = append(ns, r)
case "DistributedVirtualPortgroup":
r := object.NewDistributedVirtualPortgroup(f.client, ref)
r.InventoryPath = e.Path
@@ -701,7 +790,14 @@ func (f *Finder) NetworkOrDefault(ctx context.Context, path string) (object.Netw
}
func (f *Finder) ResourcePoolList(ctx context.Context, path string) ([]*object.ResourcePool, error) {
es, err := f.find(ctx, f.hostFolder, true, path)
s := &spec{
Relative: f.hostFolder,
Parents: []string{"ComputeResource", "ClusterComputeResource", "VirtualApp"},
Nested: []string{"ResourcePool"},
Contents: true,
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -804,7 +900,12 @@ func (f *Finder) FolderOrDefault(ctx context.Context, path string) (*object.Fold
}
func (f *Finder) VirtualMachineList(ctx context.Context, path string) ([]*object.VirtualMachine, error) {
es, err := f.find(ctx, f.vmFolder, false, path)
s := &spec{
Relative: f.vmFolder,
Parents: []string{"VirtualApp"},
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}
@@ -840,7 +941,11 @@ func (f *Finder) VirtualMachine(ctx context.Context, path string) (*object.Virtu
}
func (f *Finder) VirtualAppList(ctx context.Context, path string) ([]*object.VirtualApp, error) {
es, err := f.find(ctx, f.vmFolder, false, path)
s := &spec{
Relative: f.vmFolder,
}
es, err := f.find(ctx, path, s)
if err != nil {
return nil, err
}

245
vendor/github.com/vmware/govmomi/find/recurser.go generated vendored Normal file
View File

@@ -0,0 +1,245 @@
/*
Copyright (c) 2014-2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package find
import (
"context"
"os"
"path"
"github.com/vmware/govmomi/list"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
)
// spec is used to specify per-search configuration, independent of the Finder instance.
type spec struct {
// Relative returns the root object to resolve Relative paths (starts with ".")
Relative func(ctx context.Context) (object.Reference, error)
// ListMode can be used to optionally force "ls" behavior, rather than "find" behavior
ListMode *bool
// Contents configures the Recurser to list the Contents of traversable leaf nodes.
// This is typically set to true when used from the ls command, where listing
// a folder means listing its Contents. This is typically set to false for
// commands that take managed entities that are not folders as input.
Contents bool
// Parents specifies the types which can contain the child types being searched for.
// for example, when searching for a HostSystem, parent types can be
// "ComputeResource" or "ClusterComputeResource".
Parents []string
// Include specifies which types to be included in the results, used only in "find" mode.
Include []string
// Nested should be set to types that can be Nested, used only in "find" mode.
Nested []string
// ChildType avoids traversing into folders that can't contain the Include types, used only in "find" mode.
ChildType []string
}
func (s *spec) traversable(o mo.Reference) bool {
ref := o.Reference()
switch ref.Type {
case "Datacenter":
if len(s.Include) == 1 && s.Include[0] == "Datacenter" {
// No point in traversing deeper as Datacenters cannot be nested
return false
}
return true
case "Folder":
if f, ok := o.(mo.Folder); ok {
// TODO: Not making use of this yet, but here we can optimize when searching the entire
// inventory across Datacenters for specific types, for example: 'govc ls -t VirtualMachine /**'
// should not traverse into a Datacenter's host, network or datatore folders.
if !s.traversableChildType(f.ChildType) {
return false
}
}
return true
}
for _, kind := range s.Parents {
if kind == ref.Type {
return true
}
}
return false
}
func (s *spec) traversableChildType(ctypes []string) bool {
if len(s.ChildType) == 0 {
return true
}
for _, t := range ctypes {
for _, c := range s.ChildType {
if t == c {
return true
}
}
}
return false
}
func (s *spec) wanted(e list.Element) bool {
if len(s.Include) == 0 {
return true
}
w := e.Object.Reference().Type
for _, kind := range s.Include {
if w == kind {
return true
}
}
return false
}
// listMode is a global option to revert to the original Finder behavior,
// disabling the newer "find" mode.
var listMode = os.Getenv("GOVMOMI_FINDER_LIST_MODE") == "true"
func (s *spec) listMode(isPath bool) bool {
if listMode {
return true
}
if s.ListMode != nil {
return *s.ListMode
}
return isPath
}
type recurser struct {
Collector *property.Collector
// All configures the recurses to fetch complete objects for leaf nodes.
All bool
}
func (r recurser) List(ctx context.Context, s *spec, root list.Element, parts []string) ([]list.Element, error) {
if len(parts) == 0 {
// Include non-traversable leaf elements in result. For example, consider
// the pattern "./vm/my-vm-*", where the pattern should match the VMs and
// not try to traverse them.
//
// Include traversable leaf elements in result, if the contents
// field is set to false.
//
if !s.Contents || !s.traversable(root.Object.Reference()) {
return []list.Element{root}, nil
}
}
k := list.Lister{
Collector: r.Collector,
Reference: root.Object.Reference(),
Prefix: root.Path,
}
if r.All && len(parts) < 2 {
k.All = true
}
in, err := k.List(ctx)
if err != nil {
return nil, err
}
// This folder is a leaf as far as the glob goes.
if len(parts) == 0 {
return in, nil
}
pattern := parts[0]
parts = parts[1:]
var out []list.Element
for _, e := range in {
matched, err := path.Match(pattern, path.Base(e.Path))
if err != nil {
return nil, err
}
if !matched {
continue
}
nres, err := r.List(ctx, s, e, parts)
if err != nil {
return nil, err
}
out = append(out, nres...)
}
return out, nil
}
func (r recurser) Find(ctx context.Context, s *spec, root list.Element, parts []string) ([]list.Element, error) {
var out []list.Element
if len(parts) > 0 {
pattern := parts[0]
matched, err := path.Match(pattern, path.Base(root.Path))
if err != nil {
return nil, err
}
if matched && s.wanted(root) {
out = append(out, root)
}
}
if !s.traversable(root.Object) {
return out, nil
}
k := list.Lister{
Collector: r.Collector,
Reference: root.Object.Reference(),
Prefix: root.Path,
}
in, err := k.List(ctx)
if err != nil {
return nil, err
}
for _, e := range in {
nres, err := r.Find(ctx, s, e, parts)
if err != nil {
return nil, err
}
out = append(out, nres...)
}
return out, nil
}