Add admin interface

This commit is contained in:
Tim Raymond 2016-02-10 16:05:17 -05:00
parent 1fa3a447d4
commit f3ff0e8be8
2 changed files with 51 additions and 7 deletions

View File

@ -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")))
}

View File

@ -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")
}
}
}