packngo icon indicating copy to clipboard operation
packngo copied to clipboard

Query devices by tags

Open jasmingacic opened this issue 7 years ago • 16 comments

I've been asked this question https://github.com/hashicorp/go-discover/pull/52 about tags. If it was possible to query the API for devices with certain tags. I can't find anything in the API doc cc// @jacobsmith928 @mberrueta

jasmingacic avatar Jul 16 '18 22:07 jasmingacic

curl '../api/devices?with_tag=Hello' for a single tag curl '../api/devices?with_tags=Hello,bye' for multiple ones

mberrueta avatar Jul 17 '18 11:07 mberrueta

@mberrueta it does not seem to be working

$curl -X GET --header 'Accept: application/json' --header 'X-Auth-Token: [token]' 'https://api.packet.net/projects/93125c2a-8b78-4d4f-a3c4-7367d6b7cca8/devices?with_tag=test01' | jq '.devices | length'
8

It returns list of 8 devices where it should be returning only 2 (see the screenshot)

image

jasmingacic avatar Jul 17 '18 12:07 jasmingacic

sorry my bad, is without the with_

curl '../api/devices?tag=test01' or curl '../api/devices?tags=test01,test02'

mberrueta avatar Jul 17 '18 14:07 mberrueta

Confirmed!!!

curl -X GET --header 'Accept: application/json' --header 'X-Auth-Token: [token]' 'https://api.packet.net/projects/93125c2a-8b78-4d4f-a3c4-7367d6b7cca8/devices?tag=test01' | jq '.devices | length'

2

jasmingacic avatar Jul 17 '18 14:07 jasmingacic

But not with two tags:

$ curl -X GET --header 'Accept: application/json' --header 'X-Auth-Token: [token]' 'https://api.packet.net/projects/93125c2a-8b78-4d4f-a3c4-7367d6b7cca8/devices?tags=test01,test03' | jq '.devices | length'
8

It should have returned 4

jasmingacic avatar Jul 17 '18 14:07 jasmingacic

it's an or and probably you need an and

mberrueta avatar Jul 17 '18 14:07 mberrueta

I played with | and & but it didn't seem to work as expected.

curl -X GET --header 'Accept: application/json' --header 'X-Auth-Token: [token]' 'https://api.packet.net/projects/93125c2a-8b78-4d4f-a3c4-7367d6b7cca8/devices?tag=test01&test03' | jq '.devices | length'
2

Expected result is 4 devices I also tried using tags instead of tag and the API returned all devices (count 8)

jasmingacic avatar Jul 17 '18 20:07 jasmingacic

created a request in jira, will go back here as soon as it's done

mberrueta avatar Jul 17 '18 23:07 mberrueta

Hi @mberrueta, do we know if this has been resolved?

patrickdevivo avatar Sep 17 '18 14:09 patrickdevivo

hi @patrickdevivo not yet, it still on the backlog

mberrueta avatar Sep 17 '18 17:09 mberrueta

@jasmingacic can you test now GET /projects/id/devices?tags[]=test01&tags[]=test02

this should get only with both tags

mberrueta avatar Sep 25 '18 19:09 mberrueta

@mberrueta Is there a reason or requirement for the brackets in the query parameter key name? I'm not aware of any obvious problems with the approach (other than possibly having to encode the characters), but it does feel a little atypical and unnecessary with URI query parameters.

edevenport avatar Sep 26 '18 12:09 edevenport

not sure why it allow with [] but you can call GET /projects/id/devices?tags[]=test01&tags[]=test02 or without GET /projects/id/devices?tag=test01&tag=test02

mberrueta avatar Sep 26 '18 14:09 mberrueta

@jasmingacic @t0mk any thoughts on how this should be implemented?

We could potentially add a QueryParams field to the ListOptions struct, which is a list of query params that get passed through to the request, to support this in a generic way.

An "issue" with that is we'd have a generic way of passing query params, but only for .List(...) and not for other calls, which might feel a little asymmetric/incomplete

patrickdevivo avatar Sep 30 '18 19:09 patrickdevivo

I'd go with ListOptions for all functions like Get and List, and I would remove GetExtra.

jasmingacic avatar Oct 01 '18 09:10 jasmingacic

https://coderwall.com/p/uh8kiw/pass-arrays-objects-via-querystring-the-rack-rails-way is why the param[]

mberrueta avatar Oct 01 '18 09:10 mberrueta