From f3ff0e8be817a1ff1ff198e65183d174452e0a2c Mon Sep 17 00:00:00 2001 From: Tim Raymond Date: Wed, 10 Feb 2016 16:05:17 -0500 Subject: [PATCH] Add admin interface --- services/enterprise/enterprise.go | 25 +++++++++++++------ services/enterprise/enterprise_test.go | 33 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/services/enterprise/enterprise.go b/services/enterprise/enterprise.go index 02537f1fb..a4905ab56 100644 --- a/services/enterprise/enterprise.go +++ b/services/enterprise/enterprise.go @@ -1,27 +1,33 @@ package enterprise import ( + "fmt" "log" + "net/http" "os" "github.com/influxdata/enterprise-client/v2" + "github.com/influxdata/enterprise-client/v2/admin" ) type Config struct { - Hosts []*client.Host + AdminPort uint16 + Hosts []*client.Host } type Service struct { - hosts []*client.Host - logger *log.Logger - hostname string + hosts []*client.Host + logger *log.Logger + hostname string + adminPort string } func NewEnterprise(c Config, hostname string) *Service { return &Service{ - hosts: c.Hosts, - hostname: hostname, - logger: log.New(os.Stdout, "[enterprise]", log.Ldate|log.Ltime), + hosts: c.Hosts, + hostname: hostname, + logger: log.New(os.Stdout, "[enterprise]", log.Ldate|log.Ltime), + adminPort: fmt.Sprintf(":%d", c.AdminPort), } } @@ -32,6 +38,7 @@ func (s *Service) Open() { return } go s.registerProduct(cl) + go s.startAdminInterface() } func (s *Service) registerProduct(cl *client.Client) { @@ -45,3 +52,7 @@ func (s *Service) registerProduct(cl *client.Client) { s.logger.Println("Unable to register Telegraf with Enterprise") } } + +func (s *Service) startAdminInterface() { + go http.ListenAndServe(s.adminPort, admin.App("foo", []byte("bar"))) +} diff --git a/services/enterprise/enterprise_test.go b/services/enterprise/enterprise_test.go index 94a38edf3..5cc1a9a62 100644 --- a/services/enterprise/enterprise_test.go +++ b/services/enterprise/enterprise_test.go @@ -2,6 +2,7 @@ package enterprise_test import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "testing" @@ -59,3 +60,35 @@ func Test_RegistersWithEnterprise(t *testing.T) { } } } + +func Test_StartsAdminInterface(t *testing.T) { + hostname := "localhost" + adminPort := 2300 + + success, srv := mockEnterprise(func(c *client.Product, err error) {}) + defer srv.Close() + + c := enterprise.Config{ + Hosts: []*client.Host{ + &client.Host{URL: srv.URL}, + }, + AdminPort: 2300, + } + + e := enterprise.NewEnterprise(c, hostname) + e.Open() + + timeout := time.After(1 * time.Millisecond) + for { + select { + case <-success: + _, err := http.Get(fmt.Sprintf("http://%s:%d", hostname, adminPort)) + if err != nil { + t.Errorf("Unable to connect to admin interface: err: %s", err) + } + return + case <-timeout: + t.Fatal("Expected to receive call to Enterprise API, but received none") + } + } +}