[go: nahoru, domu]

Skip to content

Commit

Permalink
changes rampage API to JSON payload
Browse files Browse the repository at this point in the history
  • Loading branch information
mhausenblas committed Jun 13, 2016
1 parent 10e9610 commit f9eedb5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ To test it locally, run:

$ MARATHON_URL=http://localhost:8080 drax

And invoke like so (to destroy tasks of app `/dummy`)
And invoke like so (to destroy tasks of app `/dummy`):

$ cat rp.json
{
"level" : "1",
"app" : "dummy"
}
$ http POST localhost:7777/rampage < rp.json
HTTP/1.1 200 OK
Content-Length: 117
Content-Type: application/javascript
Date: Mon, 13 Jun 2016 13:05:31 GMT

{"success":true,"goners":["dummy.59dca877-3165-11e6-aad0-1e9bbbc1653f","dummy.e96ffce3-3164-11e6-aad0-1e9bbbc1653f"]}

$ http -f POST localhost:7777/rampage -- level=1 app=dummy
29 changes: 24 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ type NOUN_Stats struct{}
type NOUN_Rampage struct{}

// JSON payloads
type RampageParams struct {
Level string `json:"level"`
AppID string `json:"app"`
}
type StatsResult struct {
TasksKilled uint64 `json:"gone"`
}

type RampageResult struct {
Success bool `json:"success"`
TaskList []string `json:"goners"`
}

// Handles /stats API calls
// Handles /stats API calls (GET only)
func (n NOUN_Stats) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"handle": "/stats"}).Info("Reporting on runtime statistics ...")
// extract $RUNS parameter from /stats?runs=$RUNS in the following:
Expand All @@ -45,14 +48,18 @@ func (n NOUN_Stats) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, string(jsonsr))
}

// Handles /rampage API calls
// Handles /rampage API calls (POST only)
func (n NOUN_Rampage) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
http.Error(w, "Can't parse rampage params", 500)
} else {
levelParam := r.Form.Get("level")
ok, rp := parseRampageParams(r)
if !ok {
http.Error(w, "Can't decode rampage params", 500)
}
levelParam := rp.Level
if levelParam != "" {
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Got level param ", levelParam)
if level, err := strconv.Atoi(levelParam); err == nil {
Expand All @@ -64,7 +71,7 @@ func (n NOUN_Rampage) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case DL_BASIC:
killTasks(w, r)
case DL_ADVANCED:
appParam := r.Form.Get("app")
appParam := rp.AppID
if appParam != "" {
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Got app param ", appParam)
killTasksOfApp(w, r, appParam)
Expand All @@ -83,6 +90,18 @@ func (n NOUN_Rampage) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// parseRampageParams parses the parameters for a rampage from an HTTP request
func parseRampageParams(r *http.Request) (bool, *RampageParams) {
decoder := json.NewDecoder(r.Body)
rp := &RampageParams{}
err := decoder.Decode(rp)
if err != nil {
return false, nil
} else {
return true, rp
}
}

// killTasks will identify tasks of any apps (but not framework services)
// and randomly kill off a few of them
func killTasks(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit f9eedb5

Please sign in to comment.