Skip to content

Commit

Permalink
support looking up device by name
Browse files Browse the repository at this point in the history
  • Loading branch information
wg committed Dec 6, 2016
1 parent 3fb06a3 commit f0d3eeb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/github.com/kentik/libkflow/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ func NewClient(email, token string, timeout time.Duration) *Client {
}
}

func (c *Client) GetDevice(url string, did int) (*Device, error) {
r, err := c.do("GET", fmt.Sprintf("%s/device/%d", url, did), nil)
func (c *Client) GetDeviceByID(url string, did int) (*Device, error) {
return c.getdevice(fmt.Sprintf("%s/device/%d", url, did))
}

func (c *Client) GetDeviceByName(url string, name string) (*Device, error) {
return c.getdevice(fmt.Sprintf("%s/device/%s", url, name))
}

func (c *Client) getdevice(url string) (*Device, error) {
r, err := c.do("GET", url, nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 15 additions & 2 deletions src/github.com/kentik/libkflow/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetDevice(t *testing.T) {
func TestGetDeviceByID(t *testing.T) {
client, server, device, err := test.NewClientServer()
if err != nil {
t.Fatal(err)
}
assert := assert.New(t)

device2, err := client.GetDevice(server.URL()+"/api/v5", device.ID)
device2, err := client.GetDeviceByID(server.URL()+"/api/v5", device.ID)

assert.NoError(err)
assert.EqualValues(device, device2)
}

func TestGetDeviceByName(t *testing.T) {
client, server, device, err := test.NewClientServer()
if err != nil {
t.Fatal(err)
}
assert := assert.New(t)

device2, err := client.GetDeviceByName(server.URL()+"/api/v5", device.Name)

assert.NoError(err)
assert.EqualValues(device, device2)
Expand Down
12 changes: 4 additions & 8 deletions src/github.com/kentik/libkflow/api/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/kentik/libkflow/api"
Expand Down Expand Up @@ -85,19 +86,14 @@ func (s *Server) Flows() <-chan chf.PackedCHF {
}

func (s *Server) device(w http.ResponseWriter, r *http.Request) {
var did int
id := strings.Split(r.URL.Path, "/")[4]

n, err := fmt.Sscanf(r.URL.Path, "/api/v5/device/%d", &did)
if n != 1 || err != nil {
panic(http.StatusBadRequest)
}

if did != s.Device.ID {
if id != strconv.Itoa(s.Device.ID) && id != s.Device.Name {
panic(http.StatusNotFound)
}

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&api.DeviceResponse{
err := json.NewEncoder(w).Encode(&api.DeviceResponse{
Device: s.Device,
})

Expand Down
10 changes: 9 additions & 1 deletion src/github.com/kentik/libkflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ func kflowInit(cfg *C.kflowConfig) C.int {
email = C.GoString(cfg.API.email)
token = C.GoString(cfg.API.token)
timeout = time.Duration(cfg.timeout) * time.Millisecond
device *api.Device
)

client := api.NewClient(email, token, timeout)
device, err := client.GetDevice(C.GoString(cfg.API.URL), int(cfg.device_id))

switch url := C.GoString(cfg.API.URL); {
case cfg.device_id > 0:
device, err = client.GetDeviceByID(url, int(cfg.device_id))
case cfg.hostname != nil:
device, err = client.GetDeviceByName(url, C.GoString(cfg.hostname))
}

if err != nil {
errors <- err
return C.EKFLOWCONFIG
Expand Down

0 comments on commit f0d3eeb

Please sign in to comment.