Debian Codegraph @

status/status.go

raw symbols SCIP
1 package status
2
3 import (
4 "fmt"
5 "net/url"
6 "time"
7
8 "github.com/CenturyLinkCloud/clc-sdk/api"
9 )
10
11 func New(client api.HTTP) *Service {
12 return &Service{
13 client: client,
14 config: client.Config(),
15 PollInterval: 30 * time.Second,
16 }
17 }
18
19 type Service struct {
20 client api.HTTP
21 config *api.Config
22
23 PollInterval time.Duration
24 }
25
26 func (s *Service) Get(id string) (*Response, error) {
27 url := fmt.Sprintf("%s/operations/%s/status/%s", s.config.BaseURL, s.config.Alias, id)
28 status := &Response{}
29 err := s.client.Get(url, status)
30 return status, err
31 }
32
33 func (s *Service) GetBlueprint(id string) (*BlueprintOperation, error) {
34 url := fmt.Sprintf("%s/operations/%s/status/%s", s.config.BaseURL, s.config.Alias, id)
35 status := &BlueprintOperation{}
36 err := s.client.Get(url, status)
37 return status, err
38 }
39
40 func (s *Service) Poll(id string, poll chan *Response) error {
41 for {
42 status, err := s.Get(id)
43 if err != nil {
44 return err
45 }
46
47 if !status.Running() {
48 poll <- status
49 return nil
50 }
51 time.Sleep(s.PollInterval)
52 }
53 }
54
55 type Status struct {
56 ID string `json:"id"`
57 Rel string `json:"rel"`
58 Href string `json:"href"`
59 }
60
61 /*
62 Response represents a running async job
63 result from polling status
64 {"status": "succeeded"}
65 */
66 type Response struct {
67 Status string `json:"status"`
68 }
69
70 func (s *Response) Complete() bool {
71 return s.Status == Complete
72 }
73
74 func (s *Response) Failed() bool {
75 return s.Status == Failed
76 }
77
78 func (s *Response) Running() bool {
79 return !s.Complete() && !s.Failed() && s.Status != ""
80 }
81
82 const (
83 Complete = "succeeded"
84 Failed = "failed"
85 )
86
87 /* QueuedResponse represents a returned response for an async platform job
88 eg. create server
89 {"server":"web", "isQueued":true, "links":[
90 {"rel":"status", "href":"...", "id":"wa1-12345"},
91 {"rel":"self", "href":"...", "id":"8134c91a66784c6dada651eba90a5123"}]}
92 */
93 type QueuedResponse struct {
94 Server string `json:"server,omitempty"`
95 IsQueued bool `json:"isQueued,omitempty"`
96 Links api.Links `json:"links,omitempty"`
97 Error string `json:"errorMessage,omitempty"`
98 }
99
100 func (q *QueuedResponse) GetStatusID() (bool, string) {
101 return q.Links.GetID("status")
102 }
103
104 /* QueuedOperation may be a one-off and/or experimental version of QueuedResponse
105 eg. add secondary network
106 {"operationId": "2b70710dba4142dcaf3ab2de68e4f40c", "uri": "..."}
107 */
108 type QueuedOperation struct {
109 OperationID string `json:"operationId,omitempty"`
110 URI string `json:"uri,omitempty"`
111 }
112
113 func (q *QueuedOperation) GetStatusID() (bool, string) {
114 return q.OperationID != "", q.OperationID
115 }
116
117 func (q *QueuedOperation) GetHref() (bool, string) {
118 var path = ""
119 if q.URI != "" {
120 u, err := url.Parse(q.URI)
121 if err == nil {
122 path = u.Path
123 }
124 }
125 return path != "", path
126 }
127
128 func (q *QueuedOperation) Status() *Status {
129 st := &Status{}
130 if ok, id := q.GetStatusID(); ok {
131 st.ID = id
132 }
133 if ok, href := q.GetHref(); ok {
134 st.Href = href
135 }
136 return st
137 }
138
139 /* BlueprintOperation is a status object representing a running blueprint job
140 {
141 "requestType":"blueprintOperation",
142 "status":"succeeded",
143 "summary":{
144 "blueprintId":51229,
145 "locationId":"CA1",
146 "links":[
147 {
148 "rel":"network",
149 "href":"/v2-experimental/networks/ZZBB/CA1/6955e7c39b5648df91bfb32e5d0aa24b",
150 "id":"6955e7c39b5648df91bfb32e5d0aa24b"
151 }
152 ]
153 },
154 "source":{"userName":"ack","requestedAt":"2016-03-24T16:47:04Z"}
155 }
156 */
157 type BlueprintOperation struct {
158 RequestType string `json:"requestType,omitempty"`
159 Status string `json:"status,omitempty"`
160 Summary struct {
161 BlueprintID int `json:"blueprintId,omitempty"`
162 LocationID string `json:"locationId,omitempty"`
163 Links api.Links `json:"links,omitempty"`
164 } `json:"summary,omitempty"`
165 Source struct {
166 UserName string `json:"userName"`
167 RequestedAt time.Time `json:"requestedAt,omitempty"`
168 } `json:"source,omitempty"`
169 }