-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrunstatus_service.go
202 lines (165 loc) · 4.65 KB
/
runstatus_service.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package egoscale
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"path"
"strconv"
)
// RunstatusService is a runstatus service
type RunstatusService struct {
ID int `json:"id"` // missing field
Name string `json:"name"`
PageURL string `json:"page_url,omitempty"` // fake field
State string `json:"state,omitempty"`
URL string `json:"url,omitempty"`
}
// FakeID fills up the ID field as it's currently missing
func (service *RunstatusService) FakeID() error {
if service.ID > 0 {
return nil
}
if service.URL == "" {
return fmt.Errorf("empty URL for %#v", service)
}
u, err := url.Parse(service.URL)
if err != nil {
return err
}
s := path.Base(u.Path)
id, err := strconv.Atoi(s)
if err != nil {
return err
}
service.ID = id
return nil
}
// Match returns true if the other service has got similarities with itself
func (service RunstatusService) Match(other RunstatusService) bool {
if other.Name != "" && service.Name == other.Name {
return true
}
if other.ID > 0 && service.ID == other.ID {
return true
}
return false
}
// RunstatusServiceList service list
type RunstatusServiceList struct {
Next string `json:"next"`
Previous string `json:"previous"`
Services []RunstatusService `json:"results"`
}
// DeleteRunstatusService delete runstatus service
func (client *Client) DeleteRunstatusService(ctx context.Context, service RunstatusService) error {
if service.URL == "" {
return fmt.Errorf("empty URL for %#v", service)
}
_, err := client.runstatusRequest(ctx, service.URL, nil, "DELETE")
return err
}
// CreateRunstatusService create runstatus service
func (client *Client) CreateRunstatusService(ctx context.Context, service RunstatusService) (*RunstatusService, error) {
if service.PageURL == "" {
return nil, fmt.Errorf("empty Page URL for %#v", service)
}
page, err := client.GetRunstatusPage(ctx, RunstatusPage{URL: service.PageURL})
if err != nil {
return nil, err
}
resp, err := client.runstatusRequest(ctx, page.ServicesURL, service, "POST")
if err != nil {
return nil, err
}
s := &RunstatusService{}
if err := json.Unmarshal(resp, s); err != nil {
return nil, err
}
return s, nil
}
// GetRunstatusService displays service detail.
func (client *Client) GetRunstatusService(ctx context.Context, service RunstatusService) (*RunstatusService, error) {
if service.URL != "" {
return client.getRunstatusService(ctx, service.URL)
}
if service.PageURL == "" {
return nil, fmt.Errorf("empty Page URL in %#v", service)
}
page, err := client.getRunstatusPage(ctx, service.PageURL)
if err != nil {
return nil, err
}
for i := range page.Services {
s := &page.Services[i]
if s.Match(service) {
if err := s.FakeID(); err != nil {
log.Printf("bad fake ID for %#v, %s", s, err)
}
return s, nil
}
}
return nil, errors.New("service not found")
}
func (client *Client) getRunstatusService(ctx context.Context, serviceURL string) (*RunstatusService, error) {
resp, err := client.runstatusRequest(ctx, serviceURL, nil, "GET")
if err != nil {
return nil, err
}
s := &RunstatusService{}
if err := json.Unmarshal(resp, &s); err != nil {
return nil, err
}
if err := s.FakeID(); err != nil {
log.Printf("bad fake ID for %#v, %s", s, err)
}
return s, nil
}
// ListRunstatusServices displays the list of services.
func (client *Client) ListRunstatusServices(ctx context.Context, page RunstatusPage) ([]RunstatusService, error) {
if page.ServicesURL == "" {
return nil, fmt.Errorf("empty Services URL for %#v", page)
}
results := make([]RunstatusService, 0)
var err error
client.PaginateRunstatusServices(ctx, page, func(service *RunstatusService, e error) bool {
if e != nil {
err = e
return false
}
results = append(results, *service)
return true
})
return results, err
}
// PaginateRunstatusServices paginates Services
func (client *Client) PaginateRunstatusServices(ctx context.Context, page RunstatusPage, callback func(*RunstatusService, error) bool) { // nolint: dupl
if page.ServicesURL == "" {
callback(nil, fmt.Errorf("empty Services URL for %#v", page))
return
}
servicesURL := page.ServicesURL
for servicesURL != "" {
resp, err := client.runstatusRequest(ctx, servicesURL, nil, "GET")
if err != nil {
callback(nil, err)
return
}
var ss *RunstatusServiceList
if err := json.Unmarshal(resp, &ss); err != nil {
callback(nil, err)
return
}
for i := range ss.Services {
if err := ss.Services[i].FakeID(); err != nil {
log.Printf("bad fake ID for %#v, %s", ss.Services[i], err)
}
if cont := callback(&ss.Services[i], nil); !cont {
return
}
}
servicesURL = ss.Next
}
}