Renaming a device via the API

Hey guys,

I was looking for a way to rename my devices in an automated way. I have been using the python SDK with great success and I was wondering if there was a public route for renaming a device.

Thanks,

-Thomas

Hello Thomas. The python SDK exposes a function to update the device name (https://www.balena.io/docs/reference/sdk/python-sdk/#function-renameuuid-new_name). Under the hood it simply patches the device resource in the API with the new name, you can see an example in https://www.balena.io/docs/reference/api/resources/device/#rename-device

Thank @nazrhom I am not sure how I missed that :confused:

But I have it now! :slight_smile:

Does it also restart the supervisor so the device takes the name or should I do that with the SDK after a rename?

-Thomas

Hi,
If you are relying on an env var to be available on your application’s container to get the device name (I guess that you are using the BALENA_DEVICE_NAME_AT_INI), then you should be manually triggering a restart after the rename.

You can use the device application restart method of the python SDK as documented here:
https://www.balena.io/docs/reference/sdk/python-sdk/#function-restartuuid
This simply does a POST to https://api.balena-cloud.com/device/<device_id>/restart , in case you prefer to do this with a simple curl.

@thgreasi

That’s correct, I plan on using the name to help tell the devices where they are and who they belong two. As I merge my 30+ applications into 2 or 3 giant applications.

-Thomas

That sounds like a good approach.
Keep in mind that you can also use device tags to better organize your fleet and both the node & python SDK provide such operations.
Let me point you to the respective documentation page:
https://www.balena.io/docs/learn/manage/filters-tags/#device-tags

Let us know if you have any further questions.

Hey @thgreasi,

I took your suggestions to hard and implemented the python SDK in my application. Sadly, as I use the logging modules, and so does the python-sdk, all my messages get duplicated now.

As the logging module was only used to log all of one line, I submitted a pull request to have it removed and replaces with a simple print statement.

See it here

I think the maintainers are: @xginn8 and @nghiant2710

It would great if it could be approved or another solution suggested.

Thanks!
-Thomas