mirror of
https://github.com/Oxalide/vsphere-influxdb-go.git
synced 2023-10-10 11:36:51 +00:00
add vendoring with go dep
This commit is contained in:
201
vendor/github.com/vmware/govmomi/property/collector.go
generated
vendored
Normal file
201
vendor/github.com/vmware/govmomi/property/collector.go
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
Copyright (c) 2015 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 property
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/vmware/govmomi/vim25"
|
||||
"github.com/vmware/govmomi/vim25/methods"
|
||||
"github.com/vmware/govmomi/vim25/mo"
|
||||
"github.com/vmware/govmomi/vim25/soap"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
// Collector models the PropertyCollector managed object.
|
||||
//
|
||||
// For more information, see:
|
||||
// http://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.wssdk.apiref.doc/vmodl.query.PropertyCollector.html
|
||||
//
|
||||
type Collector struct {
|
||||
roundTripper soap.RoundTripper
|
||||
reference types.ManagedObjectReference
|
||||
}
|
||||
|
||||
// DefaultCollector returns the session's default property collector.
|
||||
func DefaultCollector(c *vim25.Client) *Collector {
|
||||
p := Collector{
|
||||
roundTripper: c,
|
||||
reference: c.ServiceContent.PropertyCollector,
|
||||
}
|
||||
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p Collector) Reference() types.ManagedObjectReference {
|
||||
return p.reference
|
||||
}
|
||||
|
||||
// Create creates a new session-specific Collector that can be used to
|
||||
// retrieve property updates independent of any other Collector.
|
||||
func (p *Collector) Create(ctx context.Context) (*Collector, error) {
|
||||
req := types.CreatePropertyCollector{
|
||||
This: p.Reference(),
|
||||
}
|
||||
|
||||
res, err := methods.CreatePropertyCollector(ctx, p.roundTripper, &req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newp := Collector{
|
||||
roundTripper: p.roundTripper,
|
||||
reference: res.Returnval,
|
||||
}
|
||||
|
||||
return &newp, nil
|
||||
}
|
||||
|
||||
// Destroy destroys this Collector.
|
||||
func (p *Collector) Destroy(ctx context.Context) error {
|
||||
req := types.DestroyPropertyCollector{
|
||||
This: p.Reference(),
|
||||
}
|
||||
|
||||
_, err := methods.DestroyPropertyCollector(ctx, p.roundTripper, &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.reference = types.ManagedObjectReference{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Collector) CreateFilter(ctx context.Context, req types.CreateFilter) error {
|
||||
req.This = p.Reference()
|
||||
|
||||
_, err := methods.CreateFilter(ctx, p.roundTripper, &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Collector) WaitForUpdates(ctx context.Context, v string) (*types.UpdateSet, error) {
|
||||
req := types.WaitForUpdatesEx{
|
||||
This: p.Reference(),
|
||||
Version: v,
|
||||
}
|
||||
|
||||
res, err := methods.WaitForUpdatesEx(ctx, p.roundTripper, &req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res.Returnval, nil
|
||||
}
|
||||
|
||||
func (p *Collector) RetrieveProperties(ctx context.Context, req types.RetrieveProperties) (*types.RetrievePropertiesResponse, error) {
|
||||
req.This = p.Reference()
|
||||
return methods.RetrieveProperties(ctx, p.roundTripper, &req)
|
||||
}
|
||||
|
||||
// Retrieve loads properties for a slice of managed objects. The dst argument
|
||||
// must be a pointer to a []interface{}, which is populated with the instances
|
||||
// of the specified managed objects, with the relevant properties filled in. If
|
||||
// the properties slice is nil, all properties are loaded.
|
||||
func (p *Collector) Retrieve(ctx context.Context, objs []types.ManagedObjectReference, ps []string, dst interface{}) error {
|
||||
var propSpec *types.PropertySpec
|
||||
var objectSet []types.ObjectSpec
|
||||
|
||||
for _, obj := range objs {
|
||||
// Ensure that all object reference types are the same
|
||||
if propSpec == nil {
|
||||
propSpec = &types.PropertySpec{
|
||||
Type: obj.Type,
|
||||
}
|
||||
|
||||
if ps == nil {
|
||||
propSpec.All = types.NewBool(true)
|
||||
} else {
|
||||
propSpec.PathSet = ps
|
||||
}
|
||||
} else {
|
||||
if obj.Type != propSpec.Type {
|
||||
return errors.New("object references must have the same type")
|
||||
}
|
||||
}
|
||||
|
||||
objectSpec := types.ObjectSpec{
|
||||
Obj: obj,
|
||||
Skip: types.NewBool(false),
|
||||
}
|
||||
|
||||
objectSet = append(objectSet, objectSpec)
|
||||
}
|
||||
|
||||
req := types.RetrieveProperties{
|
||||
SpecSet: []types.PropertyFilterSpec{
|
||||
{
|
||||
ObjectSet: objectSet,
|
||||
PropSet: []types.PropertySpec{*propSpec},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := p.RetrieveProperties(ctx, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if d, ok := dst.(*[]types.ObjectContent); ok {
|
||||
*d = res.Returnval
|
||||
return nil
|
||||
}
|
||||
|
||||
return mo.LoadRetrievePropertiesResponse(res, dst)
|
||||
}
|
||||
|
||||
// RetrieveWithFilter populates dst as Retrieve does, but only for entities matching the given filter.
|
||||
func (p *Collector) RetrieveWithFilter(ctx context.Context, objs []types.ManagedObjectReference, ps []string, dst interface{}, filter Filter) error {
|
||||
if len(filter) == 0 {
|
||||
return p.Retrieve(ctx, objs, ps, dst)
|
||||
}
|
||||
|
||||
var content []types.ObjectContent
|
||||
|
||||
err := p.Retrieve(ctx, objs, filter.Keys(), &content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
objs = filter.MatchObjectContent(content)
|
||||
|
||||
if len(objs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.Retrieve(ctx, objs, ps, dst)
|
||||
}
|
||||
|
||||
// RetrieveOne calls Retrieve with a single managed object reference.
|
||||
func (p *Collector) RetrieveOne(ctx context.Context, obj types.ManagedObjectReference, ps []string, dst interface{}) error {
|
||||
var objs = []types.ManagedObjectReference{obj}
|
||||
return p.Retrieve(ctx, objs, ps, dst)
|
||||
}
|
135
vendor/github.com/vmware/govmomi/property/filter.go
generated
vendored
Normal file
135
vendor/github.com/vmware/govmomi/property/filter.go
generated
vendored
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
Copyright (c) 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 property
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
// Filter provides methods for matching against types.DynamicProperty
|
||||
type Filter map[string]types.AnyType
|
||||
|
||||
// Keys returns the Filter map keys as a []string
|
||||
func (f Filter) Keys() []string {
|
||||
keys := make([]string, 0, len(f))
|
||||
|
||||
for key := range f {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// MatchProperty returns true if a Filter entry matches the given prop.
|
||||
func (f Filter) MatchProperty(prop types.DynamicProperty) bool {
|
||||
match, ok := f[prop.Name]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if match == prop.Val {
|
||||
return true
|
||||
}
|
||||
|
||||
ptype := reflect.TypeOf(prop.Val)
|
||||
|
||||
if strings.HasPrefix(ptype.Name(), "ArrayOf") {
|
||||
pval := reflect.ValueOf(prop.Val).Field(0)
|
||||
|
||||
for i := 0; i < pval.Len(); i++ {
|
||||
prop.Val = pval.Index(i).Interface()
|
||||
|
||||
if f.MatchProperty(prop) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if reflect.TypeOf(match) != ptype {
|
||||
s, ok := match.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
// convert if we can
|
||||
switch prop.Val.(type) {
|
||||
case bool:
|
||||
match, _ = strconv.ParseBool(s)
|
||||
case int16:
|
||||
x, _ := strconv.ParseInt(s, 10, 16)
|
||||
match = int16(x)
|
||||
case int32:
|
||||
x, _ := strconv.ParseInt(s, 10, 32)
|
||||
match = int32(x)
|
||||
case int64:
|
||||
match, _ = strconv.ParseInt(s, 10, 64)
|
||||
case float32:
|
||||
x, _ := strconv.ParseFloat(s, 32)
|
||||
match = float32(x)
|
||||
case float64:
|
||||
match, _ = strconv.ParseFloat(s, 64)
|
||||
case fmt.Stringer:
|
||||
prop.Val = prop.Val.(fmt.Stringer).String()
|
||||
default:
|
||||
if ptype.Kind() != reflect.String {
|
||||
return false
|
||||
}
|
||||
// An enum type we can convert to a string type
|
||||
prop.Val = reflect.ValueOf(prop.Val).String()
|
||||
}
|
||||
}
|
||||
|
||||
switch pval := prop.Val.(type) {
|
||||
case string:
|
||||
m, _ := filepath.Match(match.(string), pval)
|
||||
return m
|
||||
default:
|
||||
return reflect.DeepEqual(match, pval)
|
||||
}
|
||||
}
|
||||
|
||||
// MatchPropertyList returns true if all given props match the Filter.
|
||||
func (f Filter) MatchPropertyList(props []types.DynamicProperty) bool {
|
||||
for _, p := range props {
|
||||
if !f.MatchProperty(p) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// MatchObjectContent returns a list of ObjectContent.Obj where the ObjectContent.PropSet matches the Filter.
|
||||
func (f Filter) MatchObjectContent(objects []types.ObjectContent) []types.ManagedObjectReference {
|
||||
var refs []types.ManagedObjectReference
|
||||
|
||||
for _, o := range objects {
|
||||
if f.MatchPropertyList(o.PropSet) {
|
||||
refs = append(refs, o.Obj)
|
||||
}
|
||||
}
|
||||
|
||||
return refs
|
||||
}
|
60
vendor/github.com/vmware/govmomi/property/filter_test.go
generated
vendored
Normal file
60
vendor/github.com/vmware/govmomi/property/filter_test.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
Copyright (c) 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 property
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
func TestMatchProperty(t *testing.T) {
|
||||
tests := []struct {
|
||||
key string
|
||||
val types.AnyType
|
||||
pass types.AnyType
|
||||
fail types.AnyType
|
||||
}{
|
||||
{"string", "bar", "bar", "foo"},
|
||||
{"match", "foo.bar", "foo.*", "foobarbaz"},
|
||||
{"moref", types.ManagedObjectReference{Type: "HostSystem", Value: "foo"}, "HostSystem:foo", "bar"}, // implements fmt.Stringer
|
||||
{"morefm", types.ManagedObjectReference{Type: "HostSystem", Value: "foo"}, "*foo", "bar"},
|
||||
{"morefs", types.ArrayOfManagedObjectReference{ManagedObjectReference: []types.ManagedObjectReference{{Type: "HostSystem", Value: "foo"}}}, "*foo", "bar"},
|
||||
{"enum", types.VirtualMachinePowerStatePoweredOn, "poweredOn", "poweredOff"},
|
||||
{"int16", int32(16), int32(16), int32(42)},
|
||||
{"int32", int32(32), int32(32), int32(42)},
|
||||
{"int32s", int32(32), "32", "42"},
|
||||
{"int64", int64(64), int64(64), int64(42)},
|
||||
{"int64s", int64(64), "64", "42"},
|
||||
{"float32", float32(32.32), float32(32.32), float32(42.0)},
|
||||
{"float32s", float32(32.32), "32.32", "42.0"},
|
||||
{"float64", float64(64.64), float64(64.64), float64(42.0)},
|
||||
{"float64s", float64(64.64), "64.64", "42.0"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
p := types.DynamicProperty{Name: test.key, Val: test.val}
|
||||
|
||||
for match, value := range map[bool]types.AnyType{true: test.pass, false: test.fail} {
|
||||
result := Filter{test.key: value}.MatchProperty(p)
|
||||
|
||||
if result != match {
|
||||
t.Errorf("%s: %t", test.key, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
153
vendor/github.com/vmware/govmomi/property/wait.go
generated
vendored
Normal file
153
vendor/github.com/vmware/govmomi/property/wait.go
generated
vendored
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
Copyright (c) 2015 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 property
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
// Wait waits for any of the specified properties of the specified managed
|
||||
// object to change. It calls the specified function for every update it
|
||||
// receives. If this function returns false, it continues waiting for
|
||||
// subsequent updates. If this function returns true, it stops waiting and
|
||||
// returns.
|
||||
//
|
||||
// To only receive updates for the specified managed object, the function
|
||||
// creates a new property collector and calls CreateFilter. A new property
|
||||
// collector is required because filters can only be added, not removed.
|
||||
//
|
||||
// The newly created collector is destroyed before this function returns (both
|
||||
// in case of success or error).
|
||||
//
|
||||
func Wait(ctx context.Context, c *Collector, obj types.ManagedObjectReference, ps []string, f func([]types.PropertyChange) bool) error {
|
||||
p, err := c.Create(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Attempt to destroy the collector using the background context, as the
|
||||
// specified context may have timed out or have been cancelled.
|
||||
defer p.Destroy(context.Background())
|
||||
|
||||
req := types.CreateFilter{
|
||||
Spec: types.PropertyFilterSpec{
|
||||
ObjectSet: []types.ObjectSpec{
|
||||
{
|
||||
Obj: obj,
|
||||
},
|
||||
},
|
||||
PropSet: []types.PropertySpec{
|
||||
{
|
||||
PathSet: ps,
|
||||
Type: obj.Type,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if len(ps) == 0 {
|
||||
req.Spec.PropSet[0].All = types.NewBool(true)
|
||||
}
|
||||
|
||||
err = p.CreateFilter(ctx, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return waitLoop(ctx, p, func(_ types.ManagedObjectReference, pc []types.PropertyChange) bool {
|
||||
return f(pc)
|
||||
})
|
||||
}
|
||||
|
||||
// WaitForView waits for any of the specified properties of the managed
|
||||
// objects in the View to change. It calls the specified function for every update it
|
||||
// receives. If this function returns false, it continues waiting for
|
||||
// subsequent updates. If this function returns true, it stops waiting and
|
||||
// returns.
|
||||
//
|
||||
// To only receive updates for the View's specified managed objects, the function
|
||||
// creates a new property collector and calls CreateFilter. A new property
|
||||
// collector is required because filters can only be added, not removed.
|
||||
//
|
||||
// The newly created collector is destroyed before this function returns (both
|
||||
// in case of success or error).
|
||||
//
|
||||
// The code assumes that all objects in the View are the same type
|
||||
func WaitForView(ctx context.Context, c *Collector, view types.ManagedObjectReference, obj types.ManagedObjectReference, ps []string, f func(types.ManagedObjectReference, []types.PropertyChange) bool) error {
|
||||
p, err := c.Create(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Attempt to destroy the collector using the background context, as the
|
||||
// specified context may have timed out or have been cancelled.
|
||||
defer p.Destroy(context.Background())
|
||||
|
||||
req := types.CreateFilter{
|
||||
Spec: types.PropertyFilterSpec{
|
||||
ObjectSet: []types.ObjectSpec{
|
||||
{
|
||||
Obj: view,
|
||||
SelectSet: []types.BaseSelectionSpec{
|
||||
&types.TraversalSpec{
|
||||
SelectionSpec: types.SelectionSpec{
|
||||
Name: "traverseEntities",
|
||||
},
|
||||
Path: "view",
|
||||
Type: view.Type}},
|
||||
},
|
||||
},
|
||||
PropSet: []types.PropertySpec{
|
||||
{
|
||||
Type: obj.Type,
|
||||
PathSet: ps,
|
||||
},
|
||||
},
|
||||
}}
|
||||
|
||||
err = p.CreateFilter(ctx, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return waitLoop(ctx, p, f)
|
||||
}
|
||||
|
||||
func waitLoop(ctx context.Context, c *Collector, f func(types.ManagedObjectReference, []types.PropertyChange) bool) error {
|
||||
for version := ""; ; {
|
||||
res, err := c.WaitForUpdates(ctx, version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Retry if the result came back empty
|
||||
if res == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
version = res.Version
|
||||
|
||||
for _, fs := range res.FilterSet {
|
||||
for _, os := range fs.ObjectSet {
|
||||
if f(os.Obj, os.ChangeSet) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user