compute_resource.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright (c) 2015-2023 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 object
  14. import (
  15. "context"
  16. "fmt"
  17. "path"
  18. "github.com/vmware/govmomi/property"
  19. "github.com/vmware/govmomi/vim25"
  20. "github.com/vmware/govmomi/vim25/methods"
  21. "github.com/vmware/govmomi/vim25/mo"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. type ComputeResource struct {
  25. Common
  26. }
  27. func NewComputeResource(c *vim25.Client, ref types.ManagedObjectReference) *ComputeResource {
  28. return &ComputeResource{
  29. Common: NewCommon(c, ref),
  30. }
  31. }
  32. func (c ComputeResource) Hosts(ctx context.Context) ([]*HostSystem, error) {
  33. var cr mo.ComputeResource
  34. err := c.Properties(ctx, c.Reference(), []string{"host"}, &cr)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if len(cr.Host) == 0 {
  39. return nil, nil
  40. }
  41. var hs []mo.HostSystem
  42. pc := property.DefaultCollector(c.Client())
  43. err = pc.Retrieve(ctx, cr.Host, []string{"name"}, &hs)
  44. if err != nil {
  45. return nil, err
  46. }
  47. var hosts []*HostSystem
  48. for _, h := range hs {
  49. host := NewHostSystem(c.Client(), h.Reference())
  50. host.InventoryPath = path.Join(c.InventoryPath, h.Name)
  51. hosts = append(hosts, host)
  52. }
  53. return hosts, nil
  54. }
  55. func (c ComputeResource) Datastores(ctx context.Context) ([]*Datastore, error) {
  56. var cr mo.ComputeResource
  57. err := c.Properties(ctx, c.Reference(), []string{"datastore"}, &cr)
  58. if err != nil {
  59. return nil, err
  60. }
  61. var dss []*Datastore
  62. for _, ref := range cr.Datastore {
  63. ds := NewDatastore(c.c, ref)
  64. dss = append(dss, ds)
  65. }
  66. return dss, nil
  67. }
  68. func (c ComputeResource) EnvironmentBrowser(ctx context.Context) (*EnvironmentBrowser, error) {
  69. var cr mo.ComputeResource
  70. err := c.Properties(ctx, c.Reference(), []string{"environmentBrowser"}, &cr)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if cr.EnvironmentBrowser == nil {
  75. return nil, fmt.Errorf("%s: nil environmentBrowser", c.Reference())
  76. }
  77. return NewEnvironmentBrowser(c.c, *cr.EnvironmentBrowser), nil
  78. }
  79. func (c ComputeResource) ResourcePool(ctx context.Context) (*ResourcePool, error) {
  80. var cr mo.ComputeResource
  81. err := c.Properties(ctx, c.Reference(), []string{"resourcePool"}, &cr)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return NewResourcePool(c.c, *cr.ResourcePool), nil
  86. }
  87. func (c ComputeResource) Reconfigure(ctx context.Context, spec types.BaseComputeResourceConfigSpec, modify bool) (*Task, error) {
  88. req := types.ReconfigureComputeResource_Task{
  89. This: c.Reference(),
  90. Spec: spec,
  91. Modify: modify,
  92. }
  93. res, err := methods.ReconfigureComputeResource_Task(ctx, c.c, &req)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return NewTask(c.c, res.Returnval), nil
  98. }