Skip to content

Commit

Permalink
api: Support returning DS info when resolving lib stor
Browse files Browse the repository at this point in the history
This patch updates the ResolveLibraryItemStorage method with the
ability to return the resolve datastore information beyond just the
resovled URIs. Now the datastore name, URL, and whether it supports
top-level directory create can be returned.

BREAKING: This change updates the signature for the
          ResolveLibraryItemStorage function.

Signed-off-by: akutz <akutz@vmware.com>
  • Loading branch information
akutz committed Dec 20, 2024
1 parent 078eb5b commit aa8279b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cli/library/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (r infoResultsWriter) writeFile(
}
if r.cmd.Stor {
label = "Resolved URI"
err = r.cmd.pathFinder.ResolveLibraryItemStorage(r.ctx, nil, s)
err = r.cmd.pathFinder.ResolveLibraryItemStorage(r.ctx, nil, nil, s)
if err != nil {
return err
}
Expand Down
13 changes: 10 additions & 3 deletions vapi/library/finder/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func (f *PathFinder) convertPath(
// to the format that includes the datastore name, ex.
// "[DATASTORE_NAME] contentlib-LIB_UUID/ITEM_UUID/file.vmdk".
//
// If datastoreMap is provided, then it will be updated with the datastores
// involved in the resolver. The properties name, summary.url, and
// capability.topLevelDirectoryCreateSupported will be available after the
// resolver completes.
//
// If a storage item resides on a datastore that does not support the creation
// of top-level directories, then this means the datastore is vSAN and the
// storage item path needs to be further converted. If this occurs, then the
Expand All @@ -187,16 +192,18 @@ func (f *PathFinder) convertPath(
func (f *PathFinder) ResolveLibraryItemStorage(
ctx context.Context,
datacenter *object.Datacenter,
datastoreMap map[string]mo.Datastore,
storage []library.Storage) error {

// TODO:
// - reuse PathFinder.cache
// - the transform here isn't Content Library specific, but is currently
// the only known use case
var (
ids []types.ManagedObjectReference
var ids []types.ManagedObjectReference

if datastoreMap == nil {
datastoreMap = map[string]mo.Datastore{}
)
}

// Currently ContentLibrary only supports a single storage backing, but this
// future proofs things.
Expand Down
43 changes: 41 additions & 2 deletions vapi/library/finder/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,55 @@ func TestResolveLibraryItemStorage(t *testing.T) {
testCases := []struct {
name string
nilDatacenter bool
datastoreMap map[string]mo.Datastore
topLevelDirectoryCreateSupported *bool
}{
{
name: "Nil datacenter and nil topLevelCreate",
nilDatacenter: true,
datastoreMap: nil,
topLevelDirectoryCreateSupported: nil,
},
{
name: "Nil datacenter and false topLevelCreate",
nilDatacenter: true,
datastoreMap: nil,
topLevelDirectoryCreateSupported: types.New(false),
},
{
name: "Nil datacenter and true topLevelCreate",
nilDatacenter: true,
datastoreMap: nil,
topLevelDirectoryCreateSupported: types.New(true),
},
{
name: "Non-nil datacenter and nil topLevelCreate",
nilDatacenter: false,
datastoreMap: nil,
topLevelDirectoryCreateSupported: nil,
},
{
name: "Non-Nil datacenter and false topLevelCreate",
nilDatacenter: false,
datastoreMap: nil,
topLevelDirectoryCreateSupported: types.New(false),
},
{
name: "Non-Nil datacenter and true topLevelCreate",
nilDatacenter: false,
datastoreMap: nil,
topLevelDirectoryCreateSupported: types.New(true),
},
{
name: "Nil datastoreMap",
nilDatacenter: true,
datastoreMap: nil,
topLevelDirectoryCreateSupported: nil,
},
{
name: "Non-Nil datastoreMap and true topLevelCreate",
nilDatacenter: true,
datastoreMap: map[string]mo.Datastore{},
topLevelDirectoryCreateSupported: types.New(true),
},
}
Expand Down Expand Up @@ -105,7 +124,7 @@ func TestResolveLibraryItemStorage(t *testing.T) {
ds.Properties(
ctx,
ds.Reference(),
[]string{"name", "summary"},
[]string{"name", "summary.url"},
&moDS)) {
t.FailNow()
}
Expand Down Expand Up @@ -142,16 +161,36 @@ func TestResolveLibraryItemStorage(t *testing.T) {
ds.Capability.TopLevelDirectoryCreateSupported = tc.topLevelDirectoryCreateSupported
})

nilDSM := tc.datastoreMap == nil

if !assert.NoError(
t,
lf.ResolveLibraryItemStorage(ctx, dc, storage)) {
lf.ResolveLibraryItemStorage(
ctx,
dc,
tc.datastoreMap,
storage)) {

t.FailNow()
}

assert.Len(t, storage, 1)
assert.Len(t, storage[0].StorageURIs, 2)

if nilDSM {
assert.Nil(t, tc.datastoreMap)
} else if assert.NotNil(t, tc.datastoreMap) {
if assert.Len(t, tc.datastoreMap, 1) {
dsv := ds.Reference().Value
if assert.Contains(t, tc.datastoreMap, dsv) {
ds := tc.datastoreMap[dsv]
assert.Equal(t, ds.Name, dsName)
assert.Equal(t, ds.Summary.Url, dsURL)
assert.Equal(t, ds.Capability.TopLevelDirectoryCreateSupported, tc.topLevelDirectoryCreateSupported)
}
}
}

for _, s := range storage {
for _, u := range s.StorageURIs {
var path object.DatastorePath
Expand Down

0 comments on commit aa8279b

Please sign in to comment.