forked from ewilde/go-runscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
188 lines (154 loc) · 4.96 KB
/
bucket.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
/*
Package runscope implements a client library for the runscope api (https://api.blazemeter.com/api-monitoring/)
*/
package runscope
import (
"encoding/json"
"fmt"
"io"
"net/url"
)
const (
// DefaultPageSize is the max number of items fetched in each request
DefaultPageSize = 10
)
// Bucket resources are a simple way to organize your requests and tests.
// See https://api.blazemeter.com/api-monitoring/#buckets and https://www.runscope.com/docs/buckets
type Bucket struct {
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
Default bool `json:"default,omitempty"`
AuthToken string `json:"auth_token,omitempty"`
TestsURL string `json:"tests_url,omitempty" mapstructure:"tests_url"`
CollectionsURL string `json:"collections_url,omitempty"`
MessagesURL string `json:"messages_url,omitempty"`
TriggerURL string `json:"trigger_url,omitempty"`
VerifySsl bool `json:"verify_ssl,omitempty"`
Team *Team `json:"team,omitempty"`
}
// CreateBucket creates a new bucket resource. See https://api.blazemeter.com/api-monitoring/#creating-a-bucket
func (client *Client) CreateBucket(bucket *Bucket) (*Bucket, error) {
DebugF(1, "creating bucket %s", bucket.Name)
endpoint := fmt.Sprintf("/buckets?name=%s&team_uuid=%s",
url.QueryEscape(bucket.Name), url.QueryEscape(bucket.Team.ID))
DebugF(2, " request: POST %s", endpoint)
req, err := client.newRequest("POST", endpoint, nil)
if err != nil {
return nil, err
}
DebugF(2, "%#v", req)
resp, err := client.HTTP.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
bodyString := string(bodyBytes)
DebugF(2, " response: %d %s", resp.StatusCode, bodyString)
if resp.StatusCode >= 300 {
errorResp := new(errorResponse)
if err = json.Unmarshal(bodyBytes, &errorResp); err != nil {
return nil, fmt.Errorf("Error creating bucket: %s", bucket.Name)
}
return nil, fmt.Errorf("Error creating bucket: %s, status: %d reason: %q", bucket.Name,
errorResp.Status, errorResp.ErrorMessage)
}
response := new(response)
if err := json.Unmarshal(bodyBytes, &response); err != nil {
return nil, err
}
return getBucketFromResponse(response.Data)
}
// ReadBucket list details about an existing bucket resource. See https://www.runscope.com/docs/api/buckets#bucket-list
func (client *Client) ReadBucket(key string) (*Bucket, error) {
resource, err := client.readResource("bucket", key, fmt.Sprintf("/buckets/%s", key))
if err != nil {
return nil, err
}
bucket, err := getBucketFromResponse(resource.Data)
return bucket, err
}
// DeleteBucket deletes a bucket by key. See https://www.runscope.com/docs/api/buckets#bucket-delete
func (client *Client) DeleteBucket(key string) error {
return client.deleteResource("bucket", key, fmt.Sprintf("/buckets/%s", key))
}
// DeleteBuckets deletes all buckets matching the predicate
func (client *Client) DeleteBuckets(predicate func(bucket *Bucket) bool) error {
buckets, err := client.ListBuckets()
if err != nil {
return err
}
for _, bucket := range buckets {
if predicate(bucket) {
client.DeleteBucket(bucket.Key) // nolint: errcheck
}
}
return nil
}
// ListBuckets lists all buckets for an account
func (client *Client) ListBuckets() ([]*Bucket, error) {
resource, err := client.readResource("[]bucket", "", "/buckets")
if err != nil {
return nil, err
}
buckets, err := getBucketsFromResponse(resource.Data)
return buckets, err
}
// ListTestsInput represents the input to ListTests func
type ListTestsInput struct {
BucketKey string
Count int
Offset int
}
// ListTests lists some tests given ListTestsInput
func (client *Client) ListTests(input *ListTestsInput) ([]*Test, error) {
count := input.Count
if count == 0 {
count = DefaultPageSize
}
resource, err := client.readResource("[]test", "",
fmt.Sprintf("/buckets/%s/tests?count=%d&offset=%d", input.BucketKey, count, input.Offset))
if err != nil {
return nil, err
}
tests, err := getTestsFromResponse(resource.Data)
return tests, err
}
// ListAllTests lists all tests for a bucket
func (client *Client) ListAllTests(input *ListTestsInput) ([]*Test, error) {
var allTests []*Test
cfg := &ListTestsInput{
BucketKey: input.BucketKey,
Count: input.Count,
}
if cfg.Count == 0 {
cfg.Count = DefaultPageSize
}
for cfg.Offset = 0; ; cfg.Offset += cfg.Count {
tests, err := client.ListTests(cfg)
if err != nil {
return allTests, err
}
allTests = append(allTests, tests...)
if len(tests) < cfg.Count {
return allTests, nil
}
}
}
func (bucket *Bucket) String() string {
value, err := json.Marshal(bucket)
if err != nil {
return ""
}
return string(value)
}
func getBucketsFromResponse(response interface{}) ([]*Bucket, error) {
var buckets []*Bucket
err := decode(&buckets, response)
return buckets, err
}
func getBucketFromResponse(response interface{}) (*Bucket, error) {
bucket := new(Bucket)
err := decode(bucket, response)
return bucket, err
}