Skip to content

Commit 7f9401d

Browse files
committed
govc: Add envbrowser query commands
Add a cli/envbrowser package with three govc commands that wrap the vSphere EnvironmentBrowser managed object API: - envbrowser.query-config-option: query VM hardware config options by hardware version and/or guest ID; supports iterating all known hardware versions (VMX-10+) - envbrowser.query-config-option-descriptor: list available config option descriptor keys - envbrowser.query-config-target: query the config target for a host or cluster All commands accept -host/-cluster to select the compute object and -copy-to-file to serialize the result as XML. The directory cli/envbrowser/testdata also includes examples of all of the output from each command when using the -copy-to-file flag. Signed-off-by: akutz <andrew.kutz@broadcom.com>
1 parent 57d3dfb commit 7f9401d

22 files changed

Lines changed: 285615 additions & 0 deletions

cli/envbrowser/command.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package envbrowser
6+
7+
import (
8+
"context"
9+
"flag"
10+
11+
"github.com/vmware/govmomi/cli/flags"
12+
"github.com/vmware/govmomi/object"
13+
)
14+
15+
type command struct {
16+
*flags.ClusterFlag
17+
*flags.HostSystemFlag
18+
*flags.OutputFlag
19+
20+
hardwareVersion string
21+
guestIDs string
22+
allHardwareVersions bool
23+
copyToFile bool
24+
25+
host *object.HostSystem
26+
cluster *object.ClusterComputeResource
27+
28+
eb *object.EnvironmentBrowser
29+
}
30+
31+
func (cmd *command) Register(ctx context.Context, f *flag.FlagSet) {
32+
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
33+
cmd.OutputFlag.Register(ctx, f)
34+
35+
cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
36+
cmd.ClusterFlag.Register(ctx, f)
37+
38+
cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
39+
cmd.HostSystemFlag.Register(ctx, f)
40+
41+
f.BoolVar(
42+
&cmd.copyToFile,
43+
"copy-to-file",
44+
false,
45+
"True to marshal the result's XML to a file.")
46+
}
47+
48+
func (cmd *command) Process(ctx context.Context) error {
49+
if err := cmd.ClusterFlag.Process(ctx); err != nil {
50+
return err
51+
}
52+
if err := cmd.HostSystemFlag.Process(ctx); err != nil {
53+
return err
54+
}
55+
return nil
56+
}
57+
58+
func (cmd *command) Run(ctx context.Context, f *flag.FlagSet) error {
59+
cluster, err := cmd.ClusterIfSpecified()
60+
if err != nil {
61+
return err
62+
}
63+
cmd.cluster = cluster
64+
65+
host, err := cmd.HostSystemIfSpecified()
66+
if err != nil {
67+
return err
68+
}
69+
cmd.host = host
70+
71+
if cluster != nil {
72+
eb, err := cluster.EnvironmentBrowser(ctx)
73+
if err != nil {
74+
return err
75+
}
76+
cmd.eb = eb
77+
} else if host != nil {
78+
pool, err := host.ResourcePool(ctx)
79+
if err != nil {
80+
return err
81+
}
82+
83+
var cr *object.ComputeResource
84+
85+
crRef, err := pool.Owner(ctx)
86+
if err != nil {
87+
return err
88+
}
89+
90+
switch tCR := crRef.(type) {
91+
case *object.ComputeResource:
92+
cr = tCR
93+
case *object.ClusterComputeResource:
94+
cr = &tCR.ComputeResource
95+
}
96+
97+
eb, err := cr.EnvironmentBrowser(ctx)
98+
if err != nil {
99+
return err
100+
}
101+
cmd.eb = eb
102+
}
103+
104+
return cmd.run(ctx, cmd.eb)
105+
}
106+
107+
func (cmd *command) run(
108+
_ context.Context,
109+
_ *object.EnvironmentBrowser) error {
110+
111+
return nil
112+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package envbrowser
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
"io"
12+
"os"
13+
"strings"
14+
15+
"github.com/vmware/govmomi/cli"
16+
"github.com/vmware/govmomi/vim25/types"
17+
"github.com/vmware/govmomi/vim25/xml"
18+
)
19+
20+
type queryConfigOption struct {
21+
command
22+
hardwareVersion string
23+
guestIDs string
24+
allHardwareVersions bool
25+
}
26+
27+
func init() {
28+
cli.Register("envbrowser.query-config-option", &queryConfigOption{})
29+
}
30+
31+
func (cmd *queryConfigOption) Register(ctx context.Context, f *flag.FlagSet) {
32+
cmd.command.Register(ctx, f)
33+
34+
f.StringVar(
35+
&cmd.hardwareVersion,
36+
"hardware-version",
37+
"vmx-19",
38+
"The hardware version to query.")
39+
40+
f.StringVar(
41+
&cmd.guestIDs,
42+
"guest-ids",
43+
"",
44+
"A comma-delimited list of guest IDs.")
45+
46+
f.BoolVar(
47+
&cmd.allHardwareVersions,
48+
"all-hardware-versions",
49+
false,
50+
"True to query all hardware versions.")
51+
}
52+
53+
func (cmd *queryConfigOption) Description() string {
54+
return `Query the environment browser for a config option.
55+
56+
Examples:
57+
govc envbrowser.query-config-option -cluster my-cluster
58+
govc envbrowser.query-config-option -cluster my-cluster -host my-host
59+
govc envbrowser.query-config-option -cluster my-cluster -hardware-version vmx-22
60+
govc envbrowser.query-config-option -cluster my-cluster -hardware-version vmx-22 -guest-ids otherLinuxGuest,ubuntu64Guest
61+
govc envbrowser.query-config-option -cluster my-cluster -all-hardware-versions
62+
govc envbrowser.query-config-option -cluster my-cluster -hardware-version vmx-22 -copy-to-file
63+
govc envbrowser.query-config-option -cluster my-cluster -all-hardware-versions -copy-to-file`
64+
}
65+
66+
func (cmd *queryConfigOption) Run(ctx context.Context, f *flag.FlagSet) error {
67+
if err := cmd.command.Run(ctx, f); err != nil {
68+
return err
69+
}
70+
71+
if cmd.allHardwareVersions {
72+
for _, v := range types.GetHardwareVersions() {
73+
if v < types.VMX10 {
74+
continue
75+
}
76+
r, err := cmd.eb.QueryConfigOption(
77+
ctx,
78+
&types.EnvironmentBrowserConfigOptionQuerySpec{
79+
Key: v.String(),
80+
GuestId: strings.Split(cmd.guestIDs, ","),
81+
})
82+
if err != nil {
83+
return fmt.Errorf(
84+
"failed to get config options for key=%q, guestId=%q: %w",
85+
v.String(),
86+
cmd.guestIDs,
87+
err)
88+
}
89+
if err := cmd.WriteResult(queryConfigOptionResult{
90+
r: r,
91+
copyToFile: cmd.copyToFile,
92+
}); err != nil {
93+
94+
return err
95+
}
96+
}
97+
return nil
98+
}
99+
100+
var hostRef *types.ManagedObjectReference
101+
if cmd.host != nil {
102+
ref := cmd.host.Reference()
103+
hostRef = &ref
104+
}
105+
106+
r, err := cmd.eb.QueryConfigOption(
107+
ctx,
108+
&types.EnvironmentBrowserConfigOptionQuerySpec{
109+
Key: cmd.hardwareVersion,
110+
GuestId: strings.Split(cmd.guestIDs, ","),
111+
Host: hostRef,
112+
})
113+
if err != nil {
114+
return fmt.Errorf(
115+
"failed to get config options for key=%q, guestId=%q: %w",
116+
cmd.hardwareVersion,
117+
cmd.guestIDs,
118+
err)
119+
}
120+
121+
return cmd.WriteResult(queryConfigOptionResult{
122+
r: r,
123+
copyToFile: cmd.copyToFile,
124+
})
125+
}
126+
127+
type queryConfigOptionResult struct {
128+
r *types.VirtualMachineConfigOption
129+
copyToFile bool
130+
}
131+
132+
func (r queryConfigOptionResult) Write(w io.Writer) error {
133+
fmt.Println(r.r.Version)
134+
if r.copyToFile {
135+
136+
f, err := os.Create("config-option-" + r.r.Version + ".xml")
137+
if err != nil {
138+
return err
139+
}
140+
defer f.Close()
141+
enc := xml.NewEncoder(f)
142+
enc.Indent("", " ")
143+
return enc.Encode(r.r)
144+
}
145+
return nil
146+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package envbrowser
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
"io"
12+
"os"
13+
14+
"github.com/vmware/govmomi/cli"
15+
"github.com/vmware/govmomi/vim25/types"
16+
"github.com/vmware/govmomi/vim25/xml"
17+
)
18+
19+
type queryConfigOptionDescriptor struct {
20+
command
21+
}
22+
23+
func init() {
24+
cli.Register("envbrowser.query-config-option-descriptor", &queryConfigOptionDescriptor{})
25+
}
26+
27+
func (cmd *queryConfigOptionDescriptor) Description() string {
28+
return `Query the environment browser for the config descriptors.
29+
30+
Examples:
31+
govc envbrowser.query-config-option-descriptor -cluster my-cluster`
32+
}
33+
34+
func (cmd *queryConfigOptionDescriptor) Run(
35+
ctx context.Context,
36+
f *flag.FlagSet) error {
37+
38+
if err := cmd.command.Run(ctx, f); err != nil {
39+
return err
40+
}
41+
42+
r, err := cmd.eb.QueryConfigOptionDescriptor(ctx)
43+
if err != nil {
44+
return fmt.Errorf("failed to get config option descriptor: %w", err)
45+
}
46+
47+
return cmd.WriteResult(queryConfigOptionDescriptorResult{
48+
r: r,
49+
copyToFile: cmd.copyToFile,
50+
})
51+
}
52+
53+
type queryConfigOptionDescriptorResult struct {
54+
r []types.VirtualMachineConfigOptionDescriptor
55+
copyToFile bool
56+
}
57+
58+
func (r queryConfigOptionDescriptorResult) Write(w io.Writer) error {
59+
for _, r := range r.r {
60+
fmt.Println(r.Key)
61+
}
62+
if r.copyToFile {
63+
f, err := os.Create("config-option-descriptor.xml")
64+
if err != nil {
65+
return err
66+
}
67+
defer f.Close()
68+
enc := xml.NewEncoder(f)
69+
enc.Indent("", " ")
70+
return enc.Encode(r.r)
71+
}
72+
return nil
73+
}

0 commit comments

Comments
 (0)