Skip to content

Commit

Permalink
govc: Add library.evict command
Browse files Browse the repository at this point in the history
  • Loading branch information
acharyasreej committed Apr 12, 2024
1 parent f671048 commit 9285e44
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
79 changes: 79 additions & 0 deletions govc/library/evict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright (c) 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.
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 library

import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vapi/library"
)

type evict struct {
*flags.ClientFlag
}

func init() {
cli.Register("library.evict", &evict{})
}

func (cmd *evict) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
}

func (cmd *evict) Usage() string {
return "LIBRARY NAME | ITEM NAME"
}

func (cmd *evict) Description() string {
return `Evict library NAME or item NAME.
Examples:
govc library.evict subscribed-library
govc library.evict subscribed-library/item`
}

func (cmd *evict) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}

c, err := cmd.RestClient()
if err != nil {
return err
}

m := library.NewManager(c)

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

switch t := res.GetResult().(type) {
case library.Library:
return m.EvictSubscribedLibrary(ctx, &t)
case library.Item:
return m.EvictSubscribedLibraryItem(ctx, &t)
default:
return fmt.Errorf("%q is a %T", f.Arg(0), t)
}
}
1 change: 1 addition & 0 deletions govc/library/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func (r infoResultsWriter) writeItem(
}
fmt.Fprintf(w, " Type:\t%s\n", v.Type)
fmt.Fprintf(w, " Size:\t%s\n", units.ByteSize(v.Size))
fmt.Fprintf(w, " Cached:\t%t\n", v.Cached)
fmt.Fprintf(w, " Created:\t%s\n", v.CreationTime.Format(time.ANSIC))
fmt.Fprintf(w, " Modified:\t%s\n", v.LastModifiedTime.Format(time.ANSIC))
fmt.Fprintf(w, " Version:\t%s\n", v.Version)
Expand Down
44 changes: 44 additions & 0 deletions govc/test/library.bats
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,47 @@ EOF
assert_success
assert_matches INVALID_URL
}

@test "library.sublibevict" {
vcsim_env

run govc library.create -pub published-content
assert_success
id="$output"

url="https://$(govc env GOVC_URL)/cls/vcsp/lib/$id"

run govc library.info published-content
assert_success
assert_matches "Publication:"
assert_matches "$url"

run govc library.import published-content "$GOVC_IMAGES/ttylinux-latest.ova"
assert_success

run govc library.create -sub "$url" -sub-ondemand=true subscribed-content
assert_success

run govc library.info subscribed-content
assert_success
assert_matches "Subscription:"
assert_matches "$url"

run govc library.ls subscribed-content/ttylinux-latest/
assert_success
assert_matches "/subscribed-content/ttylinux-latest/ttylinux-pc_i486-16.1.ovf"

run govc library.sync subscribed-content/ttylinux-latest
assert_success

result=$(govc library.info -l subscribed-content/ttylinux-latest)
echo "$result"
# assert cached is true

run govc library.evict subscribed-content
assert_success

# assert cached is false
}


8 changes: 8 additions & 0 deletions vapi/library/library_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,11 @@ func (c *Manager) FindLibraryItems(
var res []string
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
}

// EvictSubscribedLibraryItem evicts the cached content of a library item in an on-demand subscribed library.
// This operation allows the cached content of a subscribed library item to be removed to free up storage capacity.
func (c *Manager) EvictSubscribedLibraryItem(ctx context.Context, item *Item) error {
path := internal.SubscribedLibraryItem
url := c.Resource(path).WithID(item.ID).WithAction("evict")
return c.Do(ctx, url.Request(http.MethodPost), nil)
}

0 comments on commit 9285e44

Please sign in to comment.