Skip to content

Commit

Permalink
Make Wait function in property package standalone
Browse files Browse the repository at this point in the history
This function uses CreateFilter to be selective in the updates it
receives. Filters can only be added to property collectors, not removed.
This means that a property collector is no longer useful after being
used by Wait. This in turn means that it is most convenient to have Wait
create and destroy a property collector for its own use.
  • Loading branch information
pietern committed Mar 16, 2015
1 parent d367290 commit fb0033d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 72 deletions.
10 changes: 2 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ func (c *Client) PropertiesN(objs []types.ManagedObjectReference, p []string, ds
}

func (c *Client) WaitForProperties(obj types.ManagedObjectReference, ps []string, f func([]types.PropertyChange) bool) error {
p, err := property.NewCollector(context.TODO(), c, c.ServiceContent)
if err != nil {
return err
}

defer p.Destroy(context.TODO())

return p.Wait(context.TODO(), obj, ps, f)
p := property.DefaultCollector(c, c.ServiceContent)
return property.Wait(context.TODO(), p, obj, ps, f)
}
8 changes: 1 addition & 7 deletions object/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ func (t *Task) Wait(ctx context.Context) error {
}

func (t *Task) WaitForResult(ctx context.Context, s progress.Sinker) (*types.TaskInfo, error) {
p, err := property.NewCollector(ctx, t.c, t.c.ServiceContent)
if err != nil {
return nil, err
}

defer p.Destroy(context.Background())

p := property.DefaultCollector(t.c, t.c.ServiceContent)
return task.Wait(ctx, t.ref, p, s)
}
86 changes: 30 additions & 56 deletions property/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,57 @@ import (
"golang.org/x/net/context"
)

// 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
}

// NewCollector creates a new property collector based on the root property
// collector. It is the responsibility of the caller to destroy it when done.
func NewCollector(ctx context.Context, rt soap.RoundTripper, sc types.ServiceContent) (*Collector, error) {
// DefaultCollector returns the session's default property collector.
func DefaultCollector(rt soap.RoundTripper, sc types.ServiceContent) *Collector {
p := Collector{
roundTripper: rt,
reference: sc.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: sc.PropertyCollector,
This: p.Reference(),
}

res, err := methods.CreatePropertyCollector(ctx, rt, &req)
res, err := methods.CreatePropertyCollector(ctx, p.roundTripper, &req)
if err != nil {
return nil, err
}

p := Collector{
roundTripper: rt,
newp := Collector{
roundTripper: p.roundTripper,
reference: res.Returnval,
}

return &p, nil
}

func (p Collector) Reference() types.ManagedObjectReference {
return p.reference
return &newp, nil
}

// Destroy destroys this Collector.
func (p *Collector) Destroy(ctx context.Context) error {
req := types.DestroyCollector{
req := types.DestroyPropertyCollector{
This: p.Reference(),
}

_, err := methods.DestroyCollector(ctx, p.roundTripper, &req)
_, err := methods.DestroyPropertyCollector(ctx, p.roundTripper, &req)
if err != nil {
return err
}
Expand Down Expand Up @@ -90,45 +106,3 @@ func (p *Collector) WaitForUpdates(ctx context.Context, v string) (*types.Update

return res.Returnval, nil
}

func (p *Collector) Wait(ctx context.Context, obj types.ManagedObjectReference, ps []string, f func([]types.PropertyChange) bool) error {
req := types.CreateFilter{
Spec: types.PropertyFilterSpec{
ObjectSet: []types.ObjectSpec{
{
Obj: obj,
},
},
PropSet: []types.PropertySpec{
{
PathSet: ps,
Type: obj.Type,
},
},
},
}

err := p.CreateFilter(ctx, req)
if err != nil {
return err
}

for version := ""; ; {
res, err := p.WaitForUpdates(ctx, version)
if err != nil {
return err
}

version = res.Version

for _, fs := range res.FilterSet {
for _, os := range fs.ObjectSet {
if os.Obj == obj {
if f(os.ChangeSet) {
return nil
}
}
}
}
}
}
86 changes: 86 additions & 0 deletions property/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright (c) 2014 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 (
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)

// 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,
},
},
},
}

err = p.CreateFilter(ctx, req)
if err != nil {
return err
}

for version := ""; ; {
res, err := p.WaitForUpdates(ctx, version)
if err != nil {
return err
}

version = res.Version

for _, fs := range res.FilterSet {
for _, os := range fs.ObjectSet {
if os.Obj == obj {
if f(os.ChangeSet) {
return nil
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion task/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Wait(ctx context.Context, ref types.ManagedObjectReference, pc *property.Co
defer close(cb.ch)
}

err := pc.Wait(ctx, ref, []string{"info"}, cb.fn)
err := property.Wait(ctx, pc, ref, []string{"info"}, cb.fn)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fb0033d

Please sign in to comment.