Add basic filestack webhook.

This commit is contained in:
François de Metz 2016-06-28 19:26:41 +02:00
parent a8f9eb23cc
commit 4176375480
5 changed files with 157 additions and 3 deletions

View File

@ -0,0 +1,44 @@
package filestack
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
)
type FilestackWebhook struct {
Path string
acc telegraf.Accumulator
}
func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
router.HandleFunc(fs.Path, fs.eventHandler).Methods("POST")
log.Printf("Started the webhooks_filestack on %s\n", fs.Path)
fs.acc = acc
}
func (fs *FilestackWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
event := &DialogEvent{}
err = json.Unmarshal(body, event)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
fs.acc.AddFields("filestack_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0))
w.WriteHeader(http.StatusOK)
}

View File

@ -0,0 +1,21 @@
package filestack
import "strconv"
type DialogEvent struct {
Action string `json:"action"`
TimeStamp int64 `json:"timestamp"`
Id int `json:"id"`
}
func (de *DialogEvent) Tags() map[string]string {
return map[string]string{
"action": de.Action,
}
}
func (de *DialogEvent) Fields() map[string]interface{} {
return map[string]interface{}{
"id": strconv.Itoa(de.Id),
}
}

View File

@ -0,0 +1,41 @@
package filestack
func DialogOpenJSON() string {
return `{
"action": "fp.dialog",
"timestamp": 1435584646,
"id": 102,
"text": {
"mimetypes": ["*/*"],
"iframe": false,
"language": "en",
"id": "1435584650723",
"mobile": false,
"app":{
"upsell": "false",
"apikey": "YOUR_API_KEY",
"customization":{
"saveas_subheader": "Save it down to your local device or onto the Cloud",
"folder_subheader": "Choose a folder to share with this application",
"open_subheader": "Choose from the files on your local device or the ones you have online",
"folder_header": "Select a folder",
"help_text": "",
"saveas_header": "Save your file",
"open_header": "Upload a file"
}
},
"dialogType": "open",
"auth": false,
"welcome_header": "Upload a file",
"welcome_subheader": "Choose from the files on your local device or the ones you have online",
"help_text": "",
"recent_path": "/",
"extensions": null,
"maxSize": 0,
"signature": null,
"policy": null,
"custom_providers": "imgur,cloudapp",
"intra": false
}
}`
}

View File

@ -0,0 +1,46 @@
package filestack
import (
"github.com/influxdata/telegraf/testutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func postWebhooks(md *FilestackWebhook, eventBody string) *httptest.ResponseRecorder {
req, _ := http.NewRequest("POST", "/filestack", strings.NewReader(eventBody))
w := httptest.NewRecorder()
md.eventHandler(w, req)
return w
}
func TestDialogEvent(t *testing.T) {
var acc testutil.Accumulator
fs := &FilestackWebhook{Path: "/filestack", acc: &acc}
resp := postWebhooks(fs, DialogOpenJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
}
fields := map[string]interface{}{
"id": "102",
}
tags := map[string]string{
"action": "fp.dialog",
}
acc.AssertContainsTaggedFields(t, "filestack_webhooks", fields, tags)
}
func TestParseError(t *testing.T) {
fs := &FilestackWebhook{Path: "/filestack"}
resp := postWebhooks(fs, "")
if resp.Code != http.StatusBadRequest {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest)
}
}

View File

@ -10,6 +10,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/filestack"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/mandrill"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
@ -26,9 +27,10 @@ func init() {
type Webhooks struct {
ServiceAddress string
Github *github.GithubWebhook
Mandrill *mandrill.MandrillWebhook
Rollbar *rollbar.RollbarWebhook
Github *github.GithubWebhook
Filestack *filestack.FilestackWebhook
Mandrill *mandrill.MandrillWebhook
Rollbar *rollbar.RollbarWebhook
}
func NewWebhooks() *Webhooks {