-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathproxy.go
More file actions
166 lines (141 loc) · 4.15 KB
/
proxy.go
File metadata and controls
166 lines (141 loc) · 4.15 KB
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
// Package Toxiproxy provides a client wrapper around the Toxiproxy HTTP API for
// testing the resiliency of Go applications.
//
// For use with Toxiproxy 2.x
package toxiproxy
import (
"bytes"
"encoding/json"
"fmt"
)
type Proxy struct {
Name string `json:"name"` // The name of the proxy
Listen string `json:"listen"` // The address the proxy listens on
Upstream string `json:"upstream"` // The upstream address to proxy to
Enabled bool `json:"enabled"` // Whether the proxy is enabled
// The toxics active on this proxy. Note: you cannot set this
// when passing Proxy into Populate()
ActiveToxics Toxics `json:"toxics"`
client *Client
created bool // True if this proxy exists on the server
}
// Save saves changes to a proxy such as its enabled status or upstream port.
func (proxy *Proxy) Save() error {
request, err := json.Marshal(proxy)
if err != nil {
return err
}
data := bytes.NewReader(request)
var resp []byte
if proxy.created {
// TODO: Release PATCH only for v3.0
// resp, err = proxy.client.patch("/proxies/"+proxy.Name, data)
resp, err = proxy.client.post("/proxies/"+proxy.Name, data)
} else {
resp, err = proxy.client.post("/proxies", data)
}
if err != nil {
return err
}
err = json.Unmarshal(resp, proxy)
if err != nil {
return err
}
proxy.created = true
return nil
}
// Enable a proxy again after it has been disabled.
func (proxy *Proxy) Enable() error {
proxy.Enabled = true
return proxy.Save()
}
// Disable a proxy so that no connections can pass through. This will drop all active connections.
func (proxy *Proxy) Disable() error {
proxy.Enabled = false
return proxy.Save()
}
// Delete a proxy complete and close all existing connections through it. All information about
// the proxy such as listen port and active toxics will be deleted as well. If you just wish to
// stop and later enable a proxy, use `Enable()` and `Disable()`.
func (proxy *Proxy) Delete() error {
err := proxy.client.delete("/proxies/" + proxy.Name)
if err != nil {
return fmt.Errorf("Delete: %w", err)
}
return nil
}
// Toxics returns a map of all the active toxics and their attributes.
func (proxy *Proxy) Toxics() (Toxics, error) {
resp, err := proxy.client.get("/proxies/" + proxy.Name + "/toxics")
if err != nil {
return nil, err
}
toxics := make(Toxics, 0)
err = json.Unmarshal(resp, &toxics)
if err != nil {
return nil, err
}
return toxics, nil
}
// AddToxic adds a toxic to the given stream direction.
// If a name is not specified, it will default to <type>_<stream>.
// If a stream is not specified, it will default to downstream.
// See https://github.com/Shopify/toxiproxy#toxics for a list of all Toxic types.
func (proxy *Proxy) AddToxic(
name, typeName, stream string,
toxicity float32,
attrs Attributes,
) (*Toxic, error) {
toxic := Toxic{name, typeName, stream, toxicity, attrs}
if toxic.Toxicity == -1 {
toxic.Toxicity = 1 // Just to be consistent with a toxicity of -1 using the default
}
request, err := json.Marshal(&toxic)
if err != nil {
return nil, err
}
resp, err := proxy.client.post(
"/proxies/"+proxy.Name+"/toxics",
bytes.NewReader(request),
)
if err != nil {
return nil, fmt.Errorf("AddToxic: %w", err)
}
result := &Toxic{}
err = json.Unmarshal(resp, result)
if err != nil {
return nil, err
}
return result, nil
}
// UpdateToxic sets the parameters for an existing toxic with the given name.
// If toxicity is set to -1, the current value will be used.
func (proxy *Proxy) UpdateToxic(name string, toxicity float32, attrs Attributes) (*Toxic, error) {
toxic := map[string]interface{}{
"attributes": attrs,
}
if toxicity != -1 {
toxic["toxicity"] = toxicity
}
request, err := json.Marshal(&toxic)
if err != nil {
return nil, err
}
resp, err := proxy.client.patch(
"/proxies/"+proxy.Name+"/toxics/"+name,
bytes.NewReader(request),
)
if err != nil {
return nil, err
}
result := &Toxic{}
err = json.Unmarshal(resp, result)
if err != nil {
return nil, err
}
return result, nil
}
// RemoveToxic removes the toxic with the given name.
func (proxy *Proxy) RemoveToxic(name string) error {
return proxy.client.delete("/proxies/" + proxy.Name + "/toxics/" + name)
}