-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3682 from dougm/vcsim-vslm
vcsim: add disk query and metadata support
- Loading branch information
Showing
12 changed files
with
1,049 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// © Broadcom. All Rights Reserved. | ||
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metadata | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"text/tabwriter" | ||
|
||
"github.com/vmware/govmomi/cli" | ||
"github.com/vmware/govmomi/cli/flags" | ||
"github.com/vmware/govmomi/vim25/types" | ||
"github.com/vmware/govmomi/vslm" | ||
) | ||
|
||
type ls struct { | ||
*flags.OutputFlag | ||
*flags.ClientFlag | ||
|
||
key string | ||
prefix string | ||
snapshot string | ||
} | ||
|
||
func init() { | ||
cli.Register("disk.metadata.ls", &ls{}) | ||
} | ||
|
||
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) | ||
cmd.OutputFlag.Register(ctx, f) | ||
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) | ||
cmd.ClientFlag.Register(ctx, f) | ||
|
||
f.StringVar(&cmd.key, "K", "", "Get value for key only") | ||
f.StringVar(&cmd.prefix, "p", "", "Key filter prefix") | ||
f.StringVar(&cmd.snapshot, "s", "", "Snapshot ID") | ||
} | ||
|
||
func (cmd *ls) Process(ctx context.Context) error { | ||
if err := cmd.OutputFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
return cmd.ClientFlag.Process(ctx) | ||
} | ||
|
||
func (cmd *ls) Usage() string { | ||
return "ID" | ||
} | ||
|
||
func (cmd *ls) Description() string { | ||
return `List metadata for disk ID. | ||
Examples: | ||
govc disk.metadata.ls 9b06a8b-d047-4d3c-b15b-43ea9608b1a6` | ||
} | ||
|
||
type lsResult []types.KeyValue | ||
|
||
func (r lsResult) Write(w io.Writer) error { | ||
tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) | ||
for _, data := range r { | ||
fmt.Fprintf(tw, "%s\t%s\n", data.Key, data.Value) | ||
} | ||
return tw.Flush() | ||
} | ||
|
||
func (r lsResult) Dump() interface{} { | ||
return []types.KeyValue(r) | ||
} | ||
|
||
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { | ||
if f.NArg() != 1 { | ||
return flag.ErrHelp | ||
} | ||
|
||
c, err := cmd.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vc, err := vslm.NewClient(ctx, c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m := vslm.NewGlobalObjectManager(vc) | ||
|
||
id := types.ID{Id: f.Arg(0)} | ||
var data []types.KeyValue | ||
var sid *types.ID | ||
if cmd.snapshot != "" { | ||
sid = &types.ID{Id: cmd.snapshot} | ||
} | ||
|
||
if cmd.key != "" { | ||
val, err := m.RetrieveMetadataValue(ctx, id, sid, cmd.key) | ||
if err != nil { | ||
return err | ||
} | ||
data = []types.KeyValue{{Key: cmd.key, Value: val}} | ||
} else { | ||
data, err = m.RetrieveMetadata(ctx, id, sid, cmd.prefix) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return cmd.WriteResult(lsResult(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// © Broadcom. All Rights Reserved. | ||
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metadata | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"strings" | ||
"time" | ||
|
||
"github.com/vmware/govmomi/cli" | ||
"github.com/vmware/govmomi/cli/flags" | ||
"github.com/vmware/govmomi/vim25/types" | ||
"github.com/vmware/govmomi/vslm" | ||
) | ||
|
||
type update struct { | ||
*flags.ClientFlag | ||
|
||
remove flags.StringList | ||
} | ||
|
||
func init() { | ||
cli.Register("disk.metadata.update", &update{}) | ||
} | ||
|
||
func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) | ||
cmd.ClientFlag.Register(ctx, f) | ||
|
||
f.Var(&cmd.remove, "d", "Delete keys") | ||
} | ||
|
||
func (cmd *update) Usage() string { | ||
return "ID" | ||
} | ||
|
||
func (cmd *update) Description() string { | ||
return `Update metadata for disk ID. | ||
Examples: | ||
govc disk.metadata.update $id foo=bar biz=baz | ||
govc disk.metadata.update -d foo -d biz $id` | ||
} | ||
|
||
func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error { | ||
c, err := cmd.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vc, err := vslm.NewClient(ctx, c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m := vslm.NewGlobalObjectManager(vc) | ||
|
||
id := types.ID{Id: f.Arg(0)} | ||
|
||
var update []types.KeyValue | ||
|
||
for _, arg := range f.Args()[1:] { | ||
kv := strings.SplitN(arg, "=", 2) | ||
if len(kv) == 1 { | ||
kv = append(kv, "") | ||
} | ||
update = append(update, types.KeyValue{Key: kv[0], Value: kv[1]}) | ||
} | ||
|
||
task, err := m.UpdateMetadata(ctx, id, update, cmd.remove) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = task.Wait(ctx, time.Hour) | ||
return err | ||
} |
Oops, something went wrong.