recurser.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Copyright (c) 2014-2017 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package find
  14. import (
  15. "context"
  16. "os"
  17. "path"
  18. "strings"
  19. "github.com/vmware/govmomi/list"
  20. "github.com/vmware/govmomi/object"
  21. "github.com/vmware/govmomi/property"
  22. "github.com/vmware/govmomi/vim25/mo"
  23. )
  24. // spec is used to specify per-search configuration, independent of the Finder instance.
  25. type spec struct {
  26. // Relative returns the root object to resolve Relative paths (starts with ".")
  27. Relative func(ctx context.Context) (object.Reference, error)
  28. // ListMode can be used to optionally force "ls" behavior, rather than "find" behavior
  29. ListMode *bool
  30. // Contents configures the Recurser to list the Contents of traversable leaf nodes.
  31. // This is typically set to true when used from the ls command, where listing
  32. // a folder means listing its Contents. This is typically set to false for
  33. // commands that take managed entities that are not folders as input.
  34. Contents bool
  35. // Parents specifies the types which can contain the child types being searched for.
  36. // for example, when searching for a HostSystem, parent types can be
  37. // "ComputeResource" or "ClusterComputeResource".
  38. Parents []string
  39. // Include specifies which types to be included in the results, used only in "find" mode.
  40. Include []string
  41. // Nested should be set to types that can be Nested, used only in "find" mode.
  42. Nested []string
  43. // ChildType avoids traversing into folders that can't contain the Include types, used only in "find" mode.
  44. ChildType []string
  45. }
  46. func (s *spec) traversable(o mo.Reference) bool {
  47. ref := o.Reference()
  48. switch ref.Type {
  49. case "Datacenter":
  50. if len(s.Include) == 1 && s.Include[0] == "Datacenter" {
  51. // No point in traversing deeper as Datacenters cannot be nested
  52. return false
  53. }
  54. return true
  55. case "Folder":
  56. if f, ok := o.(mo.Folder); ok {
  57. // TODO: Not making use of this yet, but here we can optimize when searching the entire
  58. // inventory across Datacenters for specific types, for example: 'govc ls -t VirtualMachine /**'
  59. // should not traverse into a Datacenter's host, network or datatore folders.
  60. if !s.traversableChildType(f.ChildType) {
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. for _, kind := range s.Parents {
  67. if kind == ref.Type {
  68. return true
  69. }
  70. }
  71. return false
  72. }
  73. func (s *spec) traversableChildType(ctypes []string) bool {
  74. if len(s.ChildType) == 0 {
  75. return true
  76. }
  77. for _, t := range ctypes {
  78. for _, c := range s.ChildType {
  79. if t == c {
  80. return true
  81. }
  82. }
  83. }
  84. return false
  85. }
  86. func (s *spec) wanted(e list.Element) bool {
  87. if len(s.Include) == 0 {
  88. return true
  89. }
  90. w := e.Object.Reference().Type
  91. for _, kind := range s.Include {
  92. if w == kind {
  93. return true
  94. }
  95. }
  96. return false
  97. }
  98. // listMode is a global option to revert to the original Finder behavior,
  99. // disabling the newer "find" mode.
  100. var listMode = os.Getenv("GOVMOMI_FINDER_LIST_MODE") == "true"
  101. func (s *spec) listMode(isPath bool) bool {
  102. if listMode {
  103. return true
  104. }
  105. if s.ListMode != nil {
  106. return *s.ListMode
  107. }
  108. return isPath
  109. }
  110. type recurser struct {
  111. Collector *property.Collector
  112. // All configures the recurses to fetch complete objects for leaf nodes.
  113. All bool
  114. }
  115. func (r recurser) List(ctx context.Context, s *spec, root list.Element, parts []string) ([]list.Element, error) {
  116. if len(parts) == 0 {
  117. // Include non-traversable leaf elements in result. For example, consider
  118. // the pattern "./vm/my-vm-*", where the pattern should match the VMs and
  119. // not try to traverse them.
  120. //
  121. // Include traversable leaf elements in result, if the contents
  122. // field is set to false.
  123. //
  124. if !s.Contents || !s.traversable(root.Object.Reference()) {
  125. return []list.Element{root}, nil
  126. }
  127. }
  128. k := list.Lister{
  129. Collector: r.Collector,
  130. Reference: root.Object.Reference(),
  131. Prefix: root.Path,
  132. }
  133. if r.All && len(parts) < 2 {
  134. k.All = true
  135. }
  136. in, err := k.List(ctx)
  137. if err != nil {
  138. return nil, err
  139. }
  140. // This folder is a leaf as far as the glob goes.
  141. if len(parts) == 0 {
  142. return in, nil
  143. }
  144. all := parts
  145. pattern := parts[0]
  146. parts = parts[1:]
  147. var out []list.Element
  148. for _, e := range in {
  149. matched, err := path.Match(pattern, path.Base(e.Path))
  150. if err != nil {
  151. return nil, err
  152. }
  153. if !matched {
  154. matched = strings.HasSuffix(e.Path, "/"+path.Join(all...))
  155. if matched {
  156. // name contains a '/'
  157. out = append(out, e)
  158. }
  159. continue
  160. }
  161. nres, err := r.List(ctx, s, e, parts)
  162. if err != nil {
  163. return nil, err
  164. }
  165. out = append(out, nres...)
  166. }
  167. return out, nil
  168. }
  169. func (r recurser) Find(ctx context.Context, s *spec, root list.Element, parts []string) ([]list.Element, error) {
  170. var out []list.Element
  171. if len(parts) > 0 {
  172. pattern := parts[0]
  173. matched, err := path.Match(pattern, path.Base(root.Path))
  174. if err != nil {
  175. return nil, err
  176. }
  177. if matched && s.wanted(root) {
  178. out = append(out, root)
  179. }
  180. }
  181. if !s.traversable(root.Object) {
  182. return out, nil
  183. }
  184. k := list.Lister{
  185. Collector: r.Collector,
  186. Reference: root.Object.Reference(),
  187. Prefix: root.Path,
  188. }
  189. in, err := k.List(ctx)
  190. if err != nil {
  191. return nil, err
  192. }
  193. for _, e := range in {
  194. nres, err := r.Find(ctx, s, e, parts)
  195. if err != nil {
  196. return nil, err
  197. }
  198. out = append(out, nres...)
  199. }
  200. return out, nil
  201. }