virtual_app.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/vim25"
  17. "github.com/vmware/govmomi/vim25/methods"
  18. "github.com/vmware/govmomi/vim25/types"
  19. )
  20. type VirtualApp struct {
  21. *ResourcePool
  22. }
  23. func NewVirtualApp(c *vim25.Client, ref types.ManagedObjectReference) *VirtualApp {
  24. return &VirtualApp{
  25. ResourcePool: NewResourcePool(c, ref),
  26. }
  27. }
  28. func (p VirtualApp) CreateChildVM(ctx context.Context, config types.VirtualMachineConfigSpec, host *HostSystem) (*Task, error) {
  29. req := types.CreateChildVM_Task{
  30. This: p.Reference(),
  31. Config: config,
  32. }
  33. if host != nil {
  34. ref := host.Reference()
  35. req.Host = &ref
  36. }
  37. res, err := methods.CreateChildVM_Task(ctx, p.c, &req)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return NewTask(p.c, res.Returnval), nil
  42. }
  43. func (p VirtualApp) UpdateConfig(ctx context.Context, spec types.VAppConfigSpec) error {
  44. req := types.UpdateVAppConfig{
  45. This: p.Reference(),
  46. Spec: spec,
  47. }
  48. _, err := methods.UpdateVAppConfig(ctx, p.c, &req)
  49. return err
  50. }
  51. func (p VirtualApp) PowerOn(ctx context.Context) (*Task, error) {
  52. req := types.PowerOnVApp_Task{
  53. This: p.Reference(),
  54. }
  55. res, err := methods.PowerOnVApp_Task(ctx, p.c, &req)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return NewTask(p.c, res.Returnval), nil
  60. }
  61. func (p VirtualApp) PowerOff(ctx context.Context, force bool) (*Task, error) {
  62. req := types.PowerOffVApp_Task{
  63. This: p.Reference(),
  64. Force: force,
  65. }
  66. res, err := methods.PowerOffVApp_Task(ctx, p.c, &req)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return NewTask(p.c, res.Returnval), nil
  71. }
  72. func (p VirtualApp) Suspend(ctx context.Context) (*Task, error) {
  73. req := types.SuspendVApp_Task{
  74. This: p.Reference(),
  75. }
  76. res, err := methods.SuspendVApp_Task(ctx, p.c, &req)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return NewTask(p.c, res.Returnval), nil
  81. }