Skip to content

Commit

Permalink
vcsim: EnvironmentBrowser improvements
Browse files Browse the repository at this point in the history
vcsim: add EnvironmentBrowser methods and record/replay support

api: add object.EnvironmentBrowser helper methods

govc: add vm.option.ls, vm.target.info and vm.target.cap.ls commands

BREAKING: removed object.VirtualMachine.QueryConfigTarget method
- Use object.VirtualMachine.EnvironmentBrowser().QueryConfigTarget instead
  • Loading branch information
dougm committed Sep 27, 2023
1 parent 8f49f6c commit 524ca25
Show file tree
Hide file tree
Showing 18 changed files with 753 additions and 124 deletions.
66 changes: 66 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,14 @@ but appear via `govc $cmd -h`:
- [vm.network.add](#vmnetworkadd)
- [vm.network.change](#vmnetworkchange)
- [vm.option.info](#vmoptioninfo)
- [vm.option.ls](#vmoptionls)
- [vm.power](#vmpower)
- [vm.question](#vmquestion)
- [vm.rdm.attach](#vmrdmattach)
- [vm.rdm.ls](#vmrdmls)
- [vm.register](#vmregister)
- [vm.target.cap.ls](#vmtargetcapls)
- [vm.target.info](#vmtargetinfo)
- [vm.unregister](#vmunregister)
- [vm.upgrade](#vmupgrade)
- [vm.vnc](#vmvnc)
Expand Down Expand Up @@ -6367,6 +6370,23 @@ Examples:
govc vm.option.info -host my_hostname
govc vm.option.info -vm my_vm
Options:
-cluster= Cluster [GOVC_CLUSTER]
-host= Host system [GOVC_HOST]
-id= Option descriptor key
-vm= Virtual machine [GOVC_VM]
```

## vm.option.ls

```
Usage: govc vm.option.ls [OPTIONS]
List VM config option keys for CLUSTER.
Examples:
govc vm.option.ls -cluster C0
Options:
-cluster= Cluster [GOVC_CLUSTER]
-host= Host system [GOVC_HOST]
Expand Down Expand Up @@ -6459,6 +6479,52 @@ Options:
-template=false Mark VM as template
```

## vm.target.cap.ls

```
Usage: govc vm.target.cap.ls [OPTIONS]
List VM config target capabilities.
The config target data contains capabilities about the execution environment for a VM
in the given CLUSTER, and optionally for a specific HOST.
Examples:
govc vm.target.cap.ls -cluster C0
govc vm.target.cap.ls -host my_hostname
govc vm.target.cap.ls -vm my_vm
Options:
-cluster= Cluster [GOVC_CLUSTER]
-host= Host system [GOVC_HOST]
-vm= Virtual machine [GOVC_VM]
```

## vm.target.info

```
Usage: govc vm.target.info [OPTIONS]
VM config target info.
The config target data contains information about the execution environment for a VM
in the given CLUSTER, and optionally for a specific HOST.
Examples:
govc vm.target.info -cluster C0
govc vm.target.info -host my_hostname
govc vm.target.info -vm my_vm
Options:
-cluster= Cluster [GOVC_CLUSTER]
-datastore=true Include Datastores
-device=true Include Devices
-disk=false Include Disks
-host= Host system [GOVC_HOST]
-network=true Include Networks
-vm= Virtual machine [GOVC_VM]
```

## vm.unregister

```
Expand Down
6 changes: 3 additions & 3 deletions govc/device/pci/add.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2014-2020 VMware, Inc. All Rights Reserved.
Copyright (c) 2020-2023 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 @@ -104,7 +104,7 @@ func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
return flag.ErrHelp
}

vmConfigOptions, err := vm.QueryConfigTarget(ctx)
vmConfigOptions, err := queryConfigTarget(ctx, vm)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions govc/device/pci/ls.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2014-2020 VMware, Inc. All Rights Reserved.
Copyright (c) 2020-2023 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 All @@ -26,6 +26,7 @@ import (

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

Expand Down Expand Up @@ -71,7 +72,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
return flag.ErrHelp
}

vmConfigOptions, err := vm.QueryConfigTarget(ctx)
vmConfigOptions, err := queryConfigTarget(ctx, vm)
if err != nil {
return err
}
Expand All @@ -93,3 +94,11 @@ func (r *infoResult) Write(w io.Writer) error {
}
return tw.Flush()
}

func queryConfigTarget(ctx context.Context, m *object.VirtualMachine) (*types.ConfigTarget, error) {
b, err := m.EnvironmentBrowser(ctx)
if err != nil {
return nil, err
}
return b.QueryConfigTarget(ctx, nil)
}
94 changes: 94 additions & 0 deletions govc/flags/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright (c) 2023-2023 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 flags

import (
"context"
"flag"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
)

type EnvBrowser struct {
*ClusterFlag
*HostSystemFlag
*VirtualMachineFlag
}

func (cmd *EnvBrowser) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClusterFlag, ctx = NewClusterFlag(ctx)
cmd.ClusterFlag.Register(ctx, f)

cmd.HostSystemFlag, ctx = NewHostSystemFlag(ctx)
cmd.HostSystemFlag.Register(ctx, f)

cmd.VirtualMachineFlag, ctx = NewVirtualMachineFlag(ctx)
cmd.VirtualMachineFlag.Register(ctx, f)
}

func (cmd *EnvBrowser) Process(ctx context.Context) error {
if err := cmd.ClusterFlag.Process(ctx); err != nil {
return err
}
if err := cmd.HostSystemFlag.Process(ctx); err != nil {
return err
}
return cmd.VirtualMachineFlag.Process(ctx)
}

func (cmd *EnvBrowser) Browser(ctx context.Context) (*object.EnvironmentBrowser, error) {
c, err := cmd.VirtualMachineFlag.Client()
if err != nil {
return nil, err
}

vm, err := cmd.VirtualMachine()
if err != nil {
return nil, err
}
if vm != nil {
return vm.EnvironmentBrowser(ctx)
}

host, err := cmd.HostSystemIfSpecified()
if err != nil {
return nil, err
}

if host != nil {
var h mo.HostSystem
err = host.Properties(ctx, host.Reference(), []string{"parent"}, &h)
if err != nil {
return nil, err
}

return object.NewComputeResource(c, *h.Parent).EnvironmentBrowser(ctx)
}

finder, ferr := cmd.ClusterFlag.Finder()
if ferr != nil {
return nil, ferr
}

cr, ferr := finder.ComputeResourceOrDefault(ctx, cmd.ClusterFlag.Name)
if ferr != nil {
return nil, ferr
}

return cr.EnvironmentBrowser(ctx)
}
1 change: 1 addition & 0 deletions govc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import (
_ "github.com/vmware/govmomi/govc/vm/option"
_ "github.com/vmware/govmomi/govc/vm/rdm"
_ "github.com/vmware/govmomi/govc/vm/snapshot"
_ "github.com/vmware/govmomi/govc/vm/target"
_ "github.com/vmware/govmomi/govc/volume"
_ "github.com/vmware/govmomi/govc/volume/snapshot"
_ "github.com/vmware/govmomi/govc/vsan"
Expand Down
30 changes: 24 additions & 6 deletions govc/object/save.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Copyright (c) 2019-2023 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 @@ -113,11 +113,29 @@ func saveDVS(ctx context.Context, c *vim25.Client, ref types.ManagedObjectRefere
}

func saveEnvironmentBrowser(ctx context.Context, c *vim25.Client, ref types.ManagedObjectReference) ([]saveMethod, error) {
res, err := methods.QueryConfigOption(ctx, c, &types.QueryConfigOption{This: ref})
if err != nil {
return nil, err
var save []saveMethod
{
res, err := methods.QueryConfigOption(ctx, c, &types.QueryConfigOption{This: ref})
if err != nil {
return nil, err
}
save = append(save, saveMethod{"QueryConfigOption", res})
}
{
res, err := methods.QueryConfigTarget(ctx, c, &types.QueryConfigTarget{This: ref})
if err != nil {
return nil, err
}
save = append(save, saveMethod{"QueryConfigTarget", res})
}
{
res, err := methods.QueryTargetCapabilities(ctx, c, &types.QueryTargetCapabilities{This: ref})
if err != nil {
return nil, err
}
save = append(save, saveMethod{"QueryTargetCapabilities", res})
}
return []saveMethod{{"QueryConfigOption", res}}, nil
return save, nil
}

func saveHostNetworkSystem(ctx context.Context, c *vim25.Client, ref types.ManagedObjectReference) ([]saveMethod, error) {
Expand Down
16 changes: 16 additions & 0 deletions govc/test/vm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,22 @@ load test_helper
[ ${#lines[@]} -ge 100 ]
}

@test "vm.target.info" {
vcsim_env

run govc vm.target.info -host "$GOVC_HOST"
assert_success

run govc vm.target.info -cluster "$(dirname "$GOVC_HOST")"
assert_success

run govc vm.target.info -vm DC0_H0_VM0
assert_success

run govc vm.target.info -json
assert_success
}

@test "vm.customize" {
vcsim_env

Expand Down
Loading

0 comments on commit 524ca25

Please sign in to comment.