From f5080d3ea4ddf7fc8114b0d3582987eb6bd985ed Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Thu, 11 Apr 2024 10:42:29 -0700 Subject: [PATCH] api: use RetrievePropertiesEx in mo package functions --- property/collector.go | 24 ++---------------------- vim25/mo/retrieve.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/property/collector.go b/property/collector.go index f6bed6caa..263621a06 100644 --- a/property/collector.go +++ b/property/collector.go @@ -155,9 +155,8 @@ func (p *Collector) RetrieveProperties( opts.MaxObjects = maxObjectsArgs[0] } - req.This = p.Reference() - rx, err := methods.RetrievePropertiesEx(ctx, p.roundTripper, &types.RetrievePropertiesEx{ - This: req.This, + objects, err := mo.RetrievePropertiesEx(ctx, p.roundTripper, types.RetrievePropertiesEx{ + This: p.Reference(), SpecSet: req.SpecSet, Options: opts, }) @@ -165,25 +164,6 @@ func (p *Collector) RetrieveProperties( return nil, err } - if rx.Returnval == nil { - return &types.RetrievePropertiesResponse{}, nil - } - - objects := rx.Returnval.Objects - token := rx.Returnval.Token - - for token != "" { - cx, err := methods.ContinueRetrievePropertiesEx(ctx, p.roundTripper, &types.ContinueRetrievePropertiesEx{ - This: req.This, - Token: token, - }) - if err != nil { - return nil, err - } - token = cx.Returnval.Token - objects = append(objects, cx.Returnval.Objects...) - } - return &types.RetrievePropertiesResponse{Returnval: objects}, nil } diff --git a/vim25/mo/retrieve.go b/vim25/mo/retrieve.go index 96be376f7..9f2b32486 100644 --- a/vim25/mo/retrieve.go +++ b/vim25/mo/retrieve.go @@ -158,16 +158,49 @@ func LoadObjectContent(content []types.ObjectContent, dst interface{}) error { return nil } +// RetrievePropertiesEx wraps RetrievePropertiesEx and ContinueRetrievePropertiesEx to collect properties in batches. +func RetrievePropertiesEx(ctx context.Context, r soap.RoundTripper, req types.RetrievePropertiesEx) ([]types.ObjectContent, error) { + rx, err := methods.RetrievePropertiesEx(ctx, r, &req) + if err != nil { + return nil, err + } + + if rx.Returnval == nil { + return nil, nil + } + + objects := rx.Returnval.Objects + token := rx.Returnval.Token + + for token != "" { + cx, err := methods.ContinueRetrievePropertiesEx(ctx, r, &types.ContinueRetrievePropertiesEx{ + This: req.This, + Token: token, + }) + if err != nil { + return nil, err + } + + token = cx.Returnval.Token + objects = append(objects, cx.Returnval.Objects...) + } + + return objects, nil +} + // RetrievePropertiesForRequest calls the RetrieveProperties method with the // specified request and decodes the response struct into the value pointed to // by dst. func RetrievePropertiesForRequest(ctx context.Context, r soap.RoundTripper, req types.RetrieveProperties, dst interface{}) error { - res, err := methods.RetrieveProperties(ctx, r, &req) + objects, err := RetrievePropertiesEx(ctx, r, types.RetrievePropertiesEx{ + This: req.This, + SpecSet: req.SpecSet, + }) if err != nil { return err } - return LoadObjectContent(res.Returnval, dst) + return LoadObjectContent(objects, dst) } // RetrieveProperties retrieves the properties of the managed object specified