Skip to content

Commit

Permalink
govc: support updating items in library.update command
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Jul 29, 2021
1 parent 7822b34 commit 012f534
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
41 changes: 30 additions & 11 deletions govc/library/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package library
import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
Expand All @@ -28,7 +29,7 @@ import (
type update struct {
*flags.ClientFlag

library.Library
name, desc string
}

func init() {
Expand All @@ -39,19 +40,20 @@ func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

f.StringVar(&cmd.Library.Name, "n", "", "Library name")
f.StringVar(&cmd.Library.Description, "d", "", "Library description")
f.StringVar(&cmd.name, "n", "", "Library or item name")
f.StringVar(&cmd.desc, "d", "", "Library or item description")
}

func (cmd *update) Usage() string {
return "NAME"
return "PATH"
}

func (cmd *update) Description() string {
return `Update library.
return `Update library or item PATH.
Examples:
govc library.update -d "new description" -n "new-name" current-name`
govc library.update -d "new library description" -n "new-name" my-library
govc library.update -d "new item description" -n "new-item-name" my-library/my-item`
}

func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
Expand All @@ -65,13 +67,30 @@ func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
}

m := library.NewManager(c)
dst, err := flags.ContentLibrary(ctx, c, f.Arg(0))

res, err := flags.ContentLibraryResult(ctx, c, "", f.Arg(0))
if err != nil {
return err
}

cmd.ID = dst.ID
dst.Patch(&cmd.Library)

return m.UpdateLibrary(ctx, dst)
switch t := res.GetResult().(type) {
case library.Library:
lib := &library.Library{
ID: t.ID,
Name: cmd.name,
Description: cmd.desc,
}
t.Patch(lib)
return m.UpdateLibrary(ctx, &t)
case library.Item:
item := &library.Item{
ID: t.ID,
Name: cmd.name,
Description: cmd.desc,
}
t.Patch(item)
return m.UpdateLibraryItem(ctx, item)
default:
return fmt.Errorf("%q is a %T", f.Arg(0), t)
}
}
4 changes: 3 additions & 1 deletion govc/test/library.bats
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ load test_helper
# test where $name.{ovf,mf} differs from ova name
run govc library.import -m my-content "$GOVC_IMAGES/ttylinux-latest.ova"
assert_success
run govc library.ls "/my-content/ttylinux-latest/*"
run govc library.update -n ttylinux_latest my-content/ttylinux-latest
assert_success
run govc library.ls "/my-content/ttylinux_latest/*"
assert_success
assert_matches "$TTYLINUX_NAME.ovf"
assert_matches "$TTYLINUX_NAME-disk1.vmdk"
Expand Down
14 changes: 14 additions & 0 deletions vapi/library/library_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ func (c *Manager) PublishLibraryItem(ctx context.Context, item *Item, force bool
return c.Do(ctx, url.Request(http.MethodPost, body), nil)
}

// UpdateLibraryItem can update one or both of the item Description and Name fields.
func (c *Manager) UpdateLibraryItem(ctx context.Context, item *Item) error {
spec := struct {
Item `json:"update_spec"`
}{
Item{
Name: item.Name,
Description: item.Description,
},
}
url := c.Resource(internal.LibraryItemPath).WithID(item.ID)
return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
}

// DeleteLibraryItem deletes an existing library item.
func (c *Manager) DeleteLibraryItem(ctx context.Context, item *Item) error {
url := c.Resource(internal.LibraryItemPath).WithID(item.ID)
Expand Down

0 comments on commit 012f534

Please sign in to comment.