Debian Codegraph @

task.go

raw symbols SCIP
1 /*
2 * Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
3 */
4
5 package govcd
6
7 import (
8 "fmt"
9 "net/url"
10 "time"
11
12 types "github.com/hmrc/vmware-govcd/types/v56"
13 )
14
15 type Task struct {
16 Task *types.Task
17 c *Client
18 }
19
20 func NewTask(c *Client) *Task {
21 return &Task{
22 Task: new(types.Task),
23 c: c,
24 }
25 }
26
27 func (t *Task) Refresh() error {
28
29 if t.Task == nil {
30 return fmt.Errorf("cannot refresh, Object is empty")
31 }
32
33 u, _ := url.ParseRequestURI(t.Task.HREF)
34
35 req := t.c.NewRequest(map[string]string{}, "GET", *u, nil)
36
37 resp, err := checkResp(t.c.Http.Do(req))
38 if err != nil {
39 return fmt.Errorf("error retrieving task: %s", err)
40 }
41
42 // Empty struct before a new unmarshal, otherwise we end up with duplicate
43 // elements in slices.
44 t.Task = &types.Task{}
45
46 if err = decodeBody(resp, t.Task); err != nil {
47 return fmt.Errorf("error decoding task response: %s", err)
48 }
49
50 // The request was successful
51 return nil
52 }
53
54 func (t *Task) WaitTaskCompletion() error {
55
56 if t.Task == nil {
57 return fmt.Errorf("cannot refresh, Object is empty")
58 }
59
60 for {
61 err := t.Refresh()
62 if err != nil {
63 return fmt.Errorf("error retreiving task: %s", err)
64 }
65
66 // If task is not in a waiting status we're done, check if there's an error and return it.
67 if t.Task.Status != "queued" && t.Task.Status != "preRunning" && t.Task.Status != "running" {
68 if t.Task.Status == "error" {
69 return fmt.Errorf("task did not complete succesfully: %s", t.Task.Description)
70 }
71 return nil
72 }
73
74 // Sleep for 3 seconds and try again.
75 time.Sleep(3 * time.Second)
76 }
77 }