1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
Copyright (c) 2018 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 finder
import (
"context"
"encoding/json"
"fmt"
"path"
"strings"
"github.com/vmware/govmomi/vapi/library"
)
// Finder is a helper object for finding content library objects by their
// inventory paths: /LIBRARY/ITEM/FILE.
//
// Wildcard characters `*` and `?` are both supported. However, the use
// of a wildcard character in the search string results in a full listing of
// that part of the path's server-side items.
//
// Path parts that do not use wildcard characters rely on server-side Find
// functions to find the path token by its name. Ironically finding one
// item with a direct path takes longer than if a wildcard is used because
// of the multiple round-trips. Direct paths will be more performant on
// systems that have numerous items.
type Finder struct {
M *library.Manager
}
// NewFinder returns a new Finder.
func NewFinder(m *library.Manager) *Finder {
return &Finder{m}
}
// Find finds one or more items that match the provided inventory path(s).
func (f *Finder) Find(
ctx context.Context, ipath ...string) ([]FindResult, error) {
if len(ipath) == 0 {
ipath = []string{""}
}
var result []FindResult
for _, p := range ipath {
results, err := f.find(ctx, p)
if err != nil {
return nil, err
}
result = append(result, results...)
}
return result, nil
}
func (f *Finder) find(ctx context.Context, ipath string) ([]FindResult, error) {
if ipath == "" {
ipath = "*"
}
// Get the argument and remove any leading separator characters.
ipath = strings.TrimPrefix(ipath, "/")
// Tokenize the path into its distinct parts.
parts := strings.Split(ipath, "/")
// If there are more than three parts then the file name contains
// the "/" character. In that case collapse any additional parts
// back into the filename.
if len(parts) > 3 {
parts = []string{
parts[0],
parts[1],
strings.Join(parts[2:], "/"),
}
}
libs, err := f.findLibraries(ctx, parts[0])
if err != nil {
return nil, err
}
// If the path is a single token then the libraries are requested.
if len(parts) < 2 {
return libs, nil
}
items, err := f.findLibraryItems(ctx, libs, parts[1])
if err != nil {
return nil, err
}
// If the path is two tokens then the library items are requested.
if len(parts) < 3 {
return items, nil
}
// Get the library item files.
return f.findLibraryItemFiles(ctx, items, parts[2])
}
// FindResult is the type of object returned from a Find operation.
type FindResult interface {
// GetParent returns the parent of the find result. If the find result
// is a Library then this function will return nil.
GetParent() FindResult
// GetPath returns the inventory path of the find result.
GetPath() string
// GetID returns the ID of the find result.
GetID() string
// GetName returns the name of the find result.
GetName() string
// GetResult gets the underlying library object.
GetResult() interface{}
}
type findResult struct {
result interface{}
parent FindResult
}
func (f findResult) GetResult() interface{} {
return f.result
}
func (f findResult) GetParent() FindResult {
return f.parent
}
func (f findResult) GetPath() string {
switch f.result.(type) {
case library.Library:
return fmt.Sprintf("/%s", f.GetName())
case library.Item, library.File:
return fmt.Sprintf("%s/%s", f.parent.GetPath(), f.GetName())
default:
return ""
}
}
func (f findResult) GetID() string {
switch t := f.result.(type) {
case library.Library:
return t.ID
case library.Item:
return t.ID
default:
return ""
}
}
func (f findResult) GetName() string {
switch t := f.result.(type) {
case library.Library:
return t.Name
case library.Item:
return t.Name
case library.File:
return t.Name
default:
return ""
}
}
func (f findResult) MarshalJSON() ([]byte, error) {
return json.Marshal(f.GetResult())
}
func (f *Finder) findLibraries(
ctx context.Context,
token string) ([]FindResult, error) {
if token == "" {
token = "*"
}
var result []FindResult
// If the token does not contain any wildcard characters then perform
// a lookup by name using a server side call.
if !strings.ContainsAny(token, "*?") {
libIDs, err := f.M.FindLibrary(ctx, library.Find{Name: token})
if err != nil {
return nil, err
}
for _, id := range libIDs {
lib, err := f.M.GetLibraryByID(ctx, id)
if err != nil {
return nil, err
}
result = append(result, findResult{result: *lib})
}
if len(result) == 0 {
lib, err := f.M.GetLibraryByID(ctx, token)
if err == nil {
result = append(result, findResult{result: *lib})
}
}
return result, nil
}
libs, err := f.M.GetLibraries(ctx)
if err != nil {
return nil, err
}
for _, lib := range libs {
match, err := path.Match(token, lib.Name)
if err != nil {
return nil, err
}
if match {
result = append(result, findResult{result: lib})
}
}
return result, nil
}
func (f *Finder) findLibraryItems(
ctx context.Context,
parents []FindResult, token string) ([]FindResult, error) {
if token == "" {
token = "*"
}
var result []FindResult
for _, parent := range parents {
// If the token does not contain any wildcard characters then perform
// a lookup by name using a server side call.
if !strings.ContainsAny(token, "*?") {
childIDs, err := f.M.FindLibraryItems(
ctx, library.FindItem{
Name: token,
LibraryID: parent.GetID(),
})
if err != nil {
return nil, err
}
for _, id := range childIDs {
child, err := f.M.GetLibraryItem(ctx, id)
if err != nil {
return nil, err
}
result = append(result, findResult{
result: *child,
parent: parent,
})
}
continue
}
children, err := f.M.GetLibraryItems(ctx, parent.GetID())
if err != nil {
return nil, err
}
for _, child := range children {
match, err := path.Match(token, child.Name)
if err != nil {
return nil, err
}
if match {
result = append(
result, findResult{parent: parent, result: child})
}
}
}
return result, nil
}
func (f *Finder) findLibraryItemFiles(
ctx context.Context,
parents []FindResult, token string) ([]FindResult, error) {
if token == "" {
token = "*"
}
var result []FindResult
for _, parent := range parents {
// If the token does not contain any wildcard characters then perform
// a lookup by name using a server side call.
if !strings.ContainsAny(token, "*?") {
child, err := f.M.GetLibraryItemFile(ctx, parent.GetID(), token)
if err != nil {
return nil, err
}
result = append(result, findResult{
result: *child,
parent: parent,
})
continue
}
children, err := f.M.ListLibraryItemFiles(ctx, parent.GetID())
if err != nil {
return nil, err
}
for _, child := range children {
match, err := path.Match(token, child.Name)
if err != nil {
return nil, err
}
if match {
result = append(
result, findResult{parent: parent, result: child})
}
}
}
return result, nil
}
|