resource_pool.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "github.com/vmware/govmomi/nfc"
  17. "github.com/vmware/govmomi/vim25"
  18. "github.com/vmware/govmomi/vim25/methods"
  19. "github.com/vmware/govmomi/vim25/mo"
  20. "github.com/vmware/govmomi/vim25/types"
  21. )
  22. type ResourcePool struct {
  23. Common
  24. }
  25. func NewResourcePool(c *vim25.Client, ref types.ManagedObjectReference) *ResourcePool {
  26. return &ResourcePool{
  27. Common: NewCommon(c, ref),
  28. }
  29. }
  30. // Owner returns the ResourcePool owner as a ClusterComputeResource or ComputeResource.
  31. func (p ResourcePool) Owner(ctx context.Context) (Reference, error) {
  32. var pool mo.ResourcePool
  33. err := p.Properties(ctx, p.Reference(), []string{"owner"}, &pool)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return NewReference(p.Client(), pool.Owner), nil
  38. }
  39. func (p ResourcePool) ImportVApp(ctx context.Context, spec types.BaseImportSpec, folder *Folder, host *HostSystem) (*nfc.Lease, error) {
  40. req := types.ImportVApp{
  41. This: p.Reference(),
  42. Spec: spec,
  43. }
  44. if folder != nil {
  45. ref := folder.Reference()
  46. req.Folder = &ref
  47. }
  48. if host != nil {
  49. ref := host.Reference()
  50. req.Host = &ref
  51. }
  52. res, err := methods.ImportVApp(ctx, p.c, &req)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return nfc.NewLease(p.c, res.Returnval), nil
  57. }
  58. func (p ResourcePool) Create(ctx context.Context, name string, spec types.ResourceConfigSpec) (*ResourcePool, error) {
  59. req := types.CreateResourcePool{
  60. This: p.Reference(),
  61. Name: name,
  62. Spec: spec,
  63. }
  64. res, err := methods.CreateResourcePool(ctx, p.c, &req)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return NewResourcePool(p.c, res.Returnval), nil
  69. }
  70. func (p ResourcePool) CreateVApp(ctx context.Context, name string, resSpec types.ResourceConfigSpec, configSpec types.VAppConfigSpec, folder *Folder) (*VirtualApp, error) {
  71. req := types.CreateVApp{
  72. This: p.Reference(),
  73. Name: name,
  74. ResSpec: resSpec,
  75. ConfigSpec: configSpec,
  76. }
  77. if folder != nil {
  78. ref := folder.Reference()
  79. req.VmFolder = &ref
  80. }
  81. res, err := methods.CreateVApp(ctx, p.c, &req)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return NewVirtualApp(p.c, res.Returnval), nil
  86. }
  87. func (p ResourcePool) UpdateConfig(ctx context.Context, name string, config *types.ResourceConfigSpec) error {
  88. req := types.UpdateConfig{
  89. This: p.Reference(),
  90. Name: name,
  91. Config: config,
  92. }
  93. if config != nil && config.Entity == nil {
  94. ref := p.Reference()
  95. // Create copy of config so changes won't leak back to the caller
  96. newConfig := *config
  97. newConfig.Entity = &ref
  98. req.Config = &newConfig
  99. }
  100. _, err := methods.UpdateConfig(ctx, p.c, &req)
  101. return err
  102. }
  103. func (p ResourcePool) DestroyChildren(ctx context.Context) error {
  104. req := types.DestroyChildren{
  105. This: p.Reference(),
  106. }
  107. _, err := methods.DestroyChildren(ctx, p.c, &req)
  108. return err
  109. }
  110. func (p ResourcePool) Destroy(ctx context.Context) (*Task, error) {
  111. req := types.Destroy_Task{
  112. This: p.Reference(),
  113. }
  114. res, err := methods.Destroy_Task(ctx, p.c, &req)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return NewTask(p.c, res.Returnval), nil
  119. }