Update the config format.

This commit is contained in:
Cyril Duez 2016-06-01 19:35:08 +02:00 committed by François de Metz
parent daa8dc2bc6
commit 6894033511
7 changed files with 37 additions and 68 deletions

View File

@ -8,22 +8,13 @@ import (
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_models"
)
func init() {
webhooks_models.Add("github", func(path string) webhooks_models.Webhook { return NewGithubWebhook(path) })
}
type GithubWebhook struct {
Path string
acc telegraf.Accumulator
}
func NewGithubWebhook(path string) *GithubWebhook {
return &GithubWebhook{Path: path}
}
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
log.Printf("Started the webhooks_github on %s\n", gh.Path)

View File

@ -11,8 +11,7 @@ import (
func GithubWebhookRequest(event string, jsonString string, t *testing.T) {
var acc testutil.Accumulator
gh := NewGithubWebhook("/github")
gh.acc = &acc
gh := &GithubWebhook{Path: "/github", acc: &acc}
req, _ := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", event)
w := httptest.NewRecorder()

View File

@ -10,22 +10,13 @@ import (
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_models"
)
func init() {
webhooks_models.Add("rollbar", func(path string) webhooks_models.Webhook { return NewRollbarWebhook(path) })
}
type RollbarWebhook struct {
Path string
acc telegraf.Accumulator
}
func NewRollbarWebhook(path string) *RollbarWebhook {
return &RollbarWebhook{Path: path}
}
func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
log.Printf("Started the webhooks_rollbar on %s\n", rb.Path)

View File

@ -21,8 +21,7 @@ func postWebhooks(rb *RollbarWebhook, eventBody string) *httptest.ResponseRecord
func TestNewItem(t *testing.T) {
var acc testutil.Accumulator
rb := NewRollbarWebhook("/rollbar")
rb.acc = &acc
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc}
resp := postWebhooks(rb, NewItemJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
@ -45,8 +44,7 @@ func TestNewItem(t *testing.T) {
func TestDeploy(t *testing.T) {
var acc testutil.Accumulator
rb := NewRollbarWebhook("/rollbar")
rb.acc = &acc
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc}
resp := postWebhooks(rb, DeployJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST deploy returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
@ -66,7 +64,7 @@ func TestDeploy(t *testing.T) {
}
func TestUnknowItem(t *testing.T) {
rb := NewRollbarWebhook("/rollbar")
rb := &RollbarWebhook{Path: "/rollbar"}
resp := postWebhooks(rb, UnknowJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST unknow returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)

View File

@ -4,14 +4,20 @@ import (
"fmt"
"log"
"net/http"
"reflect"
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
_ "github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_all"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_models"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
)
type Webhook interface {
Register(router *mux.Router, acc telegraf.Accumulator)
}
func init() {
inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() })
}
@ -19,12 +25,8 @@ func init() {
type Webhooks struct {
ServiceAddress string
Webhook []WebhookConfig
}
type WebhookConfig struct {
Name string
Path string
Github *github.GithubWebhook
Rollbar *rollbar.RollbarWebhook
}
func NewWebhooks() *Webhooks {
@ -36,14 +38,12 @@ func (wb *Webhooks) SampleConfig() string {
## Address and port to host Webhook listener on
service_address = ":1619"
[[inputs.webhooks.webhook]]
name = "github"
[inputs.webhooks.github]
path = "/github"
[[inputs.webhooks.webhook]]
name = "rollbar"
[inputs.webhooks.rollbar]
path = "/rollbar"
`
`
}
func (wb *Webhooks) Description() string {
@ -56,20 +56,32 @@ func (wb *Webhooks) Gather(_ telegraf.Accumulator) error {
func (wb *Webhooks) Listen(acc telegraf.Accumulator) {
r := mux.NewRouter()
for _, webhook := range wb.Webhook {
if plugin, ok := webhooks_models.Webhooks[webhook.Name]; ok {
sub := plugin(webhook.Path)
sub.Register(r, acc)
} else {
log.Printf("Webhook %s is unknow\n", webhook.Name)
}
for _, webhook := range wb.AvailableWebhooks() {
webhook.Register(r, acc)
}
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r)
if err != nil {
log.Printf("Error starting server: %v", err)
}
}
// Looks for fields which implement Webhook interface
func (wb *Webhooks) AvailableWebhooks() []Webhook {
webhooks := make([]Webhook, 0)
s := reflect.ValueOf(wb).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if wbPlugin, ok := f.Interface().(Webhook); ok {
webhooks = append(webhooks, wbPlugin)
}
}
return webhooks
}
func (wb *Webhooks) Start(acc telegraf.Accumulator) error {
go wb.Listen(acc)
log.Printf("Started the webhooks service on %s\n", wb.ServiceAddress)

View File

@ -1,6 +0,0 @@
package webhooks_all
import (
_ "github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
_ "github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
)

View File

@ -1,16 +0,0 @@
package webhooks_models
import (
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
)
type Webhook interface {
Register(router *mux.Router, acc telegraf.Accumulator)
}
var Webhooks map[string]func(string) Webhook = make(map[string]func(string) Webhook)
func Add(name string, fun func(string) Webhook) {
Webhooks[name] = fun
}