Skip to content

Commit f0d3eeb

Browse files
committed
support looking up device by name
1 parent 3fb06a3 commit f0d3eeb

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

src/github.com/kentik/libkflow/api/client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ func NewClient(email, token string, timeout time.Duration) *Client {
3030
}
3131
}
3232

33-
func (c *Client) GetDevice(url string, did int) (*Device, error) {
34-
r, err := c.do("GET", fmt.Sprintf("%s/device/%d", url, did), nil)
33+
func (c *Client) GetDeviceByID(url string, did int) (*Device, error) {
34+
return c.getdevice(fmt.Sprintf("%s/device/%d", url, did))
35+
}
36+
37+
func (c *Client) GetDeviceByName(url string, name string) (*Device, error) {
38+
return c.getdevice(fmt.Sprintf("%s/device/%s", url, name))
39+
}
40+
41+
func (c *Client) getdevice(url string) (*Device, error) {
42+
r, err := c.do("GET", url, nil)
3543
if err != nil {
3644
return nil, err
3745
}

src/github.com/kentik/libkflow/api/client_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,27 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestGetDevice(t *testing.T) {
10+
func TestGetDeviceByID(t *testing.T) {
1111
client, server, device, err := test.NewClientServer()
1212
if err != nil {
1313
t.Fatal(err)
1414
}
1515
assert := assert.New(t)
1616

17-
device2, err := client.GetDevice(server.URL()+"/api/v5", device.ID)
17+
device2, err := client.GetDeviceByID(server.URL()+"/api/v5", device.ID)
18+
19+
assert.NoError(err)
20+
assert.EqualValues(device, device2)
21+
}
22+
23+
func TestGetDeviceByName(t *testing.T) {
24+
client, server, device, err := test.NewClientServer()
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
assert := assert.New(t)
29+
30+
device2, err := client.GetDeviceByName(server.URL()+"/api/v5", device.Name)
1831

1932
assert.NoError(err)
2033
assert.EqualValues(device, device2)

src/github.com/kentik/libkflow/api/test/server.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net/http"
1717
"os"
1818
"strconv"
19+
"strings"
1920
"time"
2021

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

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

90-
n, err := fmt.Sscanf(r.URL.Path, "/api/v5/device/%d", &did)
91-
if n != 1 || err != nil {
92-
panic(http.StatusBadRequest)
93-
}
94-
95-
if did != s.Device.ID {
91+
if id != strconv.Itoa(s.Device.ID) && id != s.Device.Name {
9692
panic(http.StatusNotFound)
9793
}
9894

9995
w.Header().Set("Content-Type", "application/json")
100-
err = json.NewEncoder(w).Encode(&api.DeviceResponse{
96+
err := json.NewEncoder(w).Encode(&api.DeviceResponse{
10197
Device: s.Device,
10298
})
10399

src/github.com/kentik/libkflow/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ func kflowInit(cfg *C.kflowConfig) C.int {
2929
email = C.GoString(cfg.API.email)
3030
token = C.GoString(cfg.API.token)
3131
timeout = time.Duration(cfg.timeout) * time.Millisecond
32+
device *api.Device
3233
)
3334

3435
client := api.NewClient(email, token, timeout)
35-
device, err := client.GetDevice(C.GoString(cfg.API.URL), int(cfg.device_id))
36+
37+
switch url := C.GoString(cfg.API.URL); {
38+
case cfg.device_id > 0:
39+
device, err = client.GetDeviceByID(url, int(cfg.device_id))
40+
case cfg.hostname != nil:
41+
device, err = client.GetDeviceByName(url, C.GoString(cfg.hostname))
42+
}
43+
3644
if err != nil {
3745
errors <- err
3846
return C.EKFLOWCONFIG

0 commit comments

Comments
 (0)