File: status.go

package info (click to toggle)
golang-github-centurylinkcloud-clc-sdk 0.0.2%2Bgit20161004.f62483c-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 228 kB
  • sloc: sh: 18; makefile: 13
file content (169 lines) | stat: -rw-r--r-- 3,929 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package status

import (
	"fmt"
	"net/url"
	"time"

	"github.com/CenturyLinkCloud/clc-sdk/api"
)

func New(client api.HTTP) *Service {
	return &Service{
		client:       client,
		config:       client.Config(),
		PollInterval: 30 * time.Second,
	}
}

type Service struct {
	client api.HTTP
	config *api.Config

	PollInterval time.Duration
}

func (s *Service) Get(id string) (*Response, error) {
	url := fmt.Sprintf("%s/operations/%s/status/%s", s.config.BaseURL, s.config.Alias, id)
	status := &Response{}
	err := s.client.Get(url, status)
	return status, err
}

func (s *Service) GetBlueprint(id string) (*BlueprintOperation, error) {
	url := fmt.Sprintf("%s/operations/%s/status/%s", s.config.BaseURL, s.config.Alias, id)
	status := &BlueprintOperation{}
	err := s.client.Get(url, status)
	return status, err
}

func (s *Service) Poll(id string, poll chan *Response) error {
	for {
		status, err := s.Get(id)
		if err != nil {
			return err
		}

		if !status.Running() {
			poll <- status
			return nil
		}
		time.Sleep(s.PollInterval)
	}
}

type Status struct {
	ID   string `json:"id"`
	Rel  string `json:"rel"`
	Href string `json:"href"`
}

/*
   Response represents a running async job
   result from polling status
   {"status": "succeeded"}
*/
type Response struct {
	Status string `json:"status"`
}

func (s *Response) Complete() bool {
	return s.Status == Complete
}

func (s *Response) Failed() bool {
	return s.Status == Failed
}

func (s *Response) Running() bool {
	return !s.Complete() && !s.Failed() && s.Status != ""
}

const (
	Complete = "succeeded"
	Failed   = "failed"
)

/* QueuedResponse represents a returned response for an async platform job
   eg. create server
   {"server":"web", "isQueued":true, "links":[
     {"rel":"status", "href":"...", "id":"wa1-12345"},
     {"rel":"self",  "href":"...", "id":"8134c91a66784c6dada651eba90a5123"}]}
*/
type QueuedResponse struct {
	Server   string    `json:"server,omitempty"`
	IsQueued bool      `json:"isQueued,omitempty"`
	Links    api.Links `json:"links,omitempty"`
	Error    string    `json:"errorMessage,omitempty"`
}

func (q *QueuedResponse) GetStatusID() (bool, string) {
	return q.Links.GetID("status")
}

/* QueuedOperation may be a one-off and/or experimental version of QueuedResponse
   eg. add secondary network
   {"operationId": "2b70710dba4142dcaf3ab2de68e4f40c", "uri": "..."}
*/
type QueuedOperation struct {
	OperationID string `json:"operationId,omitempty"`
	URI         string `json:"uri,omitempty"`
}

func (q *QueuedOperation) GetStatusID() (bool, string) {
	return q.OperationID != "", q.OperationID
}

func (q *QueuedOperation) GetHref() (bool, string) {
	var path = ""
	if q.URI != "" {
		u, err := url.Parse(q.URI)
		if err == nil {
			path = u.Path
		}
	}
	return path != "", path
}

func (q *QueuedOperation) Status() *Status {
	st := &Status{}
	if ok, id := q.GetStatusID(); ok {
		st.ID = id
	}
	if ok, href := q.GetHref(); ok {
		st.Href = href
	}
	return st
}

/* BlueprintOperation is a status object representing a running blueprint job
    {
      "requestType":"blueprintOperation",
      "status":"succeeded",
      "summary":{
	"blueprintId":51229,
	"locationId":"CA1",
	"links":[
	  {
	    "rel":"network",
	    "href":"/v2-experimental/networks/ZZBB/CA1/6955e7c39b5648df91bfb32e5d0aa24b",
	    "id":"6955e7c39b5648df91bfb32e5d0aa24b"
	  }
	]
      },
      "source":{"userName":"ack","requestedAt":"2016-03-24T16:47:04Z"}
    }
*/
type BlueprintOperation struct {
	RequestType string `json:"requestType,omitempty"`
	Status      string `json:"status,omitempty"`
	Summary     struct {
		BlueprintID int       `json:"blueprintId,omitempty"`
		LocationID  string    `json:"locationId,omitempty"`
		Links       api.Links `json:"links,omitempty"`
	} `json:"summary,omitempty"`
	Source struct {
		UserName    string    `json:"userName"`
		RequestedAt time.Time `json:"requestedAt,omitempty"`
	} `json:"source,omitempty"`
}