Permalink
Browse files

Add toxic wrapper, update api endpoints

Implement add/update/remove in toxic_collection
Update api tests and client for Add/Remove api
  • Loading branch information...
xthexder committed May 15, 2015
1 parent fc5a9c0 commit 87b3c84721df8b945484f14cb45c312f1d3c43d3
Showing with 638 additions and 263 deletions.
  1. +5 −2 Makefile
  2. +100 −19 api.go
  3. +132 −44 api_test.go
  4. +52 −4 client/client.go
  5. +42 −13 link.go
  6. +20 −10 testing/benchmark_test.go
  7. +43 −9 toxic.go
  8. +5 −14 toxic_bandwidth.go
  9. +128 −52 toxic_collection.go
  10. +5 −14 toxic_latency.go
  11. +4 −10 toxic_noop.go
  12. +4 −13 toxic_slicer.go
  13. +4 −13 toxic_slow_close.go
  14. +85 −29 toxic_test.go
  15. +4 −13 toxic_timeout.go
  16. +1 −4 toxiproxy.go
  17. +4 −0 version.go
View
@@ -7,7 +7,7 @@ COMBINED_GOPATH=$(GODEP_PATH):$(ORIGINAL_PATH)
.PHONY: packages deb test linux darwin windows
all: deb linux darwin windows docker
all: version deb linux darwin windows docker
deb: $(DEB)
darwin: tmp/build/toxiproxy-darwin-amd64
linux: tmp/build/toxiproxy-linux-amd64
@@ -21,7 +21,10 @@ clean:
rm *.deb
test:
GOMAXPROCS=4 GOPATH=$(COMBINED_GOPATH) go test -v
GOMAXPROCS=4 GOPATH=$(COMBINED_GOPATH) go test -v -race
version:
sed -i "s/Version = \"[^\"]*\"/Version = \"$(VERSION)\"/" version.go
tmp/build/toxiproxy-linux-amd64:
GOOS=linux GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@)
View
119 api.go
@@ -11,13 +11,18 @@ import (
"github.com/gorilla/mux"
)
var (
ErrInvalidStream = errors.New("invalid stream")
ErrToxicNotFound = errors.New("toxic not found")
)
type server struct {
collection *ProxyCollection
}
func NewServer(collection *ProxyCollection) *server {
func NewServer() *server {
return &server{
collection: collection,
collection: NewProxyCollection(),
}
}
@@ -29,10 +34,11 @@ func (server *server) Listen(host string, port string) {
r.HandleFunc("/proxies/{proxy}", server.ProxyShow).Methods("GET")
r.HandleFunc("/proxies/{proxy}", server.ProxyUpdate).Methods("POST")
r.HandleFunc("/proxies/{proxy}", server.ProxyDelete).Methods("DELETE")
r.HandleFunc("/proxies/{proxy}/upstream/toxics", server.ToxicIndexUpstream).Methods("GET")
r.HandleFunc("/proxies/{proxy}/downstream/toxics", server.ToxicIndexDownstream).Methods("GET")
r.HandleFunc("/proxies/{proxy}/upstream/toxics/{toxic}", server.ToxicSetUpstream).Methods("POST")
r.HandleFunc("/proxies/{proxy}/downstream/toxics/{toxic}", server.ToxicSetDownstream).Methods("POST")
r.HandleFunc("/proxies/{proxy}/{stream}/toxics", server.ToxicIndex).Methods("GET")
r.HandleFunc("/proxies/{proxy}/{stream}/toxics", server.ToxicCreate).Methods("POST")
r.HandleFunc("/proxies/{proxy}/{stream}/toxics/{toxic}", server.ToxicShow).Methods("GET")
r.HandleFunc("/proxies/{proxy}/{stream}/toxics/{toxic}", server.ToxicUpdate).Methods("POST")
r.HandleFunc("/proxies/{proxy}/{stream}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE")
r.HandleFunc("/version", server.Version).Methods("GET")
http.Handle("/", r)
@@ -212,7 +218,7 @@ func (server *server) ProxyShow(response http.ResponseWriter, request *http.Requ
}
}
func (server *server) ToxicIndexUpstream(response http.ResponseWriter, request *http.Request) {
func (server *server) ToxicIndex(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json")
vars := mux.Vars(request)
@@ -222,7 +228,16 @@ func (server *server) ToxicIndexUpstream(response http.ResponseWriter, request *
return
}
data, err := json.Marshal(proxy.upToxics.GetToxicMap())
var data []byte
switch vars["stream"] {
case "upstream":
data, err = json.Marshal(proxy.upToxics.GetToxicMap())
case "downstream":
data, err = json.Marshal(proxy.downToxics.GetToxicMap())
default:
http.Error(response, server.apiError(ErrInvalidStream, http.StatusBadRequest), http.StatusBadRequest)
return
}
if err != nil {
http.Error(response, server.apiError(err, http.StatusInternalServerError), http.StatusInternalServerError)
return
@@ -234,7 +249,7 @@ func (server *server) ToxicIndexUpstream(response http.ResponseWriter, request *
}
}
func (server *server) ToxicIndexDownstream(response http.ResponseWriter, request *http.Request) {
func (server *server) ToxicCreate(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json")
vars := mux.Vars(request)
@@ -244,19 +259,34 @@ func (server *server) ToxicIndexDownstream(response http.ResponseWriter, request
return
}
data, err := json.Marshal(proxy.downToxics.GetToxicMap())
var toxic Toxic
switch vars["stream"] {
case "upstream":
toxic, err = proxy.upToxics.AddToxicJson(request.Body)
case "downstream":
toxic, err = proxy.downToxics.AddToxicJson(request.Body)
default:
http.Error(response, server.apiError(ErrInvalidStream, http.StatusBadRequest), http.StatusBadRequest)
return
}
if err != nil {
http.Error(response, server.apiError(err, http.StatusInternalServerError), http.StatusInternalServerError)
return
}
data, err := json.Marshal(toxic)
if err != nil {
http.Error(response, server.apiError(err, http.StatusInternalServerError), http.StatusInternalServerError)
return
}
_, err = response.Write(data)
if err != nil {
logrus.Warn("ToxicIndex: Failed to write response to client", err)
logrus.Warn("ToxicCreate: Failed to write response to client", err)
}
}
func (server *server) ToxicSetUpstream(response http.ResponseWriter, request *http.Request) {
func (server *server) ToxicShow(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json")
vars := mux.Vars(request)
@@ -266,9 +296,18 @@ func (server *server) ToxicSetUpstream(response http.ResponseWriter, request *ht
return
}
toxic, err := proxy.upToxics.SetToxicJson(vars["toxic"], request.Body)
if err != nil {
http.Error(response, server.apiError(err, http.StatusBadRequest), http.StatusBadRequest)
var toxic Toxic
switch vars["stream"] {
case "upstream":
toxic = proxy.upToxics.GetToxic(vars["toxic"])
case "downstream":
toxic = proxy.downToxics.GetToxic(vars["toxic"])
default:
http.Error(response, server.apiError(ErrInvalidStream, http.StatusBadRequest), http.StatusBadRequest)
return
}
if toxic == nil {
http.Error(response, server.apiError(ErrToxicNotFound, http.StatusNotFound), http.StatusNotFound)
return
}
@@ -280,11 +319,11 @@ func (server *server) ToxicSetUpstream(response http.ResponseWriter, request *ht
_, err = response.Write(data)
if err != nil {
logrus.Warn("ToxicSet: Failed to write response to client", err)
logrus.Warn("ToxicShow: Failed to write response to client", err)
}
}
func (server *server) ToxicSetDownstream(response http.ResponseWriter, request *http.Request) {
func (server *server) ToxicUpdate(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json")
vars := mux.Vars(request)
@@ -294,7 +333,16 @@ func (server *server) ToxicSetDownstream(response http.ResponseWriter, request *
return
}
toxic, err := proxy.downToxics.SetToxicJson(vars["toxic"], request.Body)
var toxic Toxic
switch vars["stream"] {
case "upstream":
toxic, err = proxy.upToxics.UpdateToxicJson(vars["toxic"], request.Body)
case "downstream":
toxic, err = proxy.downToxics.UpdateToxicJson(vars["toxic"], request.Body)
default:
http.Error(response, server.apiError(ErrInvalidStream, http.StatusBadRequest), http.StatusBadRequest)
return
}
if err != nil {
http.Error(response, server.apiError(err, http.StatusBadRequest), http.StatusBadRequest)
return
@@ -308,7 +356,40 @@ func (server *server) ToxicSetDownstream(response http.ResponseWriter, request *
_, err = response.Write(data)
if err != nil {
logrus.Warn("ToxicSet: Failed to write response to client", err)
logrus.Warn("ToxicUpdate: Failed to write response to client", err)
}
}
func (server *server) ToxicDelete(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
proxy, err := server.collection.Get(vars["proxy"])
if err != nil {
response.Header().Set("Content-Type", "application/json")
http.Error(response, server.apiError(err, http.StatusNotFound), http.StatusNotFound)
return
}
switch vars["stream"] {
case "upstream":
err = proxy.upToxics.RemoveToxic(vars["toxic"])
case "downstream":
err = proxy.downToxics.RemoveToxic(vars["toxic"])
default:
response.Header().Set("Content-Type", "application/json")
http.Error(response, server.apiError(ErrInvalidStream, http.StatusBadRequest), http.StatusBadRequest)
return
}
if err != nil {
response.Header().Set("Content-Type", "application/json")
http.Error(response, server.apiError(err, http.StatusInternalServerError), http.StatusInternalServerError)
return
}
response.WriteHeader(http.StatusNoContent)
_, err = response.Write(nil)
if err != nil {
logrus.Warn("ToxicDelete: Failed to write headers to client", err)
}
}
Oops, something went wrong.

0 comments on commit 87b3c84

Please sign in to comment.