-
Notifications
You must be signed in to change notification settings - Fork 18
/
environment.go
182 lines (151 loc) · 7.45 KB
/
environment.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
package runscope
import (
"encoding/json"
"fmt"
"time"
)
// Environment stores details for shared and test-specific environments. See https://www.runscope.com/docs/api/environments
type Environment struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Script string `json:"script,omitempty"`
PreserveCookies bool `json:"preserve_cookies"`
TestID string `json:"test_id,omitempty"`
InitialVariables map[string]string `json:"initial_variables,omitempty"`
Integrations []*EnvironmentIntegration `json:"integrations,omitempty"`
Regions []string `json:"regions,omitempty"`
VerifySsl bool `json:"verify_ssl"`
ExportedAt *time.Time `json:"exported_at,omitempty"`
RetryOnFailure bool `json:"retry_on_failure"`
RemoteAgents []*LocalMachine `json:"remote_agents,omitempty"`
WebHooks []string `json:"webhooks,omitempty"`
ParentEnvironmentID string `json:"parent_environment_id,omitempty"`
EmailSettings *EmailSettings `json:"emails,omitempty"`
ClientCertificate string `json:"client_certificate,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
}
// EmailSettings determining how test failures trigger notifications
type EmailSettings struct {
NotifyAll bool `json:"notify_all,omitempty"`
NotifyOn string `json:"notify_on,omitempty"`
NotifyThreshold int `json:"notify_threshold,omitempty"`
Recipients []*Contact `json:"recipients,omitempty"`
}
// EnvironmentIntegration represents an integration with a third-party. See https://www.runscope.com/docs/api/integrations
type EnvironmentIntegration struct {
ID string `json:"id"`
IntegrationType string `json:"integration_type"`
Description string `json:"description,omitempty"`
}
// LocalMachine used in an environment to represent a remote agent
type LocalMachine struct {
Name string `json:"name"`
UUID string `json:"uuid"`
}
// NewEnvironment creates a new environment
func NewEnvironment() *Environment {
return new(Environment)
}
// CreateSharedEnvironment creates a new shared environment. See https://www.runscope.com/docs/api/environments#create-shared
func (client *Client) CreateSharedEnvironment(environment *Environment, bucket *Bucket) (*Environment, error) {
return client.createEnvironment(environment, fmt.Sprintf("/buckets/%s/environments", bucket.Key))
}
// CreateTestEnvironment creates a new test environment. See https://www.runscope.com/docs/api/environments#create
func (client *Client) CreateTestEnvironment(environment *Environment, test *Test) (*Environment, error) {
return client.createEnvironment(environment, fmt.Sprintf("/buckets/%s/tests/%s/environments",
test.Bucket.Key, test.ID))
}
// ListSharedEnvironment lists all shared environments for a given bucket. See https://www.runscope.com/docs/api/environments#list-shared
func (client *Client) ListSharedEnvironment(bucket *Bucket) ([]*Environment, error) {
return client.listEnvironments(bucket, fmt.Sprintf("/buckets/%s/environments", bucket.Key))
}
// ListTestEnvironment lists all tests environments in a given test. See https://api.blazemeter.com/api-monitoring/#test-envrionment-list
func (client *Client) ListTestEnvironment(bucket *Bucket, test *Test) ([]*Environment, error) {
return client.listEnvironments(bucket, fmt.Sprintf("/buckets/%s/tests/%s/environments", bucket.Key, test.ID))
}
// ReadSharedEnvironment lists details about an existing shared environment. See https://www.runscope.com/docs/api/environments#detail
func (client *Client) ReadSharedEnvironment(environment *Environment, bucket *Bucket) (*Environment, error) {
return client.readEnvironment(environment, fmt.Sprintf("/buckets/%s/environments/%s",
bucket.Key, environment.ID))
}
// ReadTestEnvironment lists details about an existing test environment. See https://www.runscope.com/docs/api/environments#detail
func (client *Client) ReadTestEnvironment(environment *Environment, test *Test) (*Environment, error) {
return client.readEnvironment(environment, fmt.Sprintf("/buckets/%s/tests/%s/environments/%s",
test.Bucket.Key, test.ID, environment.ID))
}
// UpdateSharedEnvironment updates details about an existing shared environment. See https://www.runscope.com/docs/api/environments#modify
func (client *Client) UpdateSharedEnvironment(environment *Environment, bucket *Bucket) (*Environment, error) {
return client.updateEnvironment(environment,
fmt.Sprintf("/buckets/%s/environments/%s", bucket.Key, environment.ID))
}
// UpdateTestEnvironment updates details about an existing test environment. See https://www.runscope.com/docs/api/environments#modify
func (client *Client) UpdateTestEnvironment(environment *Environment, test *Test) (*Environment, error) {
return client.updateEnvironment(environment,
fmt.Sprintf("/buckets/%s/tests/%s/environments/%s", test.Bucket.Key, test.ID, environment.ID))
}
// DeleteEnvironment deletes an existing shared environment. https://www.runscope.com/docs/api/environments#delete
func (client *Client) DeleteEnvironment(environment *Environment, bucket *Bucket) error {
return client.deleteResource("environment", environment.ID,
fmt.Sprintf("/buckets/%s/environments/%s", bucket.Key, environment.ID))
}
func (environment *Environment) String() string {
value, err := json.Marshal(environment)
if err != nil {
return ""
}
return string(value)
}
func (client *Client) createEnvironment(environment *Environment, endpoint string) (*Environment, error) {
newResource, error := client.createResource(environment, "environment", environment.Name, endpoint)
if error != nil {
return nil, error
}
newEnvironment, error := getEnvironmentFromResponse(newResource.Data)
if error != nil {
return nil, error
}
return newEnvironment, nil
}
func (client *Client) listEnvironments(bucket *Bucket, endpoint string) ([]*Environment, error) {
resource, error := client.readResource("environments", bucket.Key, endpoint)
if error != nil {
return nil, error
}
list, error := getEnvironmentsFromResponse(resource.Data)
if error != nil {
return nil, error
}
return list, nil
}
func (client *Client) readEnvironment(environment *Environment, endpoint string) (*Environment, error) {
resource, error := client.readResource("environment", environment.ID, endpoint)
if error != nil {
return nil, error
}
readEnvironment, error := getEnvironmentFromResponse(resource.Data)
if error != nil {
return nil, error
}
return readEnvironment, nil
}
func (client *Client) updateEnvironment(environment *Environment, endpoint string) (*Environment, error) {
resource, error := client.updateResource(environment, "environment", environment.ID, endpoint)
if error != nil {
return nil, error
}
updatedEnvironment, error := getEnvironmentFromResponse(resource.Data)
if error != nil {
return nil, error
}
return updatedEnvironment, nil
}
func getEnvironmentFromResponse(response interface{}) (*Environment, error) {
environment := new(Environment)
err := decode(environment, response)
return environment, err
}
func getEnvironmentsFromResponse(response interface{}) ([]*Environment, error) {
var environments []*Environment
err := decode(&environments, response)
return environments, err
}