Skip to content

Commit

Permalink
Avoid possible NPE in VirtualMachine.Device method
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Nov 2, 2016
1 parent f493a3c commit f15dcbd
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### unreleased

* Avoid possible NPE in VirtualMachine.Device method

* Add support for OpaqueNetwork type to Finder

* Add HostConfigManager.AccountManager support for ESX 5.5
Expand Down
2 changes: 2 additions & 0 deletions govc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### unreleased

* Add object.reload command

* Add ESX 5.5 support to host.account commands

### 0.11.1 (2016-10-27)
Expand Down
88 changes: 88 additions & 0 deletions govc/object/reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright (c) 2016 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

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)

type reload struct {
*flags.DatacenterFlag
}

func init() {
cli.Register("object.reload", &reload{})
}

func (cmd *reload) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
cmd.DatacenterFlag.Register(ctx, f)
}

func (cmd *reload) Usage() string {
return "PATH..."
}

func (cmd *reload) Description() string {
return `Reload managed object state.
Examples:
govc datastore.upload $vm.vmx $vm/$vm.vmx
govc object.reload /dc1/vm/$vm`
}

func (cmd *reload) Process(ctx context.Context) error {
if err := cmd.DatacenterFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *reload) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() == 0 {
return flag.ErrHelp
}

c, err := cmd.Client()
if err != nil {
return err
}

objs, err := cmd.ManagedObjects(ctx, f.Args())
if err != nil {
return err
}

for _, obj := range objs {
req := types.Reload{
This: obj,
}

_, err = methods.Reload(ctx, c, &req)
if err != nil {
return err
}
}

return nil
}
20 changes: 20 additions & 0 deletions govc/test/device.bats
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,23 @@ load test_helper
run govc vm.disk.create -vm "$vm" -name disk-16 -size 1K
assert_success
}


@test "device nil config" {
vm=$(new_empty_vm)

run govc device.ls -vm "$vm"
assert_success

run govc datastore.rm "$vm"
assert_success

run govc object.reload "vm/$vm"
assert_success

run govc device.ls -vm "$vm"
assert_failure

run govc vm.unregister "$vm"
assert_success
}
12 changes: 11 additions & 1 deletion object/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,21 @@ func (v VirtualMachine) WaitForNetIP(ctx context.Context, v4 bool) (map[string][
func (v VirtualMachine) Device(ctx context.Context) (VirtualDeviceList, error) {
var o mo.VirtualMachine

err := v.Properties(ctx, v.Reference(), []string{"config.hardware.device"}, &o)
err := v.Properties(ctx, v.Reference(), []string{"config.hardware.device", "summary.runtime.connectionState"}, &o)
if err != nil {
return nil, err
}

// Quoting the SDK doc:
// The virtual machine configuration is not guaranteed to be available.
// For example, the configuration information would be unavailable if the server
// is unable to access the virtual machine files on disk, and is often also unavailable
// during the initial phases of virtual machine creation.
if o.Config == nil {
return nil, fmt.Errorf("%s Config is not available, connectionState=%s",
v.Reference(), o.Summary.Runtime.ConnectionState)
}

return VirtualDeviceList(o.Config.Hardware.Device), nil
}

Expand Down

0 comments on commit f15dcbd

Please sign in to comment.