resource_pool.go 3.2 KB

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