Skip to content

Commit

Permalink
govc: Fix incorrect MoRef parsing
Browse files Browse the repository at this point in the history
Closes: #2538
Signed-off-by: Michael Gasch <mgasch@vmware.com>
  • Loading branch information
Michael Gasch committed Sep 1, 2021
1 parent bd8668d commit f4ef4d9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
6 changes: 3 additions & 3 deletions govc/flags/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ func (flag *DatacenterFlag) ManagedObjects(ctx context.Context, args []string) (
}

for _, arg := range args {
var ref types.ManagedObjectReference
if ref.FromString(arg) {
if ref := object.ReferenceFromString(arg); ref != nil {
// e.g. output from object.collect
refs = append(refs, ref)
refs = append(refs, *ref)
continue
}

elements, err := finder.ManagedObjectList(ctx, arg)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions govc/test/vm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ load test_helper

run govc vm.create -force -cluster DC0_C0 "$id"
assert_success # create vm with the same name

run govc vm.create -cluster DC0_C0 "my:vm"
assert_success # vm has special characters (moref)

run govc object.collect -s vm/my:vm name
assert_success my:vm
}

@test "vm.change" {
Expand Down
12 changes: 12 additions & 0 deletions object/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)

Expand Down Expand Up @@ -134,3 +135,14 @@ func (c Common) SetCustomValue(ctx context.Context, key string, value string) er
_, err := methods.SetCustomValue(ctx, c.c, &req)
return err
}

func ReferenceFromString(s string) *types.ManagedObjectReference {
var ref types.ManagedObjectReference
if !ref.FromString(s) {
return nil
}
if mo.IsManagedObjectType(ref.Type) {
return &ref
}
return nil
}
5 changes: 5 additions & 0 deletions vim25/mo/type_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,8 @@ func (t *typeInfo) LoadFromObjectContent(o types.ObjectContent) (reflect.Value,

return v, nil
}

func IsManagedObjectType(kind string) bool {
_, ok := t[kind]
return ok
}
2 changes: 1 addition & 1 deletion vim25/types/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (r ManagedObjectReference) String() string {
func (r *ManagedObjectReference) FromString(o string) bool {
s := strings.SplitN(o, ":", 2)

if len(s) < 2 {
if len(s) != 2 {
return false
}

Expand Down

0 comments on commit f4ef4d9

Please sign in to comment.