Skip to content

Commit

Permalink
vcsim: add ExtensionManager.FindExtension method
Browse files Browse the repository at this point in the history
d9af2a2 added simulator.ExtensionManager but did not included this method.

govc: use ExtensionManager.FindExtension when given a single KEY arg,
rather than fetch the entire list.

Closes #2644
  • Loading branch information
dougm committed Apr 12, 2024
1 parent d528fec commit cf71944
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
46 changes: 27 additions & 19 deletions govc/extension/info.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2024 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.
Expand All @@ -21,7 +21,6 @@ import (
"flag"
"fmt"
"io"
"os"
"text/tabwriter"

"github.com/vmware/govmomi/govc/cli"
Expand Down Expand Up @@ -72,30 +71,39 @@ func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

list, err := m.List(ctx)
if err != nil {
return err
}

var res infoResult
exts := make(map[string]types.Extension)

if f.NArg() == 0 {
res.Extensions = list
if f.NArg() == 1 {
e, err := m.Find(ctx, f.Arg(0))
if err != nil {
return err
}
if e != nil {
exts[f.Arg(0)] = *e
}
} else {
exts := make(map[string]types.Extension)
for _, e := range list {
exts[e.Key] = e
list, err := m.List(ctx)
if err != nil {
return err
}

for _, key := range f.Args() {
if e, ok := exts[key]; ok {
res.Extensions = append(res.Extensions, e)
} else {
return fmt.Errorf("extension %s not found", key)
if f.NArg() == 0 {
res.Extensions = list
} else {
for _, e := range list {
exts[e.Key] = e
}
}
}

for _, key := range f.Args() {
if e, ok := exts[key]; ok {
res.Extensions = append(res.Extensions, e)
} else {
return fmt.Errorf("extension %s not found", key)
}
}

return cmd.WriteResult(&res)
}

Expand All @@ -104,7 +112,7 @@ type infoResult struct {
}

func (r *infoResult) Write(w io.Writer) error {
tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)

for _, e := range r.Extensions {
fmt.Fprintf(tw, "Name:\t%s\n", e.Key)
Expand Down
13 changes: 8 additions & 5 deletions govc/test/extension.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ load test_helper
run govc extension.info enoent
assert_failure

id=$(new_id)
run govc extension.info
assert_success

result=$(govc extension.info | grep $id | wc -l)
[ $result -eq 0 ]
id=$(new_id)

# register extension
run govc extension.register $id <<EOS
Expand Down Expand Up @@ -54,9 +54,12 @@ EOS
# remove generated cert and key
rm ${id}.{crt,key}

run govc extension.info $(govc extension.info -json | jq -r .extensions[].key)
assert_success

run govc extension.unregister $id
assert_success

result=$(govc extension.info | grep $id | wc -l)
[ $result -eq 0 ]
run govc extension.info $id
assert_failure
}
15 changes: 15 additions & 0 deletions simulator/extension_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ func (m *ExtensionManager) init(r *Registry) {
}
}

func (m *ExtensionManager) FindExtension(ctx *Context, req *types.FindExtension) soap.HasFault {
body := &methods.FindExtensionBody{
Res: new(types.FindExtensionResponse),
}

for _, x := range m.ExtensionList {
if x.Key == req.ExtensionKey {
body.Res.Returnval = &x
break
}
}

return body
}

func (m *ExtensionManager) RegisterExtension(ctx *Context, req *types.RegisterExtension) soap.HasFault {
body := &methods.RegisterExtensionBody{}

Expand Down

0 comments on commit cf71944

Please sign in to comment.