container_view.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright (c) 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 view
  14. import (
  15. "context"
  16. "github.com/vmware/govmomi/property"
  17. "github.com/vmware/govmomi/vim25"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "github.com/vmware/govmomi/vim25/types"
  20. )
  21. type ContainerView struct {
  22. ManagedObjectView
  23. }
  24. func NewContainerView(c *vim25.Client, ref types.ManagedObjectReference) *ContainerView {
  25. return &ContainerView{
  26. ManagedObjectView: *NewManagedObjectView(c, ref),
  27. }
  28. }
  29. // Retrieve populates dst as property.Collector.Retrieve does, for all entities in the view of types specified by kind.
  30. func (v ContainerView) Retrieve(ctx context.Context, kind []string, ps []string, dst interface{}, pspec ...types.PropertySpec) error {
  31. pc := property.DefaultCollector(v.Client())
  32. ospec := types.ObjectSpec{
  33. Obj: v.Reference(),
  34. Skip: types.NewBool(true),
  35. SelectSet: []types.BaseSelectionSpec{
  36. &types.TraversalSpec{
  37. Type: v.Reference().Type,
  38. Path: "view",
  39. },
  40. },
  41. }
  42. if len(kind) == 0 {
  43. kind = []string{"ManagedEntity"}
  44. }
  45. for _, t := range kind {
  46. spec := types.PropertySpec{
  47. Type: t,
  48. }
  49. if len(ps) == 0 {
  50. spec.All = types.NewBool(true)
  51. } else {
  52. spec.PathSet = ps
  53. }
  54. pspec = append(pspec, spec)
  55. }
  56. req := types.RetrieveProperties{
  57. SpecSet: []types.PropertyFilterSpec{
  58. {
  59. ObjectSet: []types.ObjectSpec{ospec},
  60. PropSet: pspec,
  61. },
  62. },
  63. }
  64. res, err := pc.RetrieveProperties(ctx, req)
  65. if err != nil {
  66. return err
  67. }
  68. if d, ok := dst.(*[]types.ObjectContent); ok {
  69. *d = res.Returnval
  70. return nil
  71. }
  72. return mo.LoadObjectContent(res.Returnval, dst)
  73. }
  74. // RetrieveWithFilter populates dst as Retrieve does, but only for entities matching the given filter.
  75. func (v ContainerView) RetrieveWithFilter(ctx context.Context, kind []string, ps []string, dst interface{}, filter property.Match) error {
  76. if len(filter) == 0 {
  77. return v.Retrieve(ctx, kind, ps, dst)
  78. }
  79. var content []types.ObjectContent
  80. err := v.Retrieve(ctx, kind, filter.Keys(), &content)
  81. if err != nil {
  82. return err
  83. }
  84. objs := filter.ObjectContent(content)
  85. pc := property.DefaultCollector(v.Client())
  86. return pc.Retrieve(ctx, objs, ps, dst)
  87. }
  88. // Find returns object references for entities of type kind, matching the given filter.
  89. func (v ContainerView) Find(ctx context.Context, kind []string, filter property.Match) ([]types.ManagedObjectReference, error) {
  90. if len(filter) == 0 {
  91. // Ensure we have at least 1 filter to avoid retrieving all properties.
  92. filter = property.Match{"name": "*"}
  93. }
  94. var content []types.ObjectContent
  95. err := v.Retrieve(ctx, kind, filter.Keys(), &content)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return filter.ObjectContent(content), nil
  100. }
  101. // FindAny returns object references for entities of type kind, matching any property the given filter.
  102. func (v ContainerView) FindAny(ctx context.Context, kind []string, filter property.Match) ([]types.ManagedObjectReference, error) {
  103. if len(filter) == 0 {
  104. // Ensure we have at least 1 filter to avoid retrieving all properties.
  105. filter = property.Match{"name": "*"}
  106. }
  107. var content []types.ObjectContent
  108. err := v.Retrieve(ctx, kind, filter.Keys(), &content)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return filter.AnyObjectContent(content), nil
  113. }