2017-07-09 14:56:39 -04:00
|
|
|
/*
|
2020-01-08 13:22:55 -05:00
|
|
|
Copyright (c) 2015-2017 VMware, Inc. All Rights Reserved.
|
2017-07-09 14:56:39 -04:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
"github.com/vmware/govmomi/vim25/methods"
|
2020-06-10 08:38:17 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/soap"
|
2017-07-09 14:56:39 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
|
|
)
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
// WaitFilter provides helpers to construct a types.CreateFilter for use with property.Wait
|
|
|
|
type WaitFilter struct {
|
|
|
|
types.CreateFilter
|
2020-06-10 08:38:17 -04:00
|
|
|
Options *types.WaitOptions
|
|
|
|
PropagateMissing bool
|
2020-08-19 07:20:25 -04:00
|
|
|
Truncated bool
|
2020-01-08 13:22:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new ObjectSpec and PropertySpec to the WaitFilter
|
|
|
|
func (f *WaitFilter) Add(obj types.ManagedObjectReference, kind string, ps []string, set ...types.BaseSelectionSpec) *WaitFilter {
|
|
|
|
spec := types.ObjectSpec{
|
|
|
|
Obj: obj,
|
|
|
|
SelectSet: set,
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
pset := types.PropertySpec{
|
|
|
|
Type: kind,
|
|
|
|
PathSet: ps,
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ps) == 0 {
|
2020-01-08 13:22:55 -05:00
|
|
|
pset.All = types.NewBool(true)
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
f.Spec.ObjectSet = append(f.Spec.ObjectSet, spec)
|
|
|
|
|
|
|
|
f.Spec.PropSet = append(f.Spec.PropSet, pset)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait creates a new WaitFilter and calls the specified function for each ObjectUpdate via WaitForUpdates
|
|
|
|
func Wait(ctx context.Context, c *Collector, obj types.ManagedObjectReference, ps []string, f func([]types.PropertyChange) bool) error {
|
|
|
|
filter := new(WaitFilter).Add(obj, obj.Type, ps)
|
|
|
|
|
|
|
|
return WaitForUpdates(ctx, c, filter, func(updates []types.ObjectUpdate) bool {
|
|
|
|
for _, update := range updates {
|
|
|
|
if f(update.ChangeSet) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2017-07-09 14:56:39 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
// WaitForUpdates waits for any of the specified properties of the specified managed
|
|
|
|
// object to change. It calls the specified function for every update it
|
2017-07-09 14:56:39 -04:00
|
|
|
// receives. If this function returns false, it continues waiting for
|
|
|
|
// subsequent updates. If this function returns true, it stops waiting and
|
|
|
|
// returns.
|
|
|
|
//
|
2020-01-08 13:22:55 -05:00
|
|
|
// To only receive updates for the specified managed object, the function
|
2017-07-09 14:56:39 -04:00
|
|
|
// creates a new property collector and calls CreateFilter. A new property
|
|
|
|
// collector is required because filters can only be added, not removed.
|
|
|
|
//
|
2020-01-08 13:22:55 -05:00
|
|
|
// If the Context is canceled, a call to CancelWaitForUpdates() is made and its error value is returned.
|
2017-07-09 14:56:39 -04:00
|
|
|
// The newly created collector is destroyed before this function returns (both
|
|
|
|
// in case of success or error).
|
|
|
|
//
|
2020-06-10 08:38:17 -04:00
|
|
|
// By default, ObjectUpdate.MissingSet faults are not propagated to the returned error,
|
|
|
|
// set WaitFilter.PropagateMissing=true to enable MissingSet fault propagation.
|
2020-01-08 13:22:55 -05:00
|
|
|
func WaitForUpdates(ctx context.Context, c *Collector, filter *WaitFilter, f func([]types.ObjectUpdate) bool) error {
|
2017-07-09 14:56:39 -04:00
|
|
|
p, err := c.Create(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to destroy the collector using the background context, as the
|
2020-01-08 13:22:55 -05:00
|
|
|
// specified context may have timed out or have been canceled.
|
|
|
|
defer func() {
|
|
|
|
_ = p.Destroy(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = p.CreateFilter(ctx, filter.CreateFilter)
|
2017-07-09 14:56:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
req := types.WaitForUpdatesEx{
|
|
|
|
This: p.Reference(),
|
|
|
|
Options: filter.Options,
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, err := methods.WaitForUpdatesEx(ctx, p.roundTripper, &req)
|
2017-07-09 14:56:39 -04:00
|
|
|
if err != nil {
|
2020-01-08 13:22:55 -05:00
|
|
|
if ctx.Err() == context.Canceled {
|
|
|
|
werr := p.CancelWaitForUpdates(context.Background())
|
|
|
|
return werr
|
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
set := res.Returnval
|
|
|
|
if set == nil {
|
|
|
|
if req.Options != nil && req.Options.MaxWaitSeconds != nil {
|
|
|
|
return nil // WaitOptions.MaxWaitSeconds exceeded
|
|
|
|
}
|
|
|
|
// Retry if the result came back empty
|
2017-07-09 14:56:39 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
req.Version = set.Version
|
2020-08-19 07:20:25 -04:00
|
|
|
filter.Truncated = false
|
|
|
|
if set.Truncated != nil {
|
|
|
|
filter.Truncated = *set.Truncated
|
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
for _, fs := range set.FilterSet {
|
2020-06-10 08:38:17 -04:00
|
|
|
if filter.PropagateMissing {
|
|
|
|
for i := range fs.ObjectSet {
|
|
|
|
for _, p := range fs.ObjectSet[i].MissingSet {
|
|
|
|
// Same behavior as mo.ObjectContentToType()
|
|
|
|
return soap.WrapVimFault(p.Fault.Fault)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
if f(fs.ObjectSet) {
|
|
|
|
return nil
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|