Skip to content

Commit

Permalink
vcsim: add PropertyCollector index support
Browse files Browse the repository at this point in the history
PropertyCollector supports use of index (unique key) to collect a single array element.
Further, a property path may also be applied to the given element.
  • Loading branch information
dougm committed May 30, 2024
1 parent 7a2712e commit dd81bd7
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 39 deletions.
4 changes: 2 additions & 2 deletions govc/object/collect.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 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.
Expand Down Expand Up @@ -416,7 +416,7 @@ func (cmd *collect) Run(ctx context.Context, f *flag.FlagSet) error {
}
res, err := p.RetrieveProperties(ctx, req)
if err != nil {
return nil
return err
}
content := res.Returnval
if len(content) != 1 {
Expand Down
60 changes: 60 additions & 0 deletions govc/test/object.bats
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,66 @@ EOF
govc object.collect -O -json | jq .
}

@test "object.collect index" {
vcsim_env

export GOVC_VM=/DC0/vm/DC0_H0_VM0

# NOTE: '-o' flag uses RetrievePropertiesEx() and mo.ObjectContentToType()
# By default, WaitForUpdatesEx() is used with raw types.ObjectContent

run govc object.collect -o $GOVC_VM 'config.hardware[4000]'
assert_failure

run govc object.collect -o $GOVC_VM 'config.hardware.device[4000'
assert_failure

run govc object.collect -o $GOVC_VM 'config.hardware.device["4000"]'
assert_failure # Key is int, not string

run govc object.collect -o -json $GOVC_VM 'config.hardware.device[4000]'
assert_success

run jq -r .config.hardware.device[].deviceInfo.label <<<"$output"
assert_success ethernet-0

run govc object.collect -o $GOVC_VM 'config.hardware.device[4000].enoent'
assert_failure # InvalidProperty

run govc object.collect -o -json $GOVC_VM 'config.hardware.device[4000].deviceInfo.label'
assert_success

run govc object.collect -s $GOVC_VM 'config.hardware.device[4000].deviceInfo.label'
assert_success ethernet-0

run govc object.collect -o $GOVC_VM 'config.extraConfig[guestinfo.a]'
assert_failure # string Key requires quotes

run govc object.collect -o $GOVC_VM 'config["guestinfo.a"]'
assert_failure

run govc object.collect -o $GOVC_VM 'config.extraConfig["guestinfo.a"]'
assert_success # Key does not exist, not an error

run govc vm.change -e "guestinfo.a=1" -e "guestinfo.b=2"
assert_success

run govc object.collect -json $GOVC_VM 'config.extraConfig["guestinfo.b"]'
assert_success

run jq -r .[].val.value <<<"$output"
assert_success 2

run govc object.collect -o -json $GOVC_VM 'config.extraConfig["guestinfo.b"]'
assert_success

run jq -r .config.extraConfig[].value <<<"$output"
assert_success 2

run govc object.collect -s $GOVC_VM 'config.extraConfig["guestinfo.b"].value'
assert_success 2
}

@test "object.find" {
vcsim_env -ds 2

Expand Down
121 changes: 121 additions & 0 deletions object/extension_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright (c) 2024-2024 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 object_test

import (
"context"
"reflect"
"sync"
"testing"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)

func TestExtensionMangerUpdates(t *testing.T) {
extension := types.Extension{
Description: &types.Description{
Label: "govmomi-test",
Summary: "Extension Manager test",
},
Key: t.Name(),
Version: "0.0.1",
ShownInSolutionManager: types.NewBool(false),
}

description := extension.Description.GetDescription()

f := func(item string) string {
return (&mo.Field{Path: "extensionList", Key: extension.Key, Item: item}).String()
}

tests := []types.PropertyChange{
{Name: f(""), Val: extension, Op: types.PropertyChangeOpAdd},
{Name: f(""), Val: extension, Op: types.PropertyChangeOpAssign},
{Name: f("description"), Val: *description, Op: types.PropertyChangeOpAssign},
{Name: f("description.label"), Val: description.Label, Op: types.PropertyChangeOpAssign},
{Name: f(""), Val: nil, Op: types.PropertyChangeOpRemove},
}

simulator.Test(func(ctx context.Context, c *vim25.Client) {
m := object.NewExtensionManager(c)
pc := property.DefaultCollector(c)

for _, test := range tests {
t.Logf("%s: %s", test.Op, test.Name)
update := make(chan bool)
parked := sync.OnceFunc(func() { update <- true })

var change *types.PropertyChange
cb := func(p []types.PropertyChange) bool {
parked()
change = &p[0]
if change.Op != test.Op {
t.Logf("ignore: change Op=%s, test Op=%s", change.Op, test.Op)
return false
}
return true
}

go func() {
werr := property.Wait(ctx, pc, m.Reference(), []string{test.Name}, cb)
if werr != nil {
t.Log(werr)
}
update <- true
}()
<-update // wait until above go func is parked in WaitForUpdatesEx()

switch test.Op {
case types.PropertyChangeOpAdd:
if err := m.Register(ctx, extension); err != nil {
t.Fatal(err)
}
case types.PropertyChangeOpAssign:
if err := m.Update(ctx, extension); err != nil {
t.Fatal(err)
}
case types.PropertyChangeOpRemove:
if err := m.Unregister(ctx, extension.Key); err != nil {
t.Fatal(err)
}
}
<-update // wait until update is received (cb returns true)

if change == nil {
t.Fatal("no change")
}

if change.Name != test.Name {
t.Errorf("Name: %s", change.Name)
}

if change.Op != test.Op {
t.Errorf("Op: %s", change.Op)
}

if !reflect.DeepEqual(change.Val, test.Val) {
t.Errorf("change.Val: %#v", change.Val)
t.Errorf("test.Val: %#v", test.Val)
}
}
})
}
18 changes: 18 additions & 0 deletions simulator/extension_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func (m *ExtensionManager) RegisterExtension(ctx *Context, req *types.RegisterEx
body.Res = new(types.RegisterExtensionResponse)
m.ExtensionList = append(m.ExtensionList, req.Extension)

f := mo.Field{Path: "extensionList", Key: req.Extension.Key}
ctx.Map.Update(m, []types.PropertyChange{
{Name: f.Path, Val: m.ExtensionList},
{Name: f.String(), Val: req.Extension, Op: types.PropertyChangeOpAdd},
})

return body
}

Expand All @@ -107,6 +113,12 @@ func (m *ExtensionManager) UnregisterExtension(ctx *Context, req *types.Unregist
if x.Key == req.ExtensionKey {
m.ExtensionList = append(m.ExtensionList[:i], m.ExtensionList[i+1:]...)

f := mo.Field{Path: "extensionList", Key: req.ExtensionKey}
ctx.Map.Update(m, []types.PropertyChange{
{Name: f.Path, Val: m.ExtensionList},
{Name: f.String(), Op: types.PropertyChangeOpRemove},
})

body.Res = new(types.UnregisterExtensionResponse)
return body
}
Expand All @@ -124,6 +136,12 @@ func (m *ExtensionManager) UpdateExtension(ctx *Context, req *types.UpdateExtens
if x.Key == req.Extension.Key {
m.ExtensionList[i] = req.Extension

f := mo.Field{Path: "extensionList", Key: req.Extension.Key}
ctx.Map.Update(m, []types.PropertyChange{
{Name: f.Path, Val: m.ExtensionList},
{Name: f.String(), Val: req.Extension},
})

body.Res = new(types.UpdateExtensionResponse)
return body
}
Expand Down
82 changes: 78 additions & 4 deletions simulator/property_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,19 @@ func wrapValue(rval reflect.Value, rtype reflect.Type) interface{} {
return pval
}

func fieldValueInterface(f reflect.StructField, rval reflect.Value) interface{} {
func fieldValueInterface(f reflect.StructField, rval reflect.Value, keyed ...bool) interface{} {
if rval.Kind() == reflect.Ptr {
rval = rval.Elem()
}

if len(keyed) == 1 && keyed[0] {
return rval.Interface() // don't wrap keyed fields in ArrayOf* type
}

return wrapValue(rval, f.Type)
}

func fieldValue(rval reflect.Value, p string) (interface{}, error) {
func fieldValue(rval reflect.Value, p string, keyed ...bool) (interface{}, error) {
var value interface{}
fields := strings.Split(p, ".")

Expand Down Expand Up @@ -204,7 +208,7 @@ func fieldValue(rval reflect.Value, p string) (interface{}, error) {

if i == len(fields)-1 {
ftype, _ := rval.Type().FieldByName(x)
value = fieldValueInterface(ftype, val)
value = fieldValueInterface(ftype, val, keyed...)
break
}

Expand All @@ -214,6 +218,64 @@ func fieldValue(rval reflect.Value, p string) (interface{}, error) {
return value, nil
}

func fieldValueKey(rval reflect.Value, p mo.Field) (interface{}, error) {
if rval.Kind() != reflect.Slice {
return nil, errInvalidField
}

zero := reflect.Value{}

for i := 0; i < rval.Len(); i++ {
item := rval.Index(i)
if item.Kind() == reflect.Interface {
item = item.Elem()
}
if item.Kind() == reflect.Ptr {
item = item.Elem()
}
if item.Kind() != reflect.Struct {
return reflect.Value{}, errInvalidField
}

field := item.FieldByName("Key")
if field == zero {
return nil, errInvalidField
}

switch key := field.Interface().(type) {
case string:
s, ok := p.Key.(string)
if !ok {
return nil, errInvalidField
}
if s == key {
return item.Interface(), nil
}
case int32:
s, ok := p.Key.(int32)
if !ok {
return nil, errInvalidField
}
if s == key {
return item.Interface(), nil
}
default:
return nil, errInvalidField
}
}

return nil, nil
}

func fieldValueIndex(rval reflect.Value, p mo.Field) (interface{}, error) {
val, err := fieldValueKey(rval, p)
if err != nil || val == nil || p.Item == "" {
return val, err
}

return fieldValue(reflect.ValueOf(val), p.Item)
}

func fieldRefs(f interface{}) []types.ManagedObjectReference {
switch fv := f.(type) {
case types.ManagedObjectReference:
Expand Down Expand Up @@ -329,7 +391,19 @@ func (rr *retrieveResult) collectFields(ctx *Context, rval reflect.Value, fields
}
seen[name] = true

val, err := fieldValue(rval, name)
var val interface{}
var err error
var field mo.Field
if field.FromString(name) {
keyed := field.Key != nil

val, err = fieldValue(rval, field.Path, keyed)
if err == nil && keyed {
val, err = fieldValueIndex(reflect.ValueOf(val), field)
}
} else {
err = errInvalidField
}

switch err {
case nil, errEmptyField:
Expand Down
18 changes: 16 additions & 2 deletions simulator/property_filter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 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
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,
Expand Down Expand Up @@ -70,6 +70,20 @@ func (f *PropertyFilter) matches(ctx *Context, ref types.ManagedObjectReference,
return true
}

var field mo.Field
if field.FromString(name) && field.Item != "" {
// "field[key].item" -> "field[key]"
field.Item = ""
if field.String() == change.Name {
change.Name = name
return true
}
}

if field.FromString(change.Name) && field.Key != nil {
continue // case below does not apply to property index
}

// strings.HasPrefix("runtime.powerState", "runtime") == parent field matches
if strings.HasPrefix(change.Name, name) {
if obj := ctx.Map.Get(ref); obj != nil { // object may have since been deleted
Expand Down
Loading

0 comments on commit dd81bd7

Please sign in to comment.