Is there any way to find a device (or list of devices) matching a partial UUID using the Python SDK, similar to how Balena CLI does it? All of the device queries seem to require a full 128-bit UUID. The best I could find was calling get_all()
to get the entire list of all devices, then looping over the list to look for matches, but that’s really inefficient.
We’re trying to write a wrapper script for Balena CLI so we can interact with devices by name instead of UUID. When working on multiple customer devices, it’s really hard to keep things straight and not accidentally operate on the wrong device when just looking at UUIDs. It’s much easier for developers to look at device names and know if it’s the right device. I’ve brought up allowing the use of device names in the CLI before (https://forums.balena.io/t/balena-tunnel-device-not-found-device-is-active-other-cli-commands-work) and I still think that’s the best solution by far, but since the CLI team doesn’t seem keen on it we’re trying to find an acceptable work-around.
Thanks,
Adam
Hey Adam, I’ve messaged the python sdk maintainer in order to give you guys an example on how you might go about doing. this as I am not very familiar with the Python SDK, but it is definitely possible. Generally speaking, our API is very flexible and it should be possible to craft any type of query that might work the best for your use-case.
Hi,
Currently the Python SDK doesn’t support add extra options to the get_all()
method but you can still use the SDK to create a custom request as you want, for your case we can use $startswith
query option to find a device (or list of devices) matching a partial UUID. Here is an example for you:
#!/usr/bin/python
from balena import Balena
from balena.base_request import BaseRequest
from balena.settings import Settings
base_request = BaseRequest()
settings = Settings()
uuid = '' # UUID GOES HERE
credentials = {}
balena = Balena()
balena.auth.login(**credentials)
raw_query="$filter=startswith(uuid, '{uuid}')".format(uuid=uuid)
print(base_request.request('device', 'GET', raw_query=raw_query, endpoint=settings.get('pine_endpoint'))['d'])
Please let me know if it works for you or you need further support on anything.
Yep, that works great! Thanks for the suggestion.