ancestors.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright (c) 2015 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 mo
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/vmware/govmomi/vim25/soap"
  18. "github.com/vmware/govmomi/vim25/types"
  19. )
  20. // Ancestors returns the entire ancestry tree of a specified managed object.
  21. // The return value includes the root node and the specified object itself.
  22. func Ancestors(ctx context.Context, rt soap.RoundTripper, pc, obj types.ManagedObjectReference) ([]ManagedEntity, error) {
  23. ospec := types.ObjectSpec{
  24. Obj: obj,
  25. SelectSet: []types.BaseSelectionSpec{
  26. &types.TraversalSpec{
  27. SelectionSpec: types.SelectionSpec{Name: "traverseParent"},
  28. Type: "ManagedEntity",
  29. Path: "parent",
  30. Skip: types.NewBool(false),
  31. SelectSet: []types.BaseSelectionSpec{
  32. &types.SelectionSpec{Name: "traverseParent"},
  33. },
  34. },
  35. &types.TraversalSpec{
  36. SelectionSpec: types.SelectionSpec{},
  37. Type: "VirtualMachine",
  38. Path: "parentVApp",
  39. Skip: types.NewBool(false),
  40. SelectSet: []types.BaseSelectionSpec{
  41. &types.SelectionSpec{Name: "traverseParent"},
  42. },
  43. },
  44. },
  45. Skip: types.NewBool(false),
  46. }
  47. pspec := []types.PropertySpec{
  48. {
  49. Type: "ManagedEntity",
  50. PathSet: []string{"name", "parent"},
  51. },
  52. {
  53. Type: "VirtualMachine",
  54. PathSet: []string{"parentVApp"},
  55. },
  56. }
  57. req := types.RetrieveProperties{
  58. This: pc,
  59. SpecSet: []types.PropertyFilterSpec{
  60. {
  61. ObjectSet: []types.ObjectSpec{ospec},
  62. PropSet: pspec,
  63. },
  64. },
  65. }
  66. var ifaces []interface{}
  67. err := RetrievePropertiesForRequest(ctx, rt, req, &ifaces)
  68. if err != nil {
  69. return nil, err
  70. }
  71. var out []ManagedEntity
  72. // Build ancestry tree by iteratively finding a new child.
  73. for len(out) < len(ifaces) {
  74. var find types.ManagedObjectReference
  75. if len(out) > 0 {
  76. find = out[len(out)-1].Self
  77. }
  78. // Find entity we're looking for given the last entity in the current tree.
  79. for _, iface := range ifaces {
  80. me := iface.(IsManagedEntity).GetManagedEntity()
  81. if me.Name == "" {
  82. // The types below have their own 'Name' field, so ManagedEntity.Name (me.Name) is empty.
  83. // We only hit this case when the 'obj' param is one of these types.
  84. // In most cases, 'obj' is a Folder so Name isn't collected in this call.
  85. switch x := iface.(type) {
  86. case Network:
  87. me.Name = x.Name
  88. case DistributedVirtualSwitch:
  89. me.Name = x.Name
  90. case DistributedVirtualPortgroup:
  91. me.Name = x.Name
  92. case OpaqueNetwork:
  93. me.Name = x.Name
  94. default:
  95. // ManagedEntity always has a Name, if we hit this point we missed a case above.
  96. panic(fmt.Sprintf("%#v Name is empty", me.Reference()))
  97. }
  98. }
  99. if me.Parent == nil {
  100. // Special case for VirtualMachine within VirtualApp,
  101. // unlikely to hit this other than via Finder.Element()
  102. switch x := iface.(type) {
  103. case VirtualMachine:
  104. me.Parent = x.ParentVApp
  105. }
  106. }
  107. if me.Parent == nil {
  108. out = append(out, me)
  109. break
  110. }
  111. if *me.Parent == find {
  112. out = append(out, me)
  113. break
  114. }
  115. }
  116. }
  117. return out, nil
  118. }