task.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/property"
  17. "github.com/vmware/govmomi/task"
  18. "github.com/vmware/govmomi/vim25"
  19. "github.com/vmware/govmomi/vim25/methods"
  20. "github.com/vmware/govmomi/vim25/progress"
  21. "github.com/vmware/govmomi/vim25/types"
  22. )
  23. // Task is a convenience wrapper around task.Task that keeps a reference to
  24. // the client that was used to create it. This allows users to call the Wait()
  25. // function with only a context parameter, instead of a context parameter, a
  26. // soap.RoundTripper, and reference to the root property collector.
  27. type Task struct {
  28. Common
  29. }
  30. func NewTask(c *vim25.Client, ref types.ManagedObjectReference) *Task {
  31. t := Task{
  32. Common: NewCommon(c, ref),
  33. }
  34. return &t
  35. }
  36. func (t *Task) Wait(ctx context.Context) error {
  37. _, err := t.WaitForResult(ctx, nil)
  38. return err
  39. }
  40. func (t *Task) WaitForResult(ctx context.Context, s progress.Sinker) (*types.TaskInfo, error) {
  41. p := property.DefaultCollector(t.c)
  42. return task.Wait(ctx, t.Reference(), p, s)
  43. }
  44. func (t *Task) Cancel(ctx context.Context) error {
  45. _, err := methods.CancelTask(ctx, t.Client(), &types.CancelTask{
  46. This: t.Reference(),
  47. })
  48. return err
  49. }