Cannot retrieve devices by tag values any more

I’m trying to use the Balena API to update the release assigned to a number of devices in my app, depending on the values of some of the tags I’ve assigned to the devices.

For example in the app ‘Jason’, I want to set the release for all devices with the tags ‘my.environment’ set to ‘debug’ and ‘my.location’ set to home.

Until quite recently I was using some code from: https://github.com/balena-io-projects/staged-releases at https://github.com/balena-io-projects/staged-releases/blob/master/update-test-group.sh to retrieve the devices, and issue a PATCH to update the release.

However, even the call at the bottom of that script has stopped working on v4 and v5 of the API. Issuing a GET on it returns “Malformed url”.

Is there a different working way to retrieve devices based on the values of tags attached to them?

Thanks

Hi Jason. Let me check with the team and we will come back to you.

Thank you!

Hi there, the scripts simply call our API, so you can do the same and modify them to your needs. The scripts have the version hard-coded currently to v5, so they should work for v5 without any issues. Can you please share how you are calling the script?

HI - I’ve been calling directly into the API and have been using HTTP GET on URLs like:

https://api.balena-cloud.com/v4/device?%24expand=belongs_to__application(%24select=app_name),device_tag(%24select=id,tag_key,value)&%24filter=(belongs_to__application/app_name%20eq%20’Jason’%20and%20((device_tag/any(dt:(((tag_key)%20eq%20(‘my.environment’))%20and%20((value)%20eq%20(‘debug’)))))%20and%20(device_tag/any(dt:(((tag_key)%20eq%20(‘my.location’))%20and%20((value)%20eq%20(‘home’)))))))

These used to work - they’re based on the code in the shell script above - and they used to return a collection of devices with the matching labels and contents. I could then issue an HTTP PATCH in the format:

{“should_be_running__release”:“<RELEASE_ID>”}

to the same URL, and that used to update all of the devices, and make them download the latest release.

Now when I issue the GET command for URLs in that format I get “Malformed URL”

It is a bit difficult to see what is wrong in the URL, but maybe something changed in the API that made your URL not work. On a positive note, we will soon release this functionality in the dashboard, so you won’t need custom scripts. I will check in a bit more detail what changed to affect this and test the staged releases scripts if there was some regression there as well.

I tested the script you linked before and it works as expected. Is there any reason you are not using the script?

Hi - the script doesn’t actually do precisely what I need - I need to compare the presence and values of two tags on a release, not just the one that the script handles.

I see, that wasn’t clear to me initially. In that case, what I would suggest is cloning the staged-releases scripts, adding the changes you have mentioned before, and see if that works better. What you have done above should work, but in the form posted it is really difficult to debug where the issue lies. As I said, we should surface this in the dashboard soon, so you will be able to filter by tag there and pin devices to a specific release.