compute_resource.go 2.6 KB

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