From 2f85731a22db4f43e0c6c717d4a731569347e675 Mon Sep 17 00:00:00 2001 From: Tim Raymond Date: Wed, 10 Feb 2016 14:54:34 -0500 Subject: [PATCH] WIP First pass at registration --- services/enterprise/enterprise.go | 45 ++++++++++++++++++++++++++ services/enterprise/enterprise_test.go | 41 +++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 services/enterprise/enterprise.go create mode 100644 services/enterprise/enterprise_test.go diff --git a/services/enterprise/enterprise.go b/services/enterprise/enterprise.go new file mode 100644 index 000000000..95ba82686 --- /dev/null +++ b/services/enterprise/enterprise.go @@ -0,0 +1,45 @@ +package enterprise + +import ( + "log" + "os" + + "github.com/influxdata/enterprise-client/v2" +) + +type Config struct { + Hosts []*client.Host +} + +type Service struct { + hosts []*client.Host + logger *log.Logger +} + +func NewEnterprise(c Config) *Service { + return &Service{ + hosts: c.Hosts, + logger: log.New(os.Stdout, "[enterprise]", log.Ldate|log.Ltime), + } +} + +func (s *Service) Open() { + cl, err := client.New(s.hosts) + if err != nil { + s.logger.Printf("Unable to contact one or more Enterprise hosts. err: %s", err.Error()) + return + } + go s.registerProduct(cl) +} + +func (s *Service) registerProduct(cl *client.Client) { + p := client.Product{ + ProductID: "telegraf", + Host: "localhost", + } + + _, err := cl.Register(&p) + if err != nil { + s.logger.Println("Unable to register Telegraf with Enterprise") + } +} diff --git a/services/enterprise/enterprise_test.go b/services/enterprise/enterprise_test.go new file mode 100644 index 000000000..43e567ac2 --- /dev/null +++ b/services/enterprise/enterprise_test.go @@ -0,0 +1,41 @@ +package enterprise_test + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/influxdata/enterprise-client/v2" + "github.com/influxdata/telegraf/services/enterprise" +) + +func Test_RegistersWithEnterprise(t *testing.T) { + success := make(chan struct{}) + mockEnterprise := http.NewServeMux() + mockEnterprise.HandleFunc("/api/v2/products", func(rw http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + close(success) + } + }) + srv := httptest.NewServer(mockEnterprise) + defer srv.Close() + + c := enterprise.Config{ + Hosts: []*client.Host{ + &client.Host{URL: srv.URL}, + }, + } + e := enterprise.NewEnterprise(c) + e.Open() + + timeout := time.After(50 * time.Millisecond) + for { + select { + case <-success: + return + case <-timeout: + t.Fatal("Expected to receive call to Enterprise API, but received none") + } + } +}