Balena Python SDK
Welcome to the balena python SDK documentation. This document aims to describe all the functions supported by the SDK, as well as showing examples of their expected usage.
Install the Balena SDK:
From Pip:
pip install balena-sdk
From Source (In case, you want to test a development branch):
https://github.com/balena-io/balena-sdk-python
Getting started:
>>> from balena import Balena
>>> balena = Balena()
>>> credentials = {'username':<your email>, 'password':<your password>}
>>> balena.auth.login(**credentials)
...
If you feel something is missing, not clear or could be improved, please don't hesitate to open an issue in GitHub, we'll be happy to help.
Table of Contents
- Balena
Models
This module implements all models for balena python SDK.
Application
This class implements application model for balena python SDK.
The returned objects properties are
__metadata, actor, app_name, application_type, commit, depends_on__application, device_type,
id, is_accessible_by_support_until__date, should_track_latest_release, slug, user
.
Function: create(name, device_type, organization, app_type)
Create an application. This function only works if you log in using credentials or Auth Token.
Args:
name (str): application name.
device_type (str): device type (display form).
organization (str): handle or id of the organization that the application will belong to.
app_type (Optional[str]): application type.
Returns:
dict: application info.
Raises:
InvalidDeviceType: if device type is not supported.
InvalidApplicationType: if app type is not supported.
InvalidParameter: if organization is missing.
OrganizationNotFound: if organization couldn't be found.
Examples:
>>> balena.models.application.create('foo', 'Raspberry Pi 3', 12345, 'microservices')
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "foo",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(12345)"},
"is_accessible_by_support_until__date": None,
"actor": 12345,
"id": 12345,
"user": {"__deferred": {"uri": "/resin/user(12345)"}, "__id": 12345},
"device_type": "raspberrypi3",
"commit": None,
"slug": "my_user/foo",
}
Function: disable_device_urls(app_id)
Disable device urls for all devices that belong to an application.
Args:
app_id (str): application id.
Returns:
OK/error.
Examples:
>> > balena.models.application.disable_device_urls('5685')
'OK'
Function: disable_rolling_updates(app_id)
Disable Rolling update on application.
Args:
name (str): application id.
Returns:
OK/error.
Raises:
ApplicationNotFound: if application couldn't be found.
Examples:
>> > balena.models.application.disable_rolling_updates('106640')
'OK'
Function: enable_device_urls(app_id)
Enable device urls for all devices that belong to an application
Args:
app_id (str): application id.
Returns:
OK/error.
Examples:
>> > balena.models.application.enable_device_urls('5685')
'OK'
Function: enable_rolling_updates(app_id)
Enable Rolling update on application.
Args:
app_id (str): application id.
Returns:
OK/error.
Raises:
ApplicationNotFound: if application couldn't be found.
Examples:
>> > balena.models.application.enable_rolling_updates('106640')
'OK'
Function: generate_provisioning_key(app_id, key_name, description, expiry_date)
Generate a device provisioning key for a specific application.
Args:
app_id (str): application id.
key_name (Optional[str]): provisioning key name.
description (Optional[str]): description for provisioning key.
expiry_date (Optional[str]): expiry date for provisioning key, for example: `2030-01-01T00:00:00Z`.
Returns:
str: device provisioning key.
Examples:
>> > balena.models.application.generate_provisioning_key('5685')
'GThZJps91PoJCdzfYqF7glHXzBDGrkr9'
Function: get(name)
Get a single application.
Args:
name (str): application name.
Returns:
dict: application info.
Raises:
ApplicationNotFound: if application couldn't be found.
AmbiguousApplication: when more than one application is returned.
Examples:
>>> balena.models.application.get('foo')
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "foo",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(12345)"},
"is_accessible_by_support_until__date": None,
"actor": 12345,
"id": 12345,
"user": {"__deferred": {"uri": "/resin/user(12345)"}, "__id": 12345},
"device_type": "raspberrypi3",
"commit": None,
"slug": "my_user/foo",
}
Function: get_all()
Get all applications (including collaborator applications).
Returns:
list: list contains info of applications.
Examples:
>>> balena.models.application.get_all()
[
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "foo",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(12345)"},
"is_accessible_by_support_until__date": None,
"actor": 12345,
"id": 12345,
"user": {"__deferred": {"uri": "/resin/user(12345)"}, "__id": 12345},
"device_type": "raspberrypi3",
"commit": None,
"slug": "my_user/foo",
},
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "bar",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(12346)"},
"is_accessible_by_support_until__date": None,
"actor": 12345,
"id": 12346,
"user": {"__deferred": {"uri": "/resin/user(12345)"}, "__id": 12345},
"device_type": "raspberrypi3",
"commit": None,
"slug": "my_user/bar",
},
]
Function: get_by_id(app_id)
Get a single application by application id.
Args:
app_id (str): application id.
Returns:
dict: application info.
Raises:
ApplicationNotFound: if application couldn't be found.
Examples:
>>> balena.models.application.get_by_id(12345)
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "foo",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(12345)"},
"is_accessible_by_support_until__date": None,
"actor": 12345,
"id": 12345,
"user": {"__deferred": {"uri": "/resin/user(12345)"}, "__id": 12345},
"device_type": "raspberrypi3",
"commit": None,
"slug": "my_user/foo",
}
Function: get_by_owner(name, owner)
Get a single application using the appname and the handle of the owning organization.
Args:
name (str): application name.
owner (str): The handle of the owning organization.
Returns:
dict: application info.
Raises:
ApplicationNotFound: if application couldn't be found.
AmbiguousApplication: when more than one application is returned.
Examples:
>>> balena.models.application.get_by_owner('foo', 'my_org')
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "foo",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(12345)"},
"is_accessible_by_support_until__date": None,
"actor": 12345,
"id": 12345,
"user": {"__deferred": {"uri": "/resin/user(12345)"}, "__id": 12345},
"device_type": "raspberrypi3",
"commit": None,
"slug": "my_user/foo",
}
Function: get_config(app_id, version)
Download application config.json.
Args:
app_id (str): application id.
version (str): the OS version of the image.
**options (dict): OS configuration keyword arguments to use. The available options are listed below:
network (Optional[str]): the network type that the device will use, one of 'ethernet' or 'wifi' and defaults to 'ethernet' if not specified.
appUpdatePollInterval (Optional[str]): how often the OS checks for updates, in minutes.
wifiKey (Optional[str]): the key for the wifi network the device will connect to.
wifiSsid (Optional[str]): the ssid for the wifi network the device will connect to.
ip (Optional[str]): static ip address.
gateway (Optional[str]): static ip gateway.
netmask (Optional[str]): static ip netmask.
Returns:
dict: application config.json content.
Raises:
ApplicationNotFound: if application couldn't be found.
Function: get_dashboard_url(app_id)
Get Dashboard URL for a specific application.
Args:
app_id (str): application id.
Raises:
InvalidParameter: if the app_id is not a finite number.
Returns:
str: Dashboard URL for the specific application.
Examples:
>>> balena.models.application.get_dashboard_url('1476418')
https://dashboard.balena-cloud.com/apps/1476418
Function: get_target_release_hash(app_id)
Get the hash of the current release for a specific application.
Args:
app_id (str): application id.
Returns:
str: The release hash of the current release.
Examples:
>>> balena.models.application.get_target_release_hash('5685')
Function: get_with_device_service_details(name, expand_release)
Get a single application along with its associated services' essential details.
Args:
name (str): application name.
expand_release (Optional[bool]): Set this to True then the commit of service details will be included.
Returns:
dict: application info.
Raises:
ApplicationNotFound: if application couldn't be found.
AmbiguousApplication: when more than one application is returned.
Examples:
>>> balena.models.application.get('test-app')
{
"depends_on__application": None,
"should_track_latest_release": True,
"app_name": "test-app",
"application_type": {
"__deferred": {"uri": "/resin/application_type(5)"},
"__id": 5,
},
"__metadata": {"type": "", "uri": "/resin/application(1252573)"},
"is_accessible_by_support_until__date": None,
"actor": 3259381,
"slug": "nghiant27101/test-app",
"owns__device": [
{
"os_variant": "prod",
"__metadata": {"type": "", "uri": "/resin/device(1460194)"},
"is_managed_by__service_instance": {
"__deferred": {"uri": "/resin/service_instance(117953)"},
"__id": 117953,
},
"should_be_running__release": None,
"belongs_to__user": {
"__deferred": {"uri": "/resin/user(5227)"},
"__id": 5227,
},
"is_web_accessible": False,
"device_type": "raspberrypi3",
"belongs_to__application": {
"__deferred": {"uri": "/resin/application(1252573)"},
"__id": 1252573,
},
"id": 1460194,
"is_locked_until__date": None,
"logs_channel": None,
"uuid": "b6070f4fea5edf808b576123157fe5ec",
"is_managed_by__device": None,
"should_be_managed_by__supervisor_release": None,
"actor": 3505229,
"note": None,
"os_version": "balenaOS 2.29.2+rev2",
"longitude": "105.8516",
"last_connectivity_event": "2019-05-06T07:30:20.230Z",
"is_on__commit": "ddf95bef72a981f826bf5303df11f318dbdbff23",
"gateway_download": [],
"location": "Hanoi, Hanoi, Vietnam",
"status": "Idle",
"public_address": "14.162.159.155",
"is_connected_to_vpn": False,
"custom_latitude": "",
"is_active": True,
"provisioning_state": "",
"latitude": "21.0313",
"custom_longitude": "",
"is_online": False,
"supervisor_version": "9.0.1",
"ip_address": "192.168.100.20",
"provisioning_progress": None,
"is_accessible_by_support_until__date": None,
"created_at": "2019-01-09T11:41:19.336Z",
"download_progress": None,
"last_vpn_event": "2019-05-06T07:30:20.230Z",
"device_name": "spring-morning",
"image_install": [
{
"status": "Running",
"__metadata": {"type": "", "uri": "/resin/image_install(34691843)"},
"image": [
{
"is_a_build_of__service": [
{
"service_name": "main",
"__metadata": {
"type": "",
"uri": "/resin/service(92238)",
},
"id": 92238,
}
],
"__metadata": {"type": "", "uri": "/resin/image(1117181)"},
"id": 1117181,
}
],
"download_progress": None,
"install_date": "2019-04-29T10:24:23.476Z",
"id": 34691843,
}
],
"local_id": None,
"vpn_address": None,
},
{
"os_variant": "prod",
"__metadata": {"type": "", "uri": "/resin/device(1308755)"},
"is_managed_by__service_instance": {
"__deferred": {"uri": "/resin/service_instance(2205)"},
"__id": 2205,
},
"should_be_running__release": None,
"belongs_to__user": {
"__deferred": {"uri": "/resin/user(5227)"},
"__id": 5227,
},
"is_web_accessible": False,
"device_type": "raspberrypi3",
"belongs_to__application": {
"__deferred": {"uri": "/resin/application(1252573)"},
"__id": 1252573,
},
"id": 1308755,
"is_locked_until__date": None,
"logs_channel": None,
"uuid": "531e5cc893b7df1e1118121059d93eee",
"is_managed_by__device": None,
"should_be_managed_by__supervisor_release": None,
"actor": 3259425,
"note": None,
"os_version": "Resin OS 2.15.1+rev1",
"longitude": "105.85",
"last_connectivity_event": "2018-09-27T14:48:53.034Z",
"is_on__commit": "19ab64483292f0a52989d0ce15ee3d21348dbfce",
"gateway_download": [],
"location": "Hanoi, Hanoi, Vietnam",
"status": "Idle",
"public_address": "14.231.247.155",
"is_connected_to_vpn": False,
"custom_latitude": "",
"is_active": True,
"provisioning_state": "",
"latitude": "21.0333",
"custom_longitude": "",
"is_online": False,
"supervisor_version": "7.16.6",
"ip_address": "192.168.0.102",
"provisioning_progress": None,
"is_accessible_by_support_until__date": None,
"created_at": "2018-09-12T04:30:13.549Z",
"download_progress": None,
"last_vpn_event": "2018-09-27T14:48:53.034Z",
"device_name": "nameless-resonance",
"image_install": [
{
"status": "Running",
"__metadata": {"type": "", "uri": "/resin/image_install(33844685)"},
"image": [
{
"is_a_build_of__service": [
{
"service_name": "main",
"__metadata": {
"type": "",
"uri": "/resin/service(92238)",
},
"id": 92238,
}
],
"__metadata": {"type": "", "uri": "/resin/image(513014)"},
"id": 513014,
}
],
"download_progress": None,
"install_date": "2018-09-27T13:53:04.748Z",
"id": 33844685,
}
],
"local_id": None,
"vpn_address": None,
},
],
"user": {"__deferred": {"uri": "/resin/user(5227)"}, "__id": 5227},
"device_type": "raspberrypi3",
"commit": "ddf95bef72a981f826bf5303df11f318dbdbff23",
"id": 1252573,
}
Function: grant_support_access(app_id, expiry_timestamp)
Grant support access to an application until a specified time.
Args:
app_id (str): application id.
expiry_timestamp (int): a timestamp in ms for when the support access will expire.
Returns:
OK/error.
Examples:
>> > balena.models.application.grant_support_access('5685', 1511974999000)
'OK'
Function: has(name)
Check if an application exists.
Args:
name (str): application name.
Returns:
bool: True if application exists, False otherwise.
Examples:
>>> balena.models.application.has('foo')
True
Function: has_any()
Check if the user has any applications.
Returns:
bool: True if user has any applications, False otherwise.
Examples:
>>> balena.models.application.has_any()
True
Function: is_tracking_latest_release(app_id)
Get whether the application is up to date and is tracking the latest release for updates.
Args:
app_id (str): application id.
Returns:
bool: is tracking the latest release.
Examples:
>> > balena.models.application.is_tracking_latest_release('5685')
True
Function: remove(name)
Remove application. This function only works if you log in using credentials or Auth Token.
Args:
name (str): application name.
Examples:
>>> balena.models.application.remove('Edison')
'OK'
Function: rename(app_id, new_name)
Rename application. This function only works if you log in using credentials or Auth Token.
Args:
app_id (int): application id.
new_name (str): new application name.
Examples:
>>> balena.models.application.rename(1681618, 'py-test-app')
'OK'
Function: restart(name)
Restart application. This function only works if you log in using credentials or Auth Token.
Args:
name (str): application name.
Raises:
ApplicationNotFound: if application couldn't be found.
Examples:
>>> balena.models.application.restart('RPI1')
'OK'
Function: revoke_support_access(app_id)
Revoke support access to an application.
Args:
app_id (str): application id.
Returns:
OK/error.
Examples:
>> > balena.models.application.revoke_support_access('5685')
'OK'
Function: set_to_release(app_id, full_release_hash)
Set an application to a specific commit.
Args:
app_id (str): application id.
full_release_hash (str) : full_release_hash.
Returns:
OK/error.
Examples:
>> > balena.models.application.set_to_release('5685', '7dba4e0c461215374edad74a5b78f470b894b5b7')
'OK'
Function: track_latest_release(app_id)
Configure a specific application to track the latest available release.
Args:
app_id (str): application id.
Examples:
>>> balena.models.application.track_latest_release('5685')
Function: will_track_new_releases(app_id)
Get whether the application is configured to receive updates whenever a new release is available.
Args:
app_id (str): application id.
Returns:
bool: is tracking the latest release.
Examples:
>> > balena.models.application.will_track_new_releases('5685')
True
ApplicationInvite
This class implements application invite model for balena python SDK.
Function: accept(invite_token)
Accepts an invite.
Args:
invite_token (str): invitationToken - invite token.
Function: create(app_id, invitee, role_name, message)
Creates a new invite for an application.
Args:
app_id (int): application id.
invitee (str): the email of the invitee.
role_name (Optional[str]): the role name to be granted to the invitee.
message (Optional[str]): the message to send along with the invite.
Returns:
dict: application invite.
Examples:
>>> balena.models.application.invite.create(1681618, '[email protected]', 'developer', 'Test invite')
{
"id": 5860,
"message": "Test invite",
"invitee": {"__id": 2965, "__deferred": {"uri": "/resin/invitee(@id)[email protected]=2965"}},
"is_created_by__user": {
"__id": 5227,
"__deferred": {"uri": "/resin/user(@id)[email protected]=5227"},
},
"is_invited_to__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {"uri": "/resin/invitee__is_invited_to__application(@id)[email protected]=5860"},
}
Function: get_all()
Get all invites.
Returns:
list: list contains info of invites.
Examples:
>>> balena.models.application.invite.get_all()
[
{
"id": 5860,
"message": "Test invite",
"invitee": {
"__id": 2965,
"__deferred": {"uri": "/resin/invitee(@id)[email protected]=2965"},
},
"is_created_by__user": {
"__id": 5227,
"__deferred": {"uri": "/resin/user(@id)[email protected]=5227"},
},
"is_invited_to__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {
"uri": "/resin/invitee__is_invited_to__application(@id)[email protected]=5860"
},
}
]
Function: get_all_by_application(app_id)
Get all invites by application.
Args:
app_id (int): application id.
Returns:
list: list contains info of invites.
Examples:
>>> balena.models.application.invite.get_all_by_application(1681618)
[
{
"id": 5860,
"message": "Test invite",
"invitee": {
"__id": 2965,
"__deferred": {"uri": "/resin/invitee(@id)[email protected]=2965"},
},
"is_created_by__user": {
"__id": 5227,
"__deferred": {"uri": "/resin/user(@id)[email protected]=5227"},
},
"is_invited_to__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {
"uri": "/resin/invitee__is_invited_to__application(@id)[email protected]=5860"
},
}
]
Function: revoke(invite_id)
Revoke an invite.
Args:
invite_id (int): application invite id.
Examples:
>>> balena.models.application.invite.revoke(5860)
'OK'
ApplicationMembership
This class implements application membership model for balena python SDK.
Function: change_role(membership_id, role_name)
Changes the role of an application member.
Args:
membership_id (int): the id of the membership that will be changed.
role_name (str): the role name to be granted to the membership.
Examples:
>>> balena.models.application.membership.change_role(55074, 'observer')
'OK'
Function: create(app_id, user_name, role_name)
Creates a new membership for an application.
Args:
app_id (int): application id.
user_name (str): the username of the balena user that will become a member.
role_name (Optional[str]): the role name to be granted to the membership.
Returns:
dict: application invite.
Examples:
>>> balena.models.application.membership.create(1681618, 'nghiant2710')
{
"is_member_of__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {"uri": "/resin/user__is_member_of__application(@id)[email protected]=55074"},
"id": 55074,
"user": {"__id": 189, "__deferred": {"uri": "/resin/user(@id)[email protected]=189"}},
}
Function: get(membership_id)
Get a single application membership.
Args:
membership_id (int): application membership id.
Returns:
dict: application membership.
Examples:
>>> balena.models.application.membership.get(55074)
{
"is_member_of__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {"uri": "/resin/user__is_member_of__application(@id)[email protected]=55074"},
"id": 55074,
"user": {"__id": 189, "__deferred": {"uri": "/resin/user(@id)[email protected]=189"}},
}
Function: get_all()
Get all application memberships.
Returns:
list: list contains info of application memberships.
Examples:
>>> balena.models.application.membership.get_all()
[
{
"is_member_of__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {"uri": "/resin/user__is_member_of__application(@id)[email protected]=55074"},
"id": 55074,
"user": {"__id": 189, "__deferred": {"uri": "/resin/user(@id)[email protected]=189"}},
}
]
Function: get_all_by_application(app_id)
Get all memberships by application.
Args:
app_id (int): application id.
Returns:
list: list contains info of application memberships.
Examples:
>>> balena.models.application.membership.get_all_by_application(1681618)
[
{
"is_member_of__application": {
"__id": 1681618,
"__deferred": {"uri": "/resin/application(@id)[email protected]=1681618"},
},
"application_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/application_membership_role(@id)[email protected]=2"},
},
"__metadata": {"uri": "/resin/user__is_member_of__application(@id)[email protected]=55074"},
"id": 55074,
"user": {"__id": 189, "__deferred": {"uri": "/resin/user(@id)[email protected]=189"}},
}
]
Function: remove(membership_id)
Remove a membership.
Args:
membership_id (int): application membership id.
ApiKey
This class implements user API key model for balena python SDK.
Function: create_api_key(name, description)
This function registers a new api key for the current user with the name given.
Args:
name (str): user API key name.
description (Optional[str]): API key description.
Returns:
str: user API key.
Examples:
>>> balena.models.api_key.create_api_key('myApiKey')
'3YHD9DVPLe6LbjEgQb7FEFXYdtPEMkV9'
Function: get_all()
This function gets all API keys.
Returns:
list: user API key.
Examples:
>>> balena.models.api_key.get_all()
[
{
"description": None,
"created_at": "2018-04-06T03:53:34.189Z",
"__metadata": {"type": "", "uri": "/balena/api_key(1296047)"},
"is_of__actor": {
"__deferred": {"uri": "/balena/actor(2454095)"},
"__id": 2454095,
},
"id": 1296047,
"name": "myApiKey",
}
]
Function: get_all_named_user_api_keys()
Get all named user API keys of the current user.
Examples:
>>> balena.models.api_key.get_all_named_user_api_keys()
[
{
"id": 2452013,
"created_at": "2019-11-12T09:48:42.437Z",
"is_of__actor": {
"__id": 113809,
"__deferred": {"uri": "/resin/actor(@id)[email protected]=113809"},
},
"name": "test",
"description": None,
"__metadata": {"uri": "/resin/api_key(@id)[email protected]=2452013"},
}
]
Function: get_device_api_keys_by_device(device_uuid)
Get all API keys for a device.
Args:
device_uuid (str): device uuid.
Examples:
>>> balena.models.api_key.get_device_api_keys_by_device('44cc9d1861b9f992808c506276e5d31d')
[
{
"id": 3111484,
"created_at": "2020-06-25T04:33:33.069Z",
"is_of__actor": {
"__id": 6444456,
"__deferred": {"uri": "/resin/actor(@id)[email protected]=6444456"},
},
"name": None,
"description": None,
"__metadata": {"uri": "/resin/api_key(@id)[email protected]=3111484"},
}
]
Function: get_provisioning_api_keys_by_application(app_id)
Get all provisioning API keys for an application.
Args:
app_id (str): application id.
Examples:
>>> balena.models.api_key.get_provisioning_api_keys_by_application(1296047)
[
{
"id": 5492033,
"created_at": "2021-12-29T03:38:04.470Z",
"is_of__actor": {
"__id": 6444453,
"__deferred": {"uri": "/resin/actor(@id)[email protected]=6444453"},
},
"name": None,
"description": None,
"__metadata": {"uri": "/resin/api_key(@id)[email protected]=5492033"},
},
{
"id": 5492044,
"created_at": "2021-12-29T03:41:04.441Z",
"is_of__actor": {
"__id": 6444453,
"__deferred": {"uri": "/resin/actor(@id)[email protected]=6444453"},
},
"name": "key p1",
"description": "key desc",
"__metadata": {"uri": "/resin/api_key(@id)[email protected]=5492044"},
},
{
"id": 3111481,
"created_at": "2020-06-25T04:24:53.621Z",
"is_of__actor": {
"__id": 6444453,
"__deferred": {"uri": "/resin/actor(@id)[email protected]=6444453"},
},
"name": None,
"description": None,
"__metadata": {"uri": "/resin/api_key(@id)[email protected]=3111481"},
},
]
Function: revoke(id)
This function revokes an API key.
Args:
id (str): API key id.
Examples:
>>> balena.models.api_key.revoke(1296047)
OK
Function: update(id, api_key_info)
This function updates details of an API key.
Args:
id (str): API key id.
api_key_info: new API key info.
name (str): new API key name.
description (Optional[str]): new API key description.
Examples:
>>> balena.models.api_key.update(1296047, {'name':'new name')
OK
Config
This class implements configuration model for balena python SDK.
Attributes:
_config (dict): caching configuration.
Function: get_all()
Get all configuration.
Returns:
dict: configuration information.
Examples:
>>> balena.models.config.get_all()
{ all configuration details }
ConfigVariable
This class is a wrapper for config variable models.
ApplicationConfigVariable
This class implements application config variable model for balena python SDK.
Function: create(app_id, config_var_name, value)
Create an application config variable.
Args:
app_id (str): application id.
config_var_name (str): application config variable name.
value (str): application config variable value.
Returns:
dict: new application config variable info.
Examples:
>>> balena.models.config_variable.application_config_variable.create('1005160', 'BALENA_TEST_APP_CONFIG_VAR', 'test value')
{
"id": 117738,
"application": {
"__deferred": {"uri": "/balena/application(1005160)"},
"__id": 1005160,
},
"name": "BALENA_TEST_APP_CONFIG_VAR",
"value": "test value",
"__metadata": {"uri": "/balena/application_config_variable(117738)", "type": ""},
}
Function: get_all(app_id)
Get all application config variables belong to an application.
Args:
app_id (str): application id.
Returns:
list: application config variables.
Examples:
>>> balena.models.config_variable.application_config_variable.get_all('1005160')
[
{
"application": {
"__deferred": {"uri": "/balena/application(1005160)"},
"__id": 1005160,
},
"__metadata": {
"type": "",
"uri": "/balena/application_config_variable(116965)",
},
"id": 116965,
"value": "false",
"name": "BALENA_SUPERVISOR_NATIVE_LOGGER",
}
]
Function: remove(var_id)
Remove a application config environment variable.
Args:
var_id (str): application config environment variable id.
Examples:
>>> balena.models.config_variable.application_config_variable.remove('117738')
'OK'
Function: update(var_id, value)
Update an application config variable.
Args:
var_id (str): application config variable id.
value (str): new application config variable value.
Examples:
>>> balena.models.config_variable.application_config_variable.update('117738', 'new test value')
'OK'
DeviceConfigVariable
This class implements device config variable model for balena python SDK.
Function: create(uuid, config_var_name, value)
Create a device config variable.
Args:
uuid (str): device uuid.
config_var_name (str): device config variable name.
value (str): device config variable value.
Returns:
dict: new device config variable info.
Examples:
>>> balena.models.config_variable.device_config_variable.create('f14a73b3a762396f7bfeacf5d530c316aa8cfeff307bea93422f71a106c344','BALENA_TEST_DEVICE_CONFIG_VAR','test value')
{
"device": {"__deferred": {"uri": "/balena/device(1083716)"}, "__id": 1083716},
"__metadata": {"type": "", "uri": "/balena/device_config_variable(163985)"},
"id": 163985,
"value": "test value",
"name": "BALENA_TEST_DEVICE_CONFIG_VAR",
}
Function: get_all(uuid)
Get all device config variables belong to a device.
Args:
uuid (str): device uuid.
Returns:
list: device config variables.
Examples:
>>> balena.models.config_variable.device_config_variable.get_all('f5213eac0d63ac47721b037a7406d306')
[
{
"id": 1234567,
"device": {"__id": 7654321},
"value": "test",
"name": "MODE",
},
{
"id": 7890123,
"device": {"__id": 3210987},
"value": "true",
"name": "IS_TESTING",
},
]
Function: get_all_by_application(app_id)
Get all device config variables by application.
Args:
app_id (int): application id.
Returns:
list: device config variables.
Examples:
>>> balena.models.config_variable.device_config_variable.get_all_by_application(1043050)
[
{
"id": 1234567,
"device": {"__id": 7654321},
"value": "test_app",
"name": "MODE_APPLICATION",
},
{
"id": 7890123,
"device": {"__id": 3210987},
"value": "true",
"name": "IS_TESTING",
},
]
Function: remove(var_id)
Remove a device config environment variable.
Args:
var_id (str): device config environment variable id.
Examples:
>>> balena.models.config_variable.device_config_variable.remove('132715')
'OK'
Function: update(var_id, value)
Update a device config variable.
Args:
var_id (str): device config variable id.
value (str): new device config variable value.
Examples:
>>> balena.models.config_variable.device_config_variable.update('132715', 'new test value')
'OK'
Device
This class implements device model for balena python SDK.
Due to API changes, the returned Device object schema has changed. Here are the formats of the old and new returned objects.
The old returned object's properties: __metadata, actor, application, build, commit, created_at, custom_latitude, custom_longitude, device, device_type, download_progress, id, ip_address, is_connected_to_vpn, is_online, is_web_accessible, last_connectivity_event, last_vpn_event, latitude, local_id, location, lock_expiry_date, logs_channel, longitude, name, note, os_variant, os_version, provisioning_progress, provisioning_state, public_address, service_instance, status, supervisor_release, supervisor_version, support_expiry_date, user, uuid, vpn_address
.
The new returned object's properties (since python SDK v2.0.0): __metadata, actor, belongs_to__application, belongs_to__user, created_at, custom_latitude, custom_longitude, device_type, download_progress, id, ip_address, is_accessible_by_support_until__date, is_connected_to_vpn, is_locked_until__date, is_managed_by__device, is_managed_by__service_instance, is_on__commit, is_online, is_web_accessible, last_connectivity_event, last_vpn_event, latitude, local_id, location, logs_channel, longitude, name, note, os_variant, os_version, provisioning_progress, provisioning_state, public_address, should_be_managed_by__supervisor_release, should_be_running__build, status, supervisor_version, uuid, vpn_address
.
Function: deactivate(uuid)
Deactivate a device.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.deactivate('44cc9d1861b9f992808c506276e5d31c')
Function: disable_device_url(uuid)
Disable device url for a device.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.disable_device_url('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
'OK'
Function: disable_local_mode(uuid)
Disable local mode.
Args:
uuid (str): device uuid.
Returns:
None.
Examples:
>>> balena.models.device.disable_local_mode('b6070f4fea5edf808b576123157fe5ec')
Function: disable_lock_override(uuid)
Disable lock override.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Function: enable_device_url(uuid)
Enable device url for a device.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
# Check if device url enabled.
>>> balena.models.device.has_device_url('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
False
# Enable device url.
>>> balena.models.device.enable_device_url('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
'OK'
# Check device url again.
>>> balena.models.device.has_device_url('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
True
Function: enable_local_mode(uuid)
Enable local mode.
Args:
uuid (str): device uuid.
Returns:
None.
Examples:
>>> balena.models.device.enable_local_mode('b6070f4fea5edf808b576123157fe5ec')
Raises:
LocalModeError: if local mode can't be enabled.
Function: enable_lock_override(uuid)
Enable lock override.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Function: generate_device_key(uuid, key_name, key_description, expiry_date)
Generate a device key.
Args:
uuid (str): device uuid.
key_name (Optional[str]): device key name.
key_description (Optional[str]): description for device key.
expiry_date (Optional[str]): expiry date for device key, for example: `2030-01-01T00:00:00Z`.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.generate_device_key('df09262c283b1dc1462d0e82caa7a88e52588b8c5d7475dd22210edec1c50a')
2UrtMWeLqYXfTznZo1xNuZQXmEE6cOZk
Function: generate_uuid()
Generate a random device UUID.
Returns:
str: a generated UUID.
Examples:
>>> balena.models.device.generate_uuid()
'19dcb86aa288c66ffbd261c7bcd46117c4c25ec655107d7302aef88b99d14c'
Function: get(uuid)
Get a single device by device uuid.
Args:
uuid (str): device uuid.
Returns:
dict: device info.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.get('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
{
"id": 8570370,
"belongs_to__application": {
"__id": 1280664
},
"belongs_to__user": None,
"actor": 12049318,
"should_be_running__release": None,
"device_name": "otaviojacobi",
"is_of__device_type": {
"__id": 145
},
"uuid": "8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143",
"is_running__release": {
"__id": 2556338
},
"note": None,
"local_id": None,
"status": "Idle",
"is_online": True,
"last_connectivity_event": "2023-04-28T05:39:48.890Z",
"is_connected_to_vpn": True,
"last_vpn_event": "2023-04-28T05:39:48.890Z",
"ip_address": "192.168.0.49 2804:14d:4cdc:1b77::1000 2804:14d:4cdc:1b77:3904:981a:95c9:cd74",
"mac_address": "1C:69:7A:AB:AB:76 F4:4E:E3:E1:F0:2D",
"vpn_address": None,
"public_address": "179.152.22.41",
"os_version": "balenaOS 2.113.4",
"os_variant": "dev",
"supervisor_version": "14.9.4",
"should_be_managed_by__supervisor_release": {
"__id": 2514540
},
"should_be_operated_by__release": {
"__id": 2182319
},
"is_managed_by__service_instance": {
"__id": 128359
},
"provisioning_progress": None,
"provisioning_state": "",
"download_progress": None,
"is_web_accessible": True,
"longitude": "51.2658",
"latitude": "-75.1169",
"location": "Secret Location, Nowhere, CA",
"custom_longitude": "",
"custom_latitude": "",
"is_locked_until__date": None,
"is_accessible_by_support_until__date": None,
"created_at": "2022-09-30T11:40:11.901Z",
"modified_at": "2023-04-28T12:46:03.584Z",
"is_active": True,
"api_heartbeat_state": "online",
"memory_usage": 5303,
"memory_total": 15624,
"storage_block_device": "/dev/nvme0n1p6",
"storage_usage": 162869,
"storage_total": 236417,
"cpu_temp": 58,
"cpu_usage": 13,
"cpu_id": "gepa136000r6",
"is_undervolted": false,
"logs_channel": null
}
Function: get_all()
Get all devices.
Returns:
list: list contains info of devices.
Examples:
>>> balena.models.device.get_all()
[
{
"__metadata": {"type": "", "uri": "/ewa/device(122950)"},
"last_connectivity_event": "1970-01-01T00:00:00.000Z",
"is_web_accessible": False,
"device_type": "raspberry-pi",
"id": 122950,
"logs_channel": None,
"uuid": "8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143",
"application": {"__deferred": {"uri": "/ewa/application(9020)"}, "__id": 9020},
"note": None,
"os_version": None,
"location": "",
"latitude": "",
"status": None,
"public_address": "",
"provisioning_state": None,
"user": {"__deferred": {"uri": "/ewa/user(5397)"}, "__id": 5397},
"is_online": False,
"supervisor_version": None,
"ip_address": None,
"vpn_address": None,
"name": "floral-mountain",
"download_progress": None,
"longitude": "",
"commit": None,
"provisioning_progress": None,
"supervisor_release": None,
}
]
Function: get_all_by_application(name)
Get devices by application name.
Args:
name (str): application name.
Returns:
list: list contains info of devices.
Examples:
>>> balena.models.device.get_all_by_application('RPI1')
[
{
"__metadata": {"type": "", "uri": "/ewa/device(122950)"},
"last_connectivity_event": "1970-01-01T00:00:00.000Z",
"is_web_accessible": False,
"device_type": "raspberry-pi",
"id": 122950,
"logs_channel": None,
"uuid": "8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143",
"application": {"__deferred": {"uri": "/ewa/application(9020)"}, "__id": 9020},
"note": None,
"os_version": None,
"location": "",
"latitude": "",
"status": None,
"public_address": "",
"provisioning_state": None,
"user": {"__deferred": {"uri": "/ewa/user(5397)"}, "__id": 5397},
"is_online": False,
"supervisor_version": None,
"ip_address": None,
"vpn_address": None,
"name": "floral-mountain",
"download_progress": None,
"longitude": "",
"commit": None,
"provisioning_progress": None,
"supervisor_release": None,
}
]
Function: get_all_by_application_id(appid)
Get devices by application name.
Args:
appid (str): application id.
Returns:
list: list contains info of devices.
Examples:
>>> balena.models.device.get_all_by_application_id(1234)
[
{
"__metadata": {"type": "", "uri": "/ewa/device(122950)"},
"last_connectivity_event": "1970-01-01T00:00:00.000Z",
"is_web_accessible": False,
"device_type": "raspberry-pi",
"id": 122950,
"logs_channel": None,
"uuid": "8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143",
"application": {"__deferred": {"uri": "/ewa/application(9020)"}, "__id": 9020},
"note": None,
"os_version": None,
"location": "",
"latitude": "",
"status": None,
"public_address": "",
"provisioning_state": None,
"user": {"__deferred": {"uri": "/ewa/user(5397)"}, "__id": 5397},
"is_online": False,
"supervisor_version": None,
"ip_address": None,
"vpn_address": None,
"name": "floral-mountain",
"download_progress": None,
"longitude": "",
"commit": None,
"provisioning_progress": None,
"supervisor_release": None,
}
]
Function: get_application_name(uuid)
Get application name by device uuid.
Args:
uuid (str): device uuid.
Returns:
str: application name.
Raises:
DeviceNotFound: if device couldn't be found.
Function: get_by_name(name)
Get devices by device name.
Args:
name (str): device name.
Returns:
list: list contains info of devices.
Examples:
>>> balena.models.device.get_by_name('floral-mountain')
[
{
"__metadata": {"type": "", "uri": "/ewa/device(122950)"},
"last_connectivity_event": "1970-01-01T00:00:00.000Z",
"is_web_accessible": False,
"device_type": "raspberry-pi",
"id": 122950,
"logs_channel": None,
"uuid": "8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143",
"application": {"__deferred": {"uri": "/ewa/application(9020)"}, "__id": 9020},
"note": None,
"os_version": None,
"location": "",
"latitude": "",
"status": None,
"public_address": "",
"provisioning_state": None,
"user": {"__deferred": {"uri": "/ewa/user(5397)"}, "__id": 5397},
"is_online": False,
"supervisor_version": None,
"ip_address": None,
"vpn_address": None,
"name": "floral-mountain",
"download_progress": None,
"longitude": "",
"commit": None,
"provisioning_progress": None,
"supervisor_release": None,
}
]
Function: get_dashboard_url(uuid)
Get balena Dashboard URL for a specific device.
Args:
uuid (str): device uuid.
Examples:
>>> balena.models.device.get_dashboard_url('19619a6317072b65a240b451f45f855d')
https://dashboard.balena-cloud.com/devices/19619a6317072b65a240b451f45f855d/summary
Function: get_device_url(uuid)
Get a device url for a device.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.get_device_url('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
'https://8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143.balenadevice.io'
Function: get_local_ip_address(uuid)
Get the local IP addresses of a device.
Args:
uuid (str): device uuid.
Returns:
list: IP addresses of a device.
Raises:
DeviceNotFound: if device couldn't be found.
DeviceOffline: if device is offline.
Function: get_local_mode_support(uuid)
Returns whether local mode is supported and a message describing the reason why local mode is not supported.
Args:
uuid (str): device uuid.
Returns:
dict: local mode support information ({'supported': True/False, 'message': '...'}).
Examples:
>>> balena.models.device.get_local_mode_support('b6070f4fea5edf808b576123157fe5ec')
{'message': 'Local mode is only supported on development OS versions', 'supported': False}
Function: get_mac_address(uuid)
Get the MAC addresses of a device.
Args:
uuid (str): device uuid.
Returns:
list: MAC addresses of a device.
Raises:
DeviceNotFound: if device couldn't be found.
Function: get_manifest_by_application(app_name)
Get a device manifest by application name.
Args:
app_name (str): application name.
Returns:
dict: dictionary contains device manifest.
Function: get_manifest_by_slug(slug)
Get a device manifest by slug.
Args:
slug (str): device slug name.
Returns:
dict: dictionary contains device manifest.
Raises:
InvalidDeviceType: if device slug name is not supported.
Function: get_metrics(uuid)
Get the metrics related information for a device.
Args:
uuid (str): device uuid.
Returns:
dict: metrics of the device.
Raises:
DeviceNotFound: if device couldn't be found.
Function: get_name(uuid)
Get device name by device uuid.
Args:
uuid (str): device uuid.
Returns:
str: device name.
Raises:
DeviceNotFound: if device couldn't be found.
Function: get_os_update_status(uuid)
Get the OS update status of a device.
Args:
uuid (str): device uuid.
Returns:
dict: action response.
Examples:
>>> balena.models.device.get_os_update_status('b6070f4fea5edf808b576123157fe5ec')
{'status': 'idle', 'action': 'resinhup', 'lastRun': None}
Function: get_status(uuid)
Get the status of a device.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Returns:
str: status of a device. List of available statuses: Idle, Configuring, Updating, Offline, Inactive and Post Provisioning.
Examples:
>>> balena.models.device.get_status('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
'Offline'
Function: get_supervisor_state(uuid)
Get the supervisor state on a device
Args:
uuid (str): device uuid.
Returns:
dict: supervisor state.
Examples:
>>> balena.models.device.get_supervisor_state('b6070f4fea5edf808b576123157fe5ec')
{
"status": "Idle",
"update_failed": False,
"os_version": "balenaOS 2.29.0+rev1",
"download_progress": None,
"update_pending": False,
"api_port": "48484",
"commit": "d26dd8a68a47c40daaa1d32e03c96d934f37c53b",
"update_downloaded": False,
"supervisor_version": "9.0.1",
"ip_address": "192.168.100.16",
}
Function: get_supervisor_target_state(uuid)
Get the supervisor target state on a device
Args:
uuid (str): device uuid.
Returns:
dict: supervisor target state.
Examples:
>>> balena.models.device.get_supervisor_target_state('b6070f4fea5edf808b576123157fe5ec')
{
"local": {
"name": "holy-darkness",
"config": {
"RESIN_SUPERVISOR_NATIVE_LOGGER": "true",
"RESIN_SUPERVISOR_POLL_INTERVAL": "900000",
},
"apps": {
"1398898": {
"name": "test-nuc",
"commit": "f9d139b80a7df94f90d7b9098b1353b14ca31b85",
"releaseId": 850293,
"services": {
"229592": {
"imageId": 1016025,
"serviceName": "main",
"image": "registry2.balena-cloud.com/v2/[email protected]:f489c30335a0036ecf1606df3150907b32ea39d73ec6de825a549385022e3e22",
"running": True,
"environment": {},
"labels": {
"io.resin.features.dbus": "1",
"io.resin.features.firmware": "1",
"io.resin.features.kernel-modules": "1",
"io.resin.features.resin-api": "1",
"io.resin.features.supervisor-api": "1",
},
"privileged": True,
"tty": True,
"restart": "always",
"network_mode": "host",
"volumes": ["resin-data:/data"],
}
},
"volumes": {"resin-data": {}},
"networks": {},
}
},
},
"dependent": {"apps": {}, "devices": {}},
}
Function: get_with_service_details(uuid, expand_release)
Get a single device along with its associated services' essential details.
Args:
uuid (str): device uuid.
expand_release (Optional[bool]): Set this to False and the commit of service details will not be included.
Returns:
dict: device info with associated services details.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.get_with_service_details('0fcd753af396247e035de53b4e43eec3')
{
"os_variant": "prod",
"__metadata": {"type": "", "uri": "/balena/device(1136312)"},
"is_managed_by__service_instance": {
"__deferred": {"uri": "/balena/service_instance(182)"},
"__id": 182,
},
"should_be_running__release": None,
"belongs_to__user": {"__deferred": {"uri": "/balena/user(32986)"}, "__id": 32986},
"is_web_accessible": False,
"device_type": "raspberrypi3",
"belongs_to__application": {
"__deferred": {"uri": "/balena/application(1116729)"},
"__id": 1116729,
},
"id": 1136312,
"is_locked_until__date": None,
"logs_channel": "1da2f8db7c5edbf268ba6c34d91974de8e910eef0033a1172386ad27807552",
"uuid": "0fcd753af396247e035de53b4e43eec3",
"is_managed_by__device": None,
"should_be_managed_by__supervisor_release": None,
"is_accessible_by_support_until__date": None,
"actor": 2895243,
"note": None,
"os_version": "Balena OS 2.12.7+rev1",
"longitude": "105.85",
"last_connectivity_event": "2018-05-27T05:43:54.027Z",
"is_on__commit": "01defe8bbd1b5b832b32c6e1d35890317671cbb5",
"location": "Hanoi, Thanh Pho Ha Noi, Vietnam",
"status": "Idle",
"public_address": "14.231.243.124",
"is_connected_to_vpn": False,
"custom_latitude": "",
"is_active": True,
"provisioning_state": "",
"latitude": "21.0333",
"custom_longitude": "",
"current_services": {
"frontend": [
{
"status": "Running",
"download_progress": None,
"__metadata": {"type": "", "uri": "/balena/image_install(8952657)"},
"install_date": "2018-05-25T19:00:12.989Z",
"image_id": 296863,
"commit": "01defe8bbd1b5b832b32c6e1d35890317671cbb5",
"service_id": 52327,
"id": 8952657,
}
],
"data": [
{
"status": "Running",
"download_progress": None,
"__metadata": {"type": "", "uri": "/balena/image_install(8952656)"},
"install_date": "2018-05-25T19:00:12.989Z",
"image_id": 296864,
"commit": "01defe8bbd1b5b832b32c6e1d35890317671cbb5",
"service_id": 52329,
"id": 8952656,
}
],
"proxy": [
{
"status": "Running",
"download_progress": None,
"__metadata": {"type": "", "uri": "/balena/image_install(8952655)"},
"install_date": "2018-05-25T19:00:12.985Z",
"image_id": 296862,
"commit": "01defe8bbd1b5b832b32c6e1d35890317671cbb5",
"service_id": 52328,
"id": 8952655,
}
],
},
"is_online": False,
"supervisor_version": "7.4.3",
"ip_address": "192.168.0.102",
"provisioning_progress": None,
"owns__device_log": {
"__deferred": {"uri": "/balena/device_log(1136312)"},
"__id": 1136312,
},
"created_at": "2018-05-25T10:55:47.825Z",
"download_progress": None,
"last_vpn_event": "2018-05-27T05:43:54.027Z",
"device_name": "billowing-night",
"local_id": None,
"vpn_address": None,
"current_gateway_downloads": [],
}
Function: grant_support_access(uuid, expiry_timestamp)
Grant support access to a device until a specified time.
Args:
uuid (str): device uuid.
expiry_timestamp (int): a timestamp in ms for when the support access will expire.
Returns:
OK.
Examples:
>>> balena.models.device.grant_support_access('49b2a76b7f188c1d6f781e67c8f34adb4a7bfd2eec3f91d40b1efb75fe413d', 1511974999000)
'OK'
Function: has(uuid)
Check if a device exists.
Args:
uuid (str): device uuid.
Returns:
bool: True if device exists, False otherwise.
Function: has_device_url(uuid)
Check if a device is web accessible with device urls
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.has_device_url('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
False
Function: has_lock_override(uuid)
Check if a device has the lock override enabled.
Args:
uuid (str): device uuid.
Returns:
bool: lock override status.
Raises:
DeviceNotFound: if device couldn't be found.
Function: identify(uuid)
Identify device. This function only works if you log in using credentials or Auth Token.
Args:
uuid (str): device uuid.
Examples:
>>> balena.models.device.identify('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
'OK'
Function: is_in_local_mode(uuid)
Check if local mode is enabled on the device.
Args:
uuid (str): device uuid.
Returns:
bool: True if local mode enabled, otherwise False.
Examples:
>>> balena.models.device.is_in_local_mode('b6070f4fea5edf808b576123157fe5ec')
True
Function: is_online(uuid)
Check if a device is online.
Args:
uuid (str): device uuid.
Returns:
bool: True if the device is online, False otherwise.
Raises:
DeviceNotFound: if device couldn't be found.
Function: is_tracking_application_release(uuid)
Get whether the device is configured to track the current application release.
Args:
uuid (str): device uuid.
Returns:
bool: is tracking the current application release.
Raises:
DeviceNotFound: if device couldn't be found.
Function: move(uuid, app_name)
Move a device to another application.
Args:
uuid (str): device uuid.
app_name (str): application name.
Raises:
DeviceNotFound: if device couldn't be found.
ApplicationNotFound: if application couldn't be found.
IncompatibleApplication: if moving a device to an application with different device-type.
Examples:
>>> balena.models.device.move('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143', 'RPI1Test')
'OK'
Function: note(uuid, note)
Note a device.
Args:
uuid (str): device uuid.
note (str): device note.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.note('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143', 'test note')
'OK'
Function: register(app_id, uuid, device_type_slug)
Register a new device with a balena application. This function only works if you log in using credentials or Auth Token.
Args:
app_id (str): application id.
uuid (str): device uuid.
device_type_slug (Optional[str]): device type slug or alias.
Returns:
dict: dictionary contains device info.
Examples:
>>> device_uuid = balena.models.device.generate_uuid()
>>> balena.models.device.register('RPI1',device_uuid)
{
"id": 122950,
"application": {"__deferred": {"uri": "/ewa/application(9020)"}, "__id": 9020},
"user": {"__deferred": {"uri": "/ewa/user(5397)"}, "__id": 5397},
"name": "floral-mountain",
"device_type": "raspberry-pi",
"uuid": "8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143",
"commit": None,
"note": None,
"status": None,
"is_online": false,
"last_connectivity_event": "1970-01-01T00:00:00.000Z",
"ip_address": None,
"vpn_address": None,
"public_address": "",
"os_version": None,
"supervisor_version": None,
"supervisor_release": None,
"provisioning_progress": None,
"provisioning_state": None,
"download_progress": None,
"is_web_accessible": false,
"longitude": "",
"latitude": "",
"location": "",
"logs_channel": None,
"__metadata": {"uri": "/ewa/device(122950)", "type": ""},
}
Function: remove(uuid)
Remove a device. This function only works if you log in using credentials or Auth Token.
Args:
uuid (str): device uuid.
Function: rename(uuid, new_name)
Rename a device.
Args:
uuid (str): device uuid.
new_name (str): device new name.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.rename('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143', 'python-sdk-test-device')
'OK'
# Check device name.
>>> balena.models.device.get_name('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
u'python-sdk-test-device'
Function: restart(uuid)
Restart a user application container on device. This function only works if you log in using credentials or Auth Token.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.restart('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
'OK'
Function: revoke_support_access(uuid)
Revoke support access to a device.
Args:
uuid (str): device uuid.
Returns:
OK.
Examples:
>> > balena.models.device.revoke_support_access('49b2a76b7f188c1d6f781e67c8f34adb4a7bfd2eec3f91d40b1efb75fe413d')
'OK'
Function: set_custom_location(uuid, location)
Set a custom location for a device.
Args:
uuid (str): device uuid.
location (dict): device custom location, format: { 'latitude': <latitude>, 'longitude': <longitude> }.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> location = {
'latitude': '21.032777',
'longitude': '105.831586'
}
>>> balena.models.device.set_custom_location('df09262c283b1dc1462d0e82caa7a88e52588b8c5d7475dd22210edec1c50a',location)
OK
Function: set_supervisor_release(device_uuid, supervisor_version)
Set a specific device to run a particular supervisor release.
Args:
device_uuid (str): device uuid.
supervisor_version (str): the version of a released supervisor
Raises:
DeviceNotFound: if device couldn't be found.
ReleaseNotFound: if supervisor version couldn't be found
Examples:
>>> balena.models.device.set_supervisor_release('f55dcdd9ad2ab0c1d59c316961268a48', 'v13.0.0')
OK
Function: set_to_release(uuid, commit_id)
Set device to a specific release. Set an empty commit_id will restore rolling releases to the device.
Args:
uuid (str): device uuid.
commit_id (str) : commit id.
Returns:
OK.
Examples:
>>> balena.models.device.set_to_release('49b2a76b7f188c1d6f781e67c8f34adb4a7bfd2eec3f91d40b1efb75fe413d', '45c90004de73557ded7274d4896a6db90ea61e36')
'OK'
Function: set_to_release_by_id(uuid, release_id)
Set device to a specific release by release id (please notice that release id is not the commit hash on balena dashboard). Remove release_id will restore rolling releases to the device.
Args:
uuid (str): device uuid.
release_id (Optional[int]): release id.
Returns:
OK.
Examples:
>>> balena.models.device.set_to_release_by_id('49b2a76b7f188c1d6f781e67c8f34adb4a7bfd2eec3f91d40b1efb75fe413d', 165432)
'OK'
>>> balena.models.device.set_to_release_by_id('49b2a76b7f188c1d6f781e67c8f34adb4a7bfd2eec3f91d40b1efb75fe413d')
'OK'
Function: start_os_update(uuid, target_os_version)
Start an OS update on a device.
Args:
uuid (str): device uuid.
target_os_version (str): semver-compatible version for the target device.
Unsupported (unpublished) version will result in rejection.
The version **must** be the exact version number, a "prod" variant and greater than the one running on the device.
Returns:
dict: action response.
Raises:
DeviceNotFound: if device couldn't be found.
InvalidParameter|OsUpdateError: if target_os_version is invalid.
Examples:
>>> balena.models.device.start_os_update('b6070f4fea5edf808b576123157fe5ec', '2.29.2+rev1.prod')
>>> # or for unified OS releases
>>> balena.models.device.start_os_update('b6070f4fea5edf808b576123157fe5ec', '2.89.0+rev1')
Function: track_application_release(uuid)
Configure a specific device to track the current application release.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Function: unset_custom_location(uuid)
clear custom location for a device.
Args:
uuid (str): device uuid.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.device.unset_custom_location('df09262c283b1dc1462d0e82caa7a88e52588b8c5d7475dd22210edec1c50a')
OK
DeviceOs
This class implements device os model for balena python SDK.
Function: download(raw)
Download an OS image. This function only works if you log in using credentials or Auth Token.
Args:
raw (bool): determining function return value.
**data: os parameters keyword arguments.
version (str): the balenaOS version of the image. The SDK will try to parse version into semver-compatible version, unsupported (unpublished) version will result in rejection.
appId (str): the application ID.
network (str): the network type that the device will use, one of 'ethernet' or 'wifi'.
fileType (Optional[str]): one of '.img' or '.zip' or '.gz', defaults to '.img'.
wifiKey (Optional[str]): the key for the wifi network the device will connect to if network is wifi.
wifiSsid (Optional[str]): the ssid for the wifi network the device will connect to if network is wifi.
appUpdatePollInterval (Optional[str]): how often the OS checks for updates, in minutes.
Returns:
object:
If raw is True, urllib3.HTTPResponse object is returned.
If raw is False, original response object is returned.
Notes:
default OS image file name can be found in response headers.
Examples:
>>> data = {'appId': '1476418', 'network': 'ethernet', 'version': '2.43.0+rev1.prod'}
>>> response = balena.models.device_os.download(**data)
>>> type(response)
<class 'requests.models.Response'>
>>> response['headers']
>>> response.headers
{
"Content-Length": "134445838",
"Access-Control-Allow-Headers": "Content-Type, Authorization, Application-Record-Count, MaxDataServiceVersion, X-Requested-With, X-Balena-Client",
"content-disposition": 'attachment; filename="balena-cloud-FooBar4-raspberry-pi2-2.43.0+rev1-v10.2.2.img.zip"',
"X-Content-Type-Options": "nosniff",
"Access-Control-Max-Age": "86400",
"x-powered-by": "Express",
"Vary": "X-HTTP-Method-Override",
"x-transfer-length": "134445838",
"Connection": "keep-alive",
"Access-Control-Allow-Credentials": "true",
"Date": "Tue, 07 Jan 2020 17:40:52 GMT",
"X-Frame-Options": "DENY",
"Access-Control-Allow-Methods": "GET, PUT, POST, PATCH, DELETE, OPTIONS, HEAD",
"Content-Type": "application/zip",
"Access-Control-Allow-Origin": "*",
}
Function: download_unconfigured_image(device_type, version, login, raw)
Download an unconfigured OS image.
Args:
device_type (str): device type slug.
version (str): the balenaOS version of the image. The SDK will try to parse version into semver-compatible version, unsupported (unpublished) version will result in rejection.
login (bool): include authentication information in the request or not. Must be set to True to download private device type. Default to False.
raw (bool): determining method returned value. Default to False.
Returns:
object:
If raw is True, urllib3.HTTPResponse object is returned.
If raw is False, original response object is returned.
Notes:
default OS image file name can be found in response headers.
Examples:
>>> response = balena.models.device_os.download_unconfigured_image('raspberrypi4-64', 'latest', login=True)
>>> type(response)
<class 'requests.models.Response'>
>>> response.headers
{'Date': 'Tue, 01 Nov 2022 11:47:52 GMT', 'Content-Type': 'application/octet-stream', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'X-Frame-Options': 'DENY', 'X-Content-Type-Options': 'nosniff', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'access-control-allow-methods': 'GET, PUT, POST, PATCH, DELETE, OPTIONS, HEAD', 'access-control-allow-headers': 'Content-Type, Authorization, Application-Record-Count, MaxDataServiceVersion, X-Requested-With', 'access-control-max-age': '86400', 'content-encoding': 'gzip', 'content-disposition': 'attachment; filename="raspberrypi4-64-2.105.21-v14.2.10.img"', 'x-transfer-length': '173390850'}
Function: get_available_os_versions(device_type)
Get the supported balenaOS versions (ESR included) for the provided device type.
Args:
device_type (str): device type slug.
Returns:
list: balenaOS versions.
Function: get_config(app_id, options)
Get an application config.json.
Args:
app_id (str): application id.
options (dict): OS configuration options to use. The available options are listed below:
version (str): the OS version of the image.
network (Optional[str]): the network type that the device will use, one of 'ethernet' or 'wifi' and defaults to 'ethernet' if not specified.
appUpdatePollInterval (Optional[str]): how often the OS checks for updates, in minutes.
wifiKey (Optional[str]): the key for the wifi network the device will connect to.
wifiSsid (Optional[str]): the ssid for the wifi network the device will connect to.
ip (Optional[str]): static ip address.
gateway (Optional[str]): static ip gateway.
netmask (Optional[str]): static ip netmask.
Returns:
dict: application config.json
Function: get_device_os_semver_with_variant(os_version, os_variant)
Get current device os semver with variant.
Args:
os_version (str): current os version.
os_variant (Optional[str]): os variant.
Examples:
>>> balena.models.device_os.get_device_os_semver_with_variant('balenaOS 2.29.2+rev1', 'prod')
'2.29.2+rev1.prod'
Function: get_supported_versions(device_type)
Get OS supported versions.
Args:
device_type (str): device type slug.
auth (Optional[bool]): if auth is True then auth header will be added to the request (to get private device types) otherwise no auth header and only public device types returned. Default to True.
Function: is_architecture_compatible_with(os_architecture, application_architecture)
Returns whether the specified OS architecture is compatible with the target architecture.
Args:
os_architecture (str): The OS's architecture as specified in its device type.
application_architecture (str): The application's architecture as specified in its device type.
Returns:
bool: Whether the specified OS architecture is capable of running applications build for the target architecture.
Function: parse_params()
Validate parameters for downloading device OS image.
Args:
**parameters: os parameters keyword arguments.
Returns:
dict: validated parameters.
Raises:
MissingOption: if mandatory option are missing.
InvalidOption: if appId or network are invalid (appId is not a number or parseable string. network is not in NETWORK_TYPES)
DeviceType
This class implements user API key model for balena python SDK.
Function: get(id_or_slug)
Get a single device type.
Args:
id_or_slug (str): device type slug or alias (string) or id (number).
Function: get_all()
Get all device types.
Returns:
list: list contains info of device types.
Function: get_all_supported()
Get all supported device types.
Returns:
list: list contains info of all supported device types.
Function: get_by_slug_or_name(slug_or_name)
Get a single device type by slug or name.
Args:
slug_or_name (str): device type slug or name.
Function: get_name(slug)
Get display name for a device.
Args:
slug (str): device type slug.
Function: get_slug_by_name(name)
Get device slug.
Args:
name (str): device type name.
EnvironmentVariable
This class is a wrapper for environment variable models.
ApplicationEnvVariable
This class implements application environment variable model for balena python SDK.
Attributes:
SYSTEM_VARIABLE_RESERVED_NAMES (list): list of reserved system variable names.
OTHER_RESERVED_NAMES_START (list): list of prefix for system variable.
Function: create(app_id, env_var_name, value)
Create an environment variable for application.
Args:
app_id (str): application id.
env_var_name (str): environment variable name.
value (str): environment variable value.
Returns:
dict: new application environment info.
Examples:
>>> balena.models.environment_variables.application.create('978062', 'test2', '123')
{
"id": 91138,
"application": {
"__deferred": {"uri": "/balena/application(978062)"},
"__id": 978062,
},
"name": "test2",
"value": "123",
"__metadata": {"uri": "/balena/environment_variable(91138)", "type": ""},
}
Function: get_all(app_id)
Get all environment variables by application.
Args:
app_id (str): application id.
Returns:
list: application environment variables.
Examples:
>>> balena.models.environment_variables.application.get_all(9020)
[
{
"application": {"__deferred": {"uri": "/ewa/application(9020)"}, "__id": 9020},
"__metadata": {"type": "", "uri": "/ewa/environment_variable(5650)"},
"id": 5650,
"value": "7330634368117899",
"name": "BALENA_RESTART",
}
]
Function: is_system_variable(variable)
Check if a variable is system specific.
Args:
variable (str): environment variable name.
Returns:
bool: True if system variable, False otherwise.
Examples:
>>> balena.models.environment_variables.application.is_system_variable('BALENA_API_KEY')
True
>>> balena.models.environment_variables.application.is_system_variable('APPLICATION_API_KEY')
False
Function: remove(var_id)
Remove application environment variable.
Args:
var_id (str): environment variable id.
Examples:
>>> balena.models.environment_variables.application.remove(5652)
'OK'
Function: update(var_id, value)
Update an environment variable value for application.
Args:
var_id (str): environment variable id.
value (str): new environment variable value.
Examples:
>>> balena.models.environment_variables.application.update(5652, 'new value')
'OK'
ServiceEnvVariable
This class implements service environment variable model for balena python SDK.
Function: create(app_id, service_name, env_var_name, value)
Create a service environment variable for application.
Args:
app_id (str): application id.
service_name(str): service name.
env_var_name (str): environment variable name.
value (str): environment variable value.
Returns:
str: new service environment variable info.
Examples:
>>> balena.models.environment_variables.service_environment_variable.create('1005160', 'proxy', 'app_proxy', 'test value')
{
"id": 12444,
"created_at": "2018-03-18T09:34:09.144Z",
"service": {"__deferred": {"uri": "/balena/service(21668)"}, "__id": 21668},
"name": "app_proxy",
"value": "test value",
"__metadata": {"uri": "/balena/service_environment_variable(12444)", "type": ""},
}
Function: get_all_by_application(app_id)
Get all service environment variables by application.
Args:
app_id (str): application id.
Returns:
list: service application environment variables.
Examples:
>>> balena.models.environment_variables.service_environment_variable.get_all_by_application('1005160')
[
{
"name": "app_data",
"service": {"__deferred": {"uri": "/balena/service(21667)"}, "__id": 21667},
"created_at": "2018-03-16T19:21:21.087Z",
"__metadata": {
"type": "",
"uri": "/balena/service_environment_variable(12365)",
},
"value": "app_data_value",
"id": 12365,
},
{
"name": "app_data1",
"service": {"__deferred": {"uri": "/balena/service(21667)"}, "__id": 21667},
"created_at": "2018-03-16T19:21:49.662Z",
"__metadata": {
"type": "",
"uri": "/balena/service_environment_variable(12366)",
},
"value": "app_data_value",
"id": 12366,
},
{
"name": "app_front",
"service": {"__deferred": {"uri": "/balena/service(21669)"}, "__id": 21669},
"created_at": "2018-03-16T19:22:06.955Z",
"__metadata": {
"type": "",
"uri": "/balena/service_environment_variable(12367)",
},
"value": "front_value",
"id": 12367,
},
]
Function: remove(var_id)
Remove service environment variable.
Args:
var_id (str): service environment variable id.
Examples:
>>> balena.models.environment_variables.service_environment_variable.remove('12444')
'OK'
Function: update(var_id, value)
Update a service environment variable value for application.
Args:
var_id (str): service environment variable id.
value (str): new service environment variable value.
Examples:
>>> balena.models.environment_variables.service_environment_variable.update('12444', 'new test value')
'OK'
DeviceEnvVariable
This class implements device environment variable model for balena python SDK.
Function: create(uuid, env_var_name, value)
Create a device environment variable.
Args:
uuid (str): device uuid.
env_var_name (str): environment variable name.
value (str): environment variable value.
Returns:
dict: new device environment variable info.
Examples:
>>> balena.models.environment_variables.device.create('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143','test_env4', 'testing1')
{
"name": "test_env4",
"__metadata": {"type": "", "uri": "/balena/device_environment_variable(42166)"},
"value": "testing1",
"device": {"__deferred": {"uri": "/balena/device(115792)"}, "__id": 115792},
"id": 42166,
}
Function: get_all(uuid)
Get all device environment variables.
Args:
uuid (str): device uuid.
Returns:
list: device environment variables.
Examples:
>>> balena.models.environment_variables.device.get_all('8deb12a58e3b6d3920db1c2b6303d1ff32f23d5ab99781ce1dde6876e8d143')
[
{
"device": {"__deferred": {"uri": "/ewa/device(122950)"}, "__id": 122950},
"__metadata": {"type": "", "uri": "/ewa/device_environment_variable(2173)"},
"id": 2173,
"value": "1322944771964103",
"env_var_name": "BALENA_DEVICE_RESTART",
}
]
Function: get_all_by_application(app_id)
Get all device environment variables for an application.
Args:
app_id (str): application id.
Returns:
list: list of device environment variables.
Examples:
>>> balena.models.environment_variables.device.get_all_by_application('5780')
[
{
"name": "device1",
"__metadata": {"type": "", "uri": "/balena/device_environment_variable(40794)"},
"value": "test",
"device": {"__deferred": {"uri": "/balena/device(115792)"}, "__id": 115792},
"id": 40794,
},
{
"name": "BALENA_DEVICE_RESTART",
"__metadata": {"type": "", "uri": "/balena/device_environment_variable(1524)"},
"value": "961506585823372",
"device": {"__deferred": {"uri": "/balena/device(121794)"}, "__id": 121794},
"id": 1524,
},
]
Function: remove(var_id)
Remove a device environment variable.
Args:
var_id (str): environment variable id.
Examples:
>>> balena.models.environment_variables.device.remove(2184)
'OK'
Function: update(var_id, value)
Update a device environment variable.
Args:
var_id (str): environment variable id.
value (str): new environment variable value.
Examples:
>>> balena.models.environment_variables.device.update(2184, 'new value')
'OK'
DeviceServiceEnvVariable
This class implements device service variable model for balena python SDK.
Function: create(uuid, service_name, env_var_name, value)
Create a device service environment variable.
Args:
uuid (str): device uuid.
service_name (str): service name.
env_var_name (str): device service environment variable name.
value (str): device service environment variable value.
Returns:
dict: new device service environment variable info.
Examples:
>>> balena.models.environment_variables.device_service_environment_variable.create('f5213eac0d63ac47721b037a7406d306', 'data', 'dev_data_sdk', 'test1')
{
"id": 28970,
"created_at": "2018-03-17T10:13:14.184Z",
"service_install": {
"__deferred": {"uri": "/balena/service_install(30789)"},
"__id": 30789,
},
"value": "test1",
"name": "dev_data_sdk",
"__metadata": {
"uri": "/balena/device_service_environment_variable(28970)",
"type": "",
},
}
Function: get_all(uuid)
Get all device service environment variables belong to a device.
Args:
uuid (str): device uuid.
Returns:
list: device service environment variables.
Examples:
>>> balena.models.environment_variables.device_service_environment_variable.get_all('f5213eac0d63ac47721b037a7406d306')
[
{
"name": "dev_proxy",
"created_at": "2018-03-16T19:23:21.727Z",
"__metadata": {
"type": "",
"uri": "/balena/device_service_environment_variable(28888)",
},
"value": "value",
"service_install": [
{
"__metadata": {"type": "", "uri": "/balena/service_install(30788)"},
"id": 30788,
"service": [
{
"service_name": "proxy",
"__metadata": {"type": "", "uri": "/balena/service(NaN)"},
}
],
}
],
"id": 28888,
},
{
"name": "dev_data",
"created_at": "2018-03-16T19:23:11.614Z",
"__metadata": {
"type": "",
"uri": "/balena/device_service_environment_variable(28887)",
},
"value": "dev_data_value",
"service_install": [
{
"__metadata": {"type": "", "uri": "/balena/service_install(30789)"},
"id": 30789,
"service": [
{
"service_name": "data",
"__metadata": {"type": "", "uri": "/balena/service(NaN)"},
}
],
}
],
"id": 28887,
},
{
"name": "dev_data1",
"created_at": "2018-03-17T05:53:19.257Z",
"__metadata": {
"type": "",
"uri": "/balena/device_service_environment_variable(28964)",
},
"value": "aaaa",
"service_install": [
{
"__metadata": {"type": "", "uri": "/balena/service_install(30789)"},
"id": 30789,
"service": [
{
"service_name": "data",
"__metadata": {"type": "", "uri": "/balena/service(NaN)"},
}
],
}
],
"id": 28964,
},
]
Function: get_all_by_application(app_id)
Get all device service environment variables belong to an application.
Args:
app_id (int): application id.
Returns:
list: list of device service environment variables.
Examples:
>>> balena.models.environment_variables.device_service_environment_variable.get_all_by_application(1043050)
[
{
"id": 566017,
"created_at": "2021-04-23T08:28:08.539Z",
"service_install": {
"__id": 1874939,
"__deferred": {"uri": "/resin/service_install(@id)[email protected]=1874939"},
},
"value": "1",
"name": "testEnv1",
"__metadata": {
"uri": "/resin/device_service_environment_variable(@id)[email protected]=566017"
},
},
{
"id": 566015,
"created_at": "2021-04-23T08:17:45.767Z",
"service_install": {
"__id": 1874939,
"__deferred": {"uri": "/resin/service_install(@id)[email protected]=1874939"},
},
"value": "12",
"name": "testEnv2",
"__metadata": {
"uri": "/resin/device_service_environment_variable(@id)[email protected]=566015"
},
},
]
Function: remove(var_id)
Remove a device service environment variable.
Args:
var_id (str): device service environment variable id.
Examples:
>>> balena.models.environment_variables.device_service_environment_variable.remove('28970')
'OK'
Function: update(var_id, value)
Update a device service environment variable.
Args:
var_id (str): device environment variable id.
value (str): new device environment variable value.
Examples:
>>> balena.models.environment_variables.device_service_environment_variable.update('28970', 'test1 new')
'OK'
Image
This class implements image model for balena python SDK.
Function: get(id)
Get a specific image.
Args:
id (str): image id.
Returns:
dict: image info.
Raises:
ImageNotFound: if image couldn't be found.
Function: get_log(id)
Get the build log from an image.
Args:
id (str): image id.
Returns:
str: build log.
Raises:
ImageNotFound: if image couldn't be found.
Organization
This class implements organization model for balena python SDK.
Function: create(name, handle)
Creates a new organization.
Args:
name (str): the name of the organization that will be created.
handle (Optional[str]): The handle of the organization that will be created.
Returns:
dict: organization info.
Examples:
>>> balena.models.organization.create('My Org', 'test_org')
{
"id": 147950,
"created_at": "2020-06-23T09:33:25.187Z",
"name": "My Org",
"handle": "test_org",
"billing_account_code": None,
"__metadata": {"uri": "/resin/organization(@id)[email protected]=147950"},
}
Function: get(org_id)
Get a single organization by id.
Args:
org_id (str): organization id.
Returns:
dict: organization info.
Raises:
OrganizationNotFound: if organization couldn't be found.
Examples:
>>> balena.models.organization.get('26474')
{
"id": 26474,
"created_at": "2018-08-14T00:24:33.144Z",
"name": "test_account1",
"handle": "test_account1",
"billing_account_code": None,
"__metadata": {"uri": "/resin/organization(@id)[email protected]=26474"},
}
Function: get_all()
Get all organizations.
Returns:
list: list contains information of organizations.
Examples:
>>> balena.models.organization.get_all()
[
{
"id": 26474,
"created_at": "2018-08-14T00:24:33.144Z",
"name": "test_account1",
"handle": "test_account1",
"billing_account_code": None,
"__metadata": {"uri": "/resin/organization(@id)[email protected]=26474"},
}
]
Function: get_by_handle(handle)
Get a single organization by handle.
Args:
handle (str): organization handle.
Returns:
dict: organization info.
Raises:
OrganizationNotFound: if organization couldn't be found.
Examples:
>>> balena.models.organization.get_by_handle('test_account1')
{
"id": 26474,
"created_at": "2018-08-14T00:24:33.144Z",
"name": "test_account1",
"handle": "test_account1",
"billing_account_code": None,
"__metadata": {"uri": "/resin/organization(@id)[email protected]=26474"},
}
Function: remove(org_id)
Remove an organization.
Args:
org_id (str): organization id.
Returns:
dict: organization info.
Examples:
>>> balena.models.organization.remove('148003')
'OK
OrganizationInvite
This class implements organization invite model for balena python SDK.
Function: accept(invite_token)
Accepts an invite.
Args:
invite_token (str): invitation Token - invite token.
Function: create(org_id, invitee, role_name, message)
Creates a new invite for an organization.
Args:
org_id (str): organization id.
invitee (str): the email/balena_username of the invitee.
role_name (Optional[str]): the role name to be granted to the invitee.
message (Optional[str]): the message to send along with the invite.
Returns:
dict: organization invite.
Examples:
>>> balena.models.organization.invite.create(26474, '[email protected]', 'member', 'Test invite')
{
"id": 2862,
"message": "Test invite",
"invitee": {"__id": 2965, "__deferred": {"uri": "/resin/invitee(@id)[email protected]=2965"}},
"is_invited_to__organization": {
"__id": 26474,
"__deferred": {"uri": "/resin/organization(@id)[email protected]=26474"},
},
"is_created_by__user": {
"__id": 5227,
"__deferred": {"uri": "/resin/user(@id)[email protected]=5227"},
},
"organization_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/organization_membership_role(@id)[email protected]=2"},
},
"__metadata": {"uri": "/resin/invitee__is_invited_to__organization(@id)[email protected]=2862"},
}
Function: get_all()
Get all invites.
Returns:
list: list contains info of invites.
Examples:
>>> balena.models.organization.invite.get_all()
[
{
"id": 2862,
"message": "Test invite",
"invitee": {
"__id": 2965,
"__deferred": {"uri": "/resin/invitee(@id)[email protected]=2965"},
},
"is_invited_to__organization": {
"__id": 26474,
"__deferred": {"uri": "/resin/organization(@id)[email protected]=26474"},
},
"is_created_by__user": {
"__id": 5227,
"__deferred": {"uri": "/resin/user(@id)[email protected]=5227"},
},
"organization_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/organization_membership_role(@id)[email protected]=2"},
},
"__metadata": {
"uri": "/resin/invitee__is_invited_to__organization(@id)[email protected]=2862"
},
}
]
Function: get_all_by_organization(org_id)
Get all invites by organization.
Args:
org_id (str): organization id.
Returns:
list: list contains info of invites.
Examples:
>>> balena.models.organization.invite.get_all_by_organization(26474)
[
{
"id": 2862,
"message": "Test invite",
"invitee": {
"__id": 2965,
"__deferred": {"uri": "/resin/invitee(@id)[email protected]=2965"},
},
"is_invited_to__organization": {
"__id": 26474,
"__deferred": {"uri": "/resin/organization(@id)[email protected]=26474"},
},
"is_created_by__user": {
"__id": 5227,
"__deferred": {"uri": "/resin/user(@id)[email protected]=5227"},
},
"organization_membership_role": {
"__id": 2,
"__deferred": {"uri": "/resin/organization_membership_role(@id)[email protected]=2"},
},
"__metadata": {
"uri": "/resin/invitee__is_invited_to__organization(@id)[email protected]=2862"
},
}
]
Function: revoke(invite_id)
Revoke an invite.
Args:
invite_id (str): organization invite id.
Examples:
>>> balena.models.organization.invite.revoke(2862)
'OK'
OrganizationMembership
This class implements organization membership model for balena python SDK.
Function: get(membership_id)
Get a single organization membership.
Args:
membership_id (str): membership id.
Returns:
Organization membership.
Raises:
OrganizationNotFound: if organization couldn't be found.
Examples:
>>> balena.models.organization.membership.get(17608)
[
{
"id": 17608,
"created_at": "2017-08-03T11:16:03.022Z",
"user": {"__id": 22294, "__deferred": {"uri": "/resin/user(@id)[email protected]=22294"}},
"is_member_of__organization": {
"__id": 3014,
"__deferred": {"uri": "/resin/organization(@id)[email protected]=3014"},
},
"organization_membership_role": {
"__id": 3,
"__deferred": {"uri": "/resin/organization_membership_role(@id)[email protected]=3"},
},
"__metadata": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
}
]
Function: get_all()
Get all organization memberships.
Returns:
list: organization memberships.
Examples:
>>> balena.models.organization.membership.get_all()
[
{
"id": 17608,
"created_at": "2017-08-03T11:16:03.022Z",
"user": {"__id": 22294, "__deferred": {"uri": "/resin/user(@id)[email protected]=22294"}},
"is_member_of__organization": {
"__id": 3014,
"__deferred": {"uri": "/resin/organization(@id)[email protected]=3014"},
},
"organization_membership_role": {
"__id": 3,
"__deferred": {"uri": "/resin/organization_membership_role(@id)[email protected]=3"},
},
"__metadata": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
}
]
Function: get_all_by_organization(handle_or_id)
Get all memberships by organization.
Args:
handle_or_id (str): organization handle (string) or id (number).
Returns:
list: organization memberships.
Raises:
OrganizationNotFound: if organization couldn't be found.
Examples:
>>> balena.models.organization.membership.get_all_by_organization(3014)
[
{
"id": 17608,
"created_at": "2017-08-03T11:16:03.022Z",
"user": {"__id": 22294, "__deferred": {"uri": "/resin/user(@id)[email protected]=22294"}},
"is_member_of__organization": {
"__id": 3014,
"__deferred": {"uri": "/resin/organization(@id)[email protected]=3014"},
},
"organization_membership_role": {
"__id": 3,
"__deferred": {"uri": "/resin/organization_membership_role(@id)[email protected]=3"},
},
"__metadata": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
}
]
OrganizationMembershipTag
This class implements organization membership tag model for balena python SDK.
Function: get_all()
Get all organization membership tags.
Returns:
list: organization membership tags.
Examples:
>>> balena.models.organization.membership.tag.get_all()
[
{
"id": 991,
"organization_membership": {
"__id": 17608,
"__deferred": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
},
"tag_key": "mTag1",
"value": "Python SDK 1",
"__metadata": {"uri": "/resin/organization_membership_tag(@id)[email protected]=991"},
}
]
Function: get_all_by_organization(handle_or_id)
Get all organization membership tags for an organization.
Args:
handle_or_id (str): organization handle (string) or id (number).
Returns:
list: organization membership tags.
Raises:
OrganizationNotFound: if organization couldn't be found.
Examples:
>>> balena.models.organization.membership.tag.get_all_by_organization(3014)
[
{
"id": 991,
"organization_membership": {
"__id": 17608,
"__deferred": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
},
"tag_key": "mTag1",
"value": "Python SDK 1",
"__metadata": {"uri": "/resin/organization_membership_tag(@id)[email protected]=991"},
}
]
Function: get_all_by_organization_membership(membership_id)
Get all organization membership tags for all memberships of an organization.
Args:
membership_id (str): organization membership id.
Returns:
list: organization membership tags.
Examples:
>>> balena.models.organization.membership.tag.get_all_by_organization_membership(17608)
[
{
"id": 991,
"organization_membership": {
"__id": 17608,
"__deferred": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
},
"tag_key": "mTag1",
"value": "Python SDK 1",
"__metadata": {"uri": "/resin/organization_membership_tag(@id)[email protected]=991"},
}
]
Function: remove(membership_id, tag_key)
Remove an organization membership tag.
Args:
membership_id: organization membership id.
tag_key (str): tag key.
Examples:
>>> balena.models.organization.membership.tag.remove(17608, 'mTag1')
'OK'
Function: set(membership_id, tag_key, value)
Set an organization membership tag.
Args:
membership_id: organization membership id.
tag_key (str): tag key.
value (str): tag value.
Returns:
dict: dict contains organization membership tag info if tag doesn't exist.
OK: if tag exists.
Examples:
>>> balena.models.organization.membership.tag.set(17608, 'mTag1', 'Python SDK')
{
"id": 991,
"organization_membership": {
"__id": 17608,
"__deferred": {"uri": "/resin/organization_membership(@id)[email protected]=17608"},
},
"tag_key": "mTag1",
"value": "Python SDK",
"__metadata": {"uri": "/resin/organization_membership_tag(@id)[email protected]=991"},
}
>>> balena.models.organization.membership.tag.set(17608, 'mTag1', 'Python SDK 1')
'OK'
Release
This class implements release model for balena python SDK.
Function: create_from_url(app_id, url, flatten_tarball)
Create a new release built from the source in the provided url.
Args:
app_id (int): application id.
url (str): a url with a tarball of the project to build.
flatten_tarball (Optional[bool]): Should be true when the tarball includes an extra root folder with all the content.
Returns:
int: release Id.
Raises:
BuilderRequestError: if builder returns any errors.
Function: finalize(commit_or_id)
Finalizes a draft release.
Args:
commit_or_id (str): release commit (str) or id (int).
Returns:
OK
Raises:
ReleaseNotFound: if release couldn't be found.
Function: get(commit_or_id)
Get a specific release.
Args:
commit_or_id: release commit (str) or id (int).
Returns:
dict: release info.
Raises:
ReleaseNotFound: if release couldn't be found.
Function: get_all_by_application(app_id)
Get all releases from an application.
Args:
app_id (str): applicaiton id.
Returns:
list: release info.
Function: get_latest_by_application(app_id)
Get the latest successful release for an application.
Args:
app_id (str): applicaiton id.
Returns:
dict: release info.
Function: get_with_image_details(commit_or_id)
Get a specific release with the details of the images built.
Args:
commit_or_id: release commit (str) or id (int).
Returns:
dict: release info.
Raises:
ReleaseNotFound: if release couldn't be found.
Function: remove(commit_or_id)
Remove a specific release. This function only works if you log in using credentials or Auth Token.
Args:
commit_or_id: release commit (str) or id (int).
Raises:
ReleaseNotFound: if release couldn't be found.
AmbiguousRelease: if release commit is ambiguous.
Function: set_is_invalidated(commit_or_id, is_invalidated)
Set the is_invalidated property of a release to True or False.
Args:
commit_or_id (str): release commit (str) or id (int).
is_invalidated (bool): True for invalidated, False for validated.
Returns:
OK
Raises:
ReleaseNotFound: if release couldn't be found.
Function: set_known_issue_list(commit_or_id, known_issue_list)
Set a known issue list for a release.
Args:
commit_or_id (str): release commit (str) or id (int).
known_issue_list (str): the known issue list.
Returns:
OK
Raises:
ReleaseNotFound: if release couldn't be found.
Function: set_note(commit_or_id, note)
Set a note for a release.
Args:
commit_or_id (str): release commit (str) or id (int).
note (str): the note.
Returns:
OK
Raises:
ReleaseNotFound: if release couldn't be found.
Function: set_release_version(commit_or_id, semver)
Set a version for a release.
Args:
commit_or_id (str): release commit (str) or id (int).
semver (str): the release version, only supports semver compliant values.
Returns:
OK
Raises:
ReleaseNotFound: if release couldn't be found.
Service
This class implements service model for balena python SDK.
Function: get_all_by_application(app_id)
Get all services from an application.
Args:
app_id (str): application id.
Returns:
list: service info.
Tag
This class is a wrapper for Tag models.
DeviceTag
This class implements device tag model for balena python SDK.
Function: get_all()
Get all device tags.
Returns:
list: list contains device tags.
Examples:
>>> balena.models.tag.device.get_all()
[
{
"device": {"__deferred": {"uri": "/balena/device(1036574)"}, "__id": 1036574},
"tag_key": "db_tag",
"id": 20157,
"value": "rpi3",
"__metadata": {"type": "", "uri": "/balena/device_tag(20157)"},
},
{
"device": {"__deferred": {"uri": "/balena/device(1055117)"}, "__id": 1055117},
"tag_key": "group1",
"id": 20158,
"value": "aaa",
"__metadata": {"type": "", "uri": "/balena/device_tag(20158)"},
},
]
Function: get_all_by_application(app_id)
Get all device tags for an application.
Args:
app_id (str): application id .
Returns:
list: list contains device tags.
Examples:
>>> balena.models.tag.device.get_all_by_application('1005160')
[
{
"device": {"__deferred": {"uri": "/balena/device(1055117)"}, "__id": 1055117},
"tag_key": "group1",
"id": 20158,
"value": "aaa",
"__metadata": {"type": "", "uri": "/balena/device_tag(20158)"},
},
{
"device": {"__deferred": {"uri": "/balena/device(1055116)"}, "__id": 1055116},
"tag_key": "group1",
"id": 20159,
"value": "bbb",
"__metadata": {"type": "", "uri": "/balena/device_tag(20159)"},
},
]
Function: get_all_by_device(uuid)
Get all device tags for a device.
Args:
uuid (str): device uuid.
Returns:
list: list contains device tags.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.tag.device.get_all_by_device('a03ab646c01f39e39a1e3deb7fce76b93075c6d599fd5be4a889b8145e2f8f')
[
{
"device": {"__deferred": {"uri": "/balena/device(1055116)"}, "__id": 1055116},
"tag_key": "group1",
"id": 20159,
"value": "bbb",
"__metadata": {"type": "", "uri": "/balena/device_tag(20159)"},
},
{
"device": {"__deferred": {"uri": "/balena/device(1055116)"}, "__id": 1055116},
"tag_key": "db_tag",
"id": 20160,
"value": "aaa",
"__metadata": {"type": "", "uri": "/balena/device_tag(20160)"},
},
]
Function: remove(parent_id, tag_key)
Remove a device tag.
Args:
parent_id (Union[str, int]): device uuid or device id.
tag_key (str): tag key.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.tag.device.remove('f5213eac0d63ac47721b037a7406d306', 'testtag'))
OK
Function: set(parent_id, tag_key, value)
Set a device tag (update tag value if it exists). Note: Notice that when using the device ID rather than UUID, it will avoid one extra API roundtrip.
Args:
parent_id (Union[str, int]): device uuid or device id.
tag_key (str): tag key.
value (str): tag value.
Returns:
dict: dict contains device tag info if tag doesn't exist.
OK: if tag exists.
Raises:
DeviceNotFound: if device couldn't be found.
Examples:
>>> balena.models.tag.device.set('f5213eac0d63ac47721b037a7406d306', 'testtag','test1')
{
"device": {"__deferred": {"uri": "/balena/device(1036574)"}, "__id": 1036574},
"tag_key": "testtag",
"id": 20163,
"value": "test1",
"__metadata": {"type": "", "uri": "/balena/device_tag(20163)"},
}
>>> balena.models.tag.device.set('f5213eac0d63ac47721b037a7406d306', 'testtag','test2')
OK
ApplicationTag
This class implements application tag model for balena python SDK.
Function: get_all()
Get all application tags.
Returns:
list: list contains application tags.
Examples:
>>> balena.models.tag.application.get_all()
[
{
"application": {
"__deferred": {"uri": "/balena/application(1005160)"},
"__id": 1005160,
},
"tag_key": "appTag",
"id": 12886,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/application_tag(12886)"},
},
{
"application": {
"__deferred": {"uri": "/balena/application(1005767)"},
"__id": 1005767,
},
"tag_key": "appTa1",
"id": 12887,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/application_tag(12887)"},
},
]
Function: get_all_by_application(app_id)
Get all application tags for an application.
Args:
app_id (str): application id .
Returns:
list: list contains application tags.
Examples:
>>> balena.models.tag.application.get_all_by_application('1005767')
[
{
"application": {
"__deferred": {"uri": "/balena/application(1005767)"},
"__id": 1005767,
},
"tag_key": "appTa1",
"id": 12887,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/application_tag(12887)"},
},
{
"application": {
"__deferred": {"uri": "/balena/application(1005767)"},
"__id": 1005767,
},
"tag_key": "appTag2",
"id": 12888,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/application_tag(12888)"},
},
]
Function: remove(app_id, tag_key)
Remove an application tag.
Args:
app_id (str): application id.
tag_key (str): tag key.
Examples:
>>> balena.models.tag.application.remove('1005767', 'tag1')
OK
Function: set(app_id, tag_key, value)
Set an application tag (update tag value if it exists).
Args:
app_id (str): application id.
tag_key (str): tag key.
value (str): tag value.
Returns:
dict: dict contains application tag info if tag doesn't exist.
OK: if tag exists.
Examples:
>>> balena.models.tag.application.set('1005767', 'tag1', 'Python SDK')
{
"application": {
"__deferred": {"uri": "/balena/application(1005767)"},
"__id": 1005767,
},
"tag_key": "tag1",
"id": 12889,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/application_tag(12889)"},
}
>>> balena.models.tag.application.set('1005767', 'tag1','Balena Python SDK')
OK
ReleaseTag
This class implements release tag model for balena python SDK.
Function: get_all()
Get all release tags.
Returns:
list: list contains release tags.
Examples:
>>> balena.models.tag.release.get_all()
[
{
"release": {"__deferred": {"uri": "/balena/release(465307)"}, "__id": 465307},
"tag_key": "releaseTag1",
"id": 135,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/release_tag(135)"},
}
]
Function: get_all_by_application(app_id)
Get all release tags for an application.
Args:
app_id (str): application id.
Returns:
list: list contains release tags.
Examples:
>>> balena.models.tag.release.get_all_by_application('1043050')
[
{
"release": {"__deferred": {"uri": "/balena/release(465307)"}, "__id": 465307},
"tag_key": "releaseTag1",
"id": 135,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/release_tag(135)"},
}
]
Function: get_all_by_release(commit_or_id)
Get all release tags for a release.
Args:
commit_or_id: release commit (str) or id (int).
Returns:
list: list contains release tags.
Examples:
>>> balena.models.tag.release.get_all_by_release(135)
[
{
"release": {"__deferred": {"uri": "/balena/release(465307)"}, "__id": 465307},
"tag_key": "releaseTag1",
"id": 135,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/release_tag(135)"},
}
]
Function: remove(commit_or_id, tag_key)
Remove a release tag.
Args:
commit_or_id: release commit (str) or id (int).
tag_key (str): tag key.
Examples:
>>> balena.models.tag.release.remove(135, 'releaseTag1')
OK
Function: set(commit_or_id, tag_key, value)
Set a release tag (update tag value if it exists).
Args:
commit_or_id: release commit (str) or id (int).
tag_key (str): tag key.
value (str): tag value.
Returns:
dict: dict contains release tag info if tag doesn't exist.
OK: if tag exists.
Examples:
>>> balena.models.tag.release.set(465307, 'releaseTag1', 'Python SDK')
{
"release": {"__deferred": {"uri": "/balena/release(465307)"}, "__id": 465307},
"tag_key": "releaseTag1",
"id": 135,
"value": "Python SDK",
"__metadata": {"type": "", "uri": "/balena/release_tag(135)"},
}
>>> balena.models.tag.release.set(465307, 'releaseTag1', 'Python SDK 1')
OK
Key
This class implements ssh key model for balena python SDK.
Function: create(title, key)
Create a ssh key. This function only works if you log in using credentials or Auth Token.
Args:
title (str): key title.
key (str): the public ssh key.
Returns:
str: new ssh key id.
Function: get(id)
Get a single ssh key.
Args:
id (str): key id.
Returns:
dict: ssh key info.
Raises:
KeyNotFound: if ssh key couldn't be found.
Function: get_all()
Get all ssh keys.
Returns:
list: list of ssh keys.
Function: remove(id)
Remove a ssh key. This function only works if you log in using credentials or Auth Token.
Args:
id (str): key id.
Supervisor
This class implements supervisor model for balena python SDK.
Attributes:
SUPERVISOR_API_VERSION (str): supervisor API version.
SUPERVISOR_ADDRESS (str): supervisor endpoint address on device.
SUPERVISOR_API_KEY (str): supervisor API key on device.
_on_device (bool): API endpoint flag.
If True then all commands will be sent to the API on device.
If False then all command will be sent to the balena API proxy endpoint (api.balena-cloud.com/supervisor/<url>).
If SUPERVISOR_ADDRESS and SUPERVISOR_API_KEY are available, _on_device will be set to True by default. Otherwise, it's False.
Function: blink(device_uuid, app_id)
Start a blink pattern on a LED for 15 seconds. This is the same with balena.models.device.identify()
.
No need to set device_uuid and app_id if command is sent to the API on device.
Args:
device_uuid (Optional[str]): device uuid.
app_id (Optional[str]): application id.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.blink(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
'OK'
Function: force_api_endpoint(endpoint)
Force all API commands to a specific endpoint.
Args:
endpoint (bool): True if selecting the API on device. False if selecting the balena API proxy endpoint.
Raises:
InvalidOption: if endpoint value is not bool.
Function: get_application_info(app_id, device_uuid)
Deprecated Return information about the application running on the device. This function requires supervisor v1.8 or higher. No need to set device_uuid if command is sent to the API on device.
Args:
app_id (str): application id.
device_uuid (Optional[str]): device uuid.
Returns:
dict: dictionary contains application information.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.get_application_info(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
Function: get_device_state(app_id, device_uuid)
Return the current device state, as reported to the balena API and with some extra fields added to allow control over pending/locked updates. This function requires supervisor v1.6 or higher. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
app_id (Optional[str]): application id.
device_uuid (Optional[str]): device uuid.
Returns:
dict: dictionary contains device state.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.get_device_state(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
{
"status": "Idle",
"update_failed": False,
"update_pending": False,
"download_progress": None,
"os_version": "Balena OS 1.1.1",
"api_port": 48484,
"commit": "ff812b9a5f82d9661fb23c24aa86dce9425f1112",
"update_downloaded": False,
"supervisor_version": "1.7.0",
"ip_address": "192.168.0.102",
}
Function: ping(device_uuid)
Check that the supervisor is alive and well. No need to set device uuid if command is sent to the API on device.
Args:
device_uuid (Optional[str]): device uuid, mandatory if not calling this method from the service container on balena device.
Returns:
str: `OK` signals that the supervisor is alive and well.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not set.
Examples:
>>> balena.models.supervisor.ping(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3')
'OK'
Function: purge(app_id, device_uuid)
Clears the user application's /data folder. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
app_id (str): application id.
device_uuid (Optional[str]): device uuid.
Returns:
dict: when successful, this dictionary is returned `{ 'Data': 'OK', 'Error': '' }`.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.purge(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
{u'Data': u'OK', u'Error': u''}
Function: reboot(device_uuid, app_id, force)
Reboot the device. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
device_uuid (Optional[str]): device uuid.
app_id (Optional[str]): application id.
force (Optional[bool]): If force is True, the update lock will be overridden.
Returns:
dict: when successful, this dictionary is returned `{ 'Data': 'OK', 'Error': '' }`.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.reboot(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
{u'Data': u'OK', u'Error': u''}
Function: regenerate_supervisor_api_key(app_id, device_uuid)
Invalidate the current SUPERVISOR_API_KEY and generates a new one. The application will be restarted on the next update cycle to update the API key environment variable. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
app_id (Optional[str]): application id.
device_uuid (Optional[str]): device uuid.
Returns:
str: new supervisor API key.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.regenerate_supervisor_api_key(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
'480af7bb8a9cf56de8a1e295f0d50e6b3bb46676aaddbf4103aa43cb57039364'
Function: restart(app_id, device_uuid)
Restart user application container. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
app_id (str): application id.
device_uuid (Optional[str]): device uuid.
Returns:
str: `OK` signals that the supervisor is alive and well.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.restart(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
'OK'
Function: restart_service(device_uuid, image_id)
Restart a service on device.
Args:
device_uuid (str): device uuid.
image_id (int): id of the image to start
Examples:
>>> balena.models.supervisor.restart_service('f3887b184396844f52402c5cf09bd3b9', 392229)
OK
Function: shutdown(device_uuid, app_id, force)
Shut down the device. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
device_uuid (Optional[str]): device uuid.
app_id (Optional[str]): application id.
force (Optional[bool]): If force is True, the update lock will be overridden.
Returns:
dict: when successful, this dictionary is returned `{ 'Data': 'OK', 'Error': '' }`.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.shutdown(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='8362')
{u'Data': u'OK', u'Error': u''}
Function: start_application(app_id, device_uuid)
Deprecated
Starts a user application container, usually after it has been stopped with stop_application()
.
This function requires supervisor v1.8 or higher.
No need to set device_uuid if command is sent to the API on device.
Args:
app_id (str): application id.
device_uuid (Optional[str]): device uuid.
Returns:
dict: dictionary contains started application container id.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.start_application(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
Function: start_service(device_uuid, image_id)
Start a service on device.
Args:
device_uuid (str): device uuid.
image_id (int): id of the image to start
Examples:
>>> balena.models.supervisor.start_service('f3887b184396844f52402c5cf09bd3b9', 392229)
OK
Function: stop_application(app_id, device_uuid)
Deprecated Temporarily stops a user application container. Application container will not be removed after invoking this function and a reboot or supervisor restart will cause the container to start again. This function requires supervisor v1.8 or higher. No need to set device_uuid if command is sent to the API on device.
Args:
app_id (str): application id.
device_uuid (Optional[str]): device uuid.
Returns:
dict: dictionary contains stopped application container id.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.stop_application(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
Function: stop_service(device_uuid, image_id)
Stop a service on device.
Args:
device_uuid (str): device uuid.
image_id (int): id of the image to start
Examples:
>>> balena.models.supervisor.stop_service('f3887b184396844f52402c5cf09bd3b9', 392229)
OK
Function: update(device_uuid, app_id, force)
Triggers an update check on the supervisor. Optionally, forces an update when updates are locked. No need to set device_uuid and app_id if command is sent to the API on device.
Args:
device_uuid (Optional[str]): device uuid.
app_id (Optional[str]): application id.
force (Optional[bool]): If force is True, the update lock will be overridden.
Raises:
InvalidOption: if the endpoint is balena API proxy endpoint and device_uuid or app_id is not specified.
Examples:
>>> balena.models.supervisor.update(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020')
(Empty Response)
# Force an update
>>> balena.models.supervisor.update(device_uuid='8f66ec7335267e7cc7999ca9eec029a01ea7d823214c742ace5cfffaa21be3', app_id='9020', force=True)
(Empty Response)
History
This class is a wrapper for history models.
DeviceHistory
This class implements device history model for balena python SDK.
Function: get_all_by_application(app_id, fromDate, toDate)
Get all device history entries for an application.
Args:
app_id (Union[str, int]): application id.
Returns:
list: device history entries.
Examples:
>>> balena.models.history.device.get_all_by_application(2036095)
>>> balena.models.history.device.get_all_by_application(2036095, fromDate=datetime.utcnow() + timedelta(days=-5))
>>> balena.models.history.device.get_all_by_application(2036095, fromDate=datetime.utcnow() + timedelta(days=-10), toDate=fromDate = datetime.utcnow() + timedelta(days=-5)))
[
{
"id": 48262901,
"tracks__device": {
"__id": 11196426,
"__deferred": {"uri": "/v6/device(@id)[email protected]=11196426"},
},
"tracks__actor": {
"__id": 14923839,
"__deferred": {"uri": "/v6/actor(@id)[email protected]=14923839"},
},
"uuid": "6046335305c8142883a4466d30abe211c3a648251556c23520dcff503c9dab",
"is_active": False,
"belongs_to__application": {
"__id": 2036095,
"__deferred": {"uri": "/v6/application(@id)[email protected]=2036095"},
},
"is_running__release": {
"__id": 2534745,
"__deferred": {"uri": "/v6/release(@id)[email protected]=2534745"},
},
"should_be_running__release": {
"__id": 2534744,
"__deferred": {"uri": "/v6/release(@id)[email protected]=2534744"},
},
"should_be_managed_by__release": None,
"os_version": None,
"os_variant": None,
"supervisor_version": None,
"is_of__device_type": {
"__id": 57,
"__deferred": {"uri": "/v6/device_type(@id)[email protected]=57"},
},
"end_timestamp": None,
"created_at": "2023-03-28T20:28:22.773Z",
"is_created_by__actor": {
"__id": 14845379,
"__deferred": {"uri": "/v6/actor(@id)[email protected]=14845379"},
},
"is_ended_by__actor": None,
"__metadata": {"uri": "/v6/device_history(@id)[email protected]=48262901"},
}
]
Function: get_all_by_device(uuid_or_id, fromDate, toDate)
Get all device history entries for a device.
Args:
uuid_or_id (str): device uuid or id.
Returns:
list: device history entries.
Examples:
>>> balena.models.history.device_history.get_all_by_device('6046335305c8142883a4466d30abe211c3a648251556c23520dcff503c9dab')
>>> balena.models.history.device_history.get_all_by_device('6046335305c8142883a4466d30abe211')
>>> balena.models.history.device_history.get_all_by_device(11196426)
>>> balena.models.history.device_history.get_all_by_device(11196426, fromDate=datetime.utcnow() + timedelta(days=-5))
>>> balena.models.history.device_history.get_all_by_device(11196426, fromDate=datetime.utcnow() + timedelta(days=-10), toDate=fromDate = datetime.utcnow() + timedelta(days=-5)))
[
{
"id": 48262901,
"tracks__device": {
"__id": 11196426,
"__deferred": {"uri": "/v6/device(@id)[email protected]=11196426"},
},
"tracks__actor": {
"__id": 14923839,
"__deferred": {"uri": "/v6/actor(@id)[email protected]=14923839"},
},
"uuid": "6046335305c8142883a4466d30abe211c3a648251556c23520dcff503c9dab",
"is_active": False,
"belongs_to__application": {
"__id": 2036095,
"__deferred": {"uri": "/v6/application(@id)[email protected]=2036095"},
},
"is_running__release": {
"__id": 2534745,
"__deferred": {"uri": "/v6/release(@id)[email protected]=2534745"},
},
"should_be_running__release": {
"__id": 2534744,
"__deferred": {"uri": "/v6/release(@id)[email protected]=2534744"},
},
"should_be_managed_by__release": None,
"os_version": None,
"os_variant": None,
"supervisor_version": None,
"is_of__device_type": {
"__id": 57,
"__deferred": {"uri": "/v6/device_type(@id)[email protected]=57"},
},
"end_timestamp": None,
"created_at": "2023-03-28T20:28:22.773Z",
"is_created_by__actor": {
"__id": 14845379,
"__deferred": {"uri": "/v6/actor(@id)[email protected]=14845379"},
},
"is_ended_by__actor": None,
"__metadata": {"uri": "/v6/device_history(@id)[email protected]=48262901"},
}
]
Auth
This class implements all authentication functions for balena python SDK.
Function: authenticate()
This function authenticates provided credentials information. You should use Auth.login when possible, as it takes care of saving the Auth Token and username as well.
Args:
**credentials: credentials keyword arguments.
username (str): Balena username.
password (str): Password.
Returns:
str: Auth Token,
Raises:
LoginFailed: if the username or password is invalid.
Examples:
>>> balena.auth.authenticate(username='<your email>', password='<your password>')
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NTM5NywidXNlcm5hbWUiOiJnX3Ryb25nX25naGlhX25ndXllbiIsImVtYWlsIjoicmVzaW5weXRob25zZGt0ZXN0QGdtYWlsLmNvbSIsInNvY2lhbF9zZXJ2aWNlX2FjY291bnQiOlt7ImNyZWF0ZWRfYXQiOiIyMDE1LTExLTIzVDAzOjMwOjE0LjU3MloiLCJpZCI6MTE2NiwidXNlciI6eyJfX2RlZmVycmVkIjp7InVyaSI6Ii9ld2EvdXNlcig1Mzk3KSJ9LCJfX2lkIjo1Mzk3fSwicHJvdmlkZXIiOiJnb29nbGUiLCJyZW1vdGVfaWQiOiIxMDE4OTMzNzc5ODQ3NDg1NDMwMDIiLCJkaXNwbGF5X25hbWUiOiJUcm9uZyBOZ2hpYSBOZ3V5ZW4iLCJfX21ldGFkYXRhIjp7InVyaSI6Ii9ld2Evc29jaWFsX3NlcnZpY2VfYWNjb3VudCgxMTY2KSIsInR5cGUiOiIifX1dLCJoYXNfZGlzYWJsZWRfbmV3c2xldHRlciI6ZmFsc2UsImp3dF9zZWNyZXQiOiI0UDVTQzZGV1pIVU5JR0NDT1dJQUtST0tST0RMUTRNVSIsImhhc1Bhc3N3b3JkU2V0Ijp0cnVlLCJuZWVkc1Bhc3N3b3JkUmVzZXQiOmZhbHNlLCJwdWJsaWNfa2V5Ijp0cnVlLCJmZWF0dXJlcyI6W10sImludGVyY29tVXNlck5hbWUiOiJnX3Ryb25nX25naGlhX25ndXllbiIsImludGVyY29tVXNlckhhc2giOiI5YTM0NmUwZTgzNjk0MzYxODU3MTdjNWRhZTZkZWZhZDdiYmM4YzZkOGNlMzgxYjhhYTY5YWRjMTRhYWZiNGU0IiwicGVybWlzc2lvbnMiOltdLCJpYXQiOjE0NDgyNTYzMDYsImV4cCI6MTQ0ODg2MTEwNn0.U9lfEpPHBRvGQSayASE-glI-lQtAjyIFYd00uXOUzLI'
Function: get_email()
This function retrieves current logged in user's get_email
Returns:
str: user email.
Raises:
InvalidOption: if not logged in.
Examples:
# If you are logged in.
>>> balena.auth.get_email()
u'[email protected]'
Function: get_token()
This function retrieves Auth Token.
Returns:
str: Auth Token.
Raises:
InvalidOption: if not logged in and there is no token in Settings.
Examples:
# If you are logged in.
>>> balena.auth.get_token()
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NTM5NywidXNlcm5hbWUiOiJnX3Ryb25nX25naGlhX25ndXllbiIsImVtYWlsIjoicmVzaW5weXRob25zZGt0ZXN0QGdtYWlsLmNvbSIsInNvY2lhbF9zZXJ2aWNlX2FjY291bnQiOlt7ImNyZWF0ZWRfYXQiOiIyMDE1LTExLTIzVDAzOjMwOjE0LjU3MloiLCJpZCI6MTE2NiwidXNlciI6eyJfX2RlZmVycmVkIjp7InVyaSI6Ii9ld2EvdXNlcig1Mzk3KSJ9LCJfX2lkIjo1Mzk3fSwicHJvdmlkZXIiOiJnb29nbGUiLCJyZW1vdGVfaWQiOiIxMDE4OTMzNzc5ODQ3NDg1NDMwMDIiLCJkaXNwbGF5X25hbWUiOiJUcm9uZyBOZ2hpYSBOZ3V5ZW4iLCJfX21ldGFkYXRhIjp7InVyaSI6Ii9ld2Evc29jaWFsX3NlcnZpY2VfYWNjb3VudCgxMTY2KSIsInR5cGUiOiIifX1dLCJoYXNfZGlzYWJsZWRfbmV3c2xldHRlciI6ZmFsc2UsImp3dF9zZWNyZXQiOiI0UDVTQzZGV1pIVU5JR0NDT1dJQUtST0tST0RMUTRNVSIsImhhc1Bhc3N3b3JkU2V0Ijp0cnVlLCJuZWVkc1Bhc3N3b3JkUmVzZXQiOmZhbHNlLCJwdWJsaWNfa2V5Ijp0cnVlLCJmZWF0dXJlcyI6W10sImludGVyY29tVXNlck5hbWUiOiJnX3Ryb25nX25naGlhX25ndXllbiIsImludGVyY29tVXNlckhhc2giOiI5YTM0NmUwZTgzNjk0MzYxODU3MTdjNWRhZTZkZWZhZDdiYmM4YzZkOGNlMzgxYjhhYTY5YWRjMTRhYWZiNGU0IiwicGVybWlzc2lvbnMiOltdLCJpYXQiOjE0NDgyNTY2ODMsImV4cCI6MTQ0ODg2MTQ4M30.oqq4DUI4cTbhzYznSwODZ_4zLOeGiJYuZRn82gTfQ6o'
Function: get_user_actor_id()
Get current logged in user's actor id.
Returns:
int: User actor id
Examples:
# If you are logged in.
>>> balena.auth.get_user_actor_id()
9761945
Function: get_user_id()
This function retrieves current logged in user's id.
Returns:
str: user id.
Raises:
InvalidOption: if not logged in.
Examples:
# If you are logged in.
>>> balena.auth.get_user_id()
5397
Function: is_logged_in()
This function checks if you're logged in
Returns:
bool: True if logged in, False otherwise.
Examples:
# Check if user logged in.
>>> if balena.auth.is_logged_in():
... print('You are logged in!')
... else:
... print('You are not logged in!')
Function: log_out()
This function is used for logging out from balena.
Returns:
bool: True if successful, False otherwise.
Examples:
# If you are logged in.
>>> balena.auth.log_out()
True
Function: login()
This function is used for logging into balena using email and password.
Args:
**credentials: credentials keyword arguments.
username (str): Balena email.
password (str): Password.
Returns:
This functions saves Auth Token to Settings and returns nothing.
Raises:
LoginFailed: if the email or password is invalid.
Examples:
>>> from balena import Balena
>>> balena = Balena()
>>> credentials = {'username': '<your email>', 'password': '<your password>'}
>>> balena.auth.login(**credentials)
(Empty Return)
Function: login_with_token(token)
This function is used for logging into balena using Auth Token. Auth Token can be found in Preferences section on balena Dashboard.
Args:
token (str): Auth Token.
Returns:
This functions saves Auth Token to Settings and returns nothing.
Raises:
MalformedToken: if token is invalid.
Examples:
>>> from balena import Balena
>>> balena = Balena()
>>> auth_token = <your token>
>>> balena.auth.login_with_token(auth_token)
(Empty Return)
Function: register()
This function is used for registering to balena.
Args:
**credentials: credentials keyword arguments.
email (str): email to register.
password (str): Password.
Returns:
str: Auth Token for new account.
Raises:
RequestError: if error occurs during registration.
Examples:
>>> credentials = {'email': '<your email>', 'password': '<your password>'}
>>> balena.auth.register(**credentials)
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NTM5OCwidXNlcm5hbWUiOiJ0ZXN0MjcxMCIsImVtYWlsIjoidGVzdDI3MTBAZ21haWwuY29tIiwic29jaWFsX3NlcnZpY2VfYWNjb3VudCI6bnVsbCwiaGFzX2Rpc2FibGVkX25ld3NsZXR0ZXIiOmZhbHNlLCJqd3Rfc2VjcmV0IjoiQlJXR0ZIVUgzNVBKT0VKTVRSSVo2MjdINjVKVkJKWDYiLCJoYXNQYXNzd29yZFNldCI6dHJ1ZSwibmVlZHNQYXNzd29yZFJlc2V0IjpmYWxzZSwicHVibGljX2tleSI6ZmFsc2UsImZlYXR1cmVzIjpbXSwiaW50ZXJjb21Vc2VyTmFtZSI6InRlc3QyNzEwIiwiaW50ZXJjb21Vc2VySGFzaCI6IjNiYTRhZDRkZjk4MDQ1OTc1YmU2ZGUwYWJmNjFiYjRmYWY4ZmEzYTljZWI0YzE4Y2QxOGU1NmViNmI1NzkxZDAiLCJwZXJtaXNzaW9ucyI6W10sImlhdCI6MTQ0ODI1NzgyOCwiZXhwIjoxNDQ4ODYyNjI4fQ.chhf6deZ9BNDMmPr1Hm-SlRoWkK7t_4cktAPo12aCoE'
Function: who_am_i()
This function retrieves username of logged in user.
Returns:
str: username.
Raises:
NotLoggedIn: if there is no user logged in.
Examples:
>>> balena.auth.who_am_i()
u'g_trong_nghia_nguyen'
Logs
This class implements functions that allow processing logs from device.
Function: history(uuid, count)
Get device logs history.
Args:
uuid (str): device uuid.
count (Optional[int]): number of historical messages to include.
Function: subscribe(uuid, callback, error, count)
Subscribe to device logs.
Args:
uuid (str): device uuid.
callback (function): this callback is called on receiving a message.
error (Optional[function]): this callback is called on an error event.
count (Optional[int]): number of historical messages to include.
Returns:
dict: a log entry will contain the following keys: `isStdErr, timestamp, message, isSystem, createdAt`.
Function: unsubscribe(uuid)
Unsubscribe from device logs for a specific device.
Args:
uuid (str): device uuid.
Function: unsubscribe_all()
Unsubscribe all subscribed devices.
Settings
This class handles settings for balena python SDK.
Attributes:
HOME_DIRECTORY (str): home directory path.
CONFIG_SECTION (str): section name in configuration file.
CONFIG_FILENAME (str): configuration file name.
_setting (dict): default value to settings.
Function: get(key)
Get a setting value.
Args:
key (str): setting.
Returns:
str: setting value.
Raises:
InvalidOption: If getting a non-existent setting.
Examples:
>>> balena.settings.get('api_endpoint')
'https://api.balena-cloud.com/'
Function: get_all()
Get all settings.
Returns:
dict: all settings.
Examples:
>>> balena.settings.get_all()
{
"image_cache_time": "604800000",
"api_endpoint": "https://api.balena-cloud.com/",
"data_directory": "/root/.balena",
"token_refresh_interval": "3600000",
"cache_directory": "/root/.balena/cache",
"pine_endpoint": "https://api.balena-cloud.com/ewa/",
}
Function: has(key)
Check if a setting exists.
Args:
key (str): setting.
Returns:
bool: True if exists, False otherwise.
Examples:
>>> balena.settings.has('api_endpoint')
True
Function: remove(key)
Remove a setting.
Args:
key (str): setting.
Returns:
bool: True if successful, False otherwise.
Examples:
# Remove an existing key from settings
>>> balena.settings.remove('tmp')
True
# Remove a non-existing key from settings
>>> balena.settings.remove('tmp1')
False
Function: set(key, value)
Set value for a setting.
Args:
key (str): setting.
value (str): setting value.
Examples:
>>> balena.settings.set(key='tmp',value='123456')
(Empty Return)
TwoFactorAuth
This class implements basic 2FA functionalities for balena python SDK.
Function: challenge(code)
Challenge two-factor authentication. If your account has two-factor authentication enabled and logging in using credentials, you need to pass two-factor authentication before being allowed to use other functions.
Args:
code (str): two-factor authentication code.
Examples:
# You need to enable two-factor authentication on dashboard first.
# Check if two-factor authentication is passed for current session.
>>> balena.twofactor_auth.is_passed()
False
>>> secret = balena.twofactor_auth.get_setup_key()
>>> balena.twofactor_auth.challenge(balena.twofactor_auth.generate_code(secret))
# Check again if two-factor authentication is passed for current session.
>>> balena.twofactor_auth.is_passed()
True
Function: disable(password)
Disable two factor authentication.
Args:
password (str): password.
Returns:
str: session token.
Examples:
>>> balena.twofactor_auth.verify('')
''
Function: enable(code)
Enable two factor authentication.
Args:
code (str): two-factor authentication code.
Returns:
str: session token.
Examples:
>>> balena.twofactor_auth.enable('')
''
Function: generate_code(secret)
Generate two-factor authentication code.
Args:
secret (str): one time password authentication secret string.
Returns:
str: 6 digit two-factor authentication code.
Examples:
>>> secret = balena.twofactor_auth.get_setup_key()
>>> balena.twofactor_auth.generate_code(secret)
'259975'
Function: get_setup_key()
Retrieves a setup key for enabling two factor authentication. This function only works if you disable two-factor authentication or log in using Auth Token from dashboard.
Returns:
str: setup key.
Examples:
>>> balena.twofactor_auth.get_setup_key()
'WGURB3DIUWXTGQDBGFNGKDLV2L3LXOVN'
Function: is_enabled()
Check if two-factor authentication is enabled.
Returns:
bool: True if enabled. Otherwise False.
Examples:
>>> balena.twofactor_auth.is_enabled()
False
Function: is_passed()
Check if two-factor authentication challenge was passed.
Returns:
bool: True if enabled. Otherwise False.
Examples:
>>> balena.twofactor_auth.is_passed()
True
Function: verify(code)
Verifies two factor authentication. Note that this method not update the token automatically. You should use balena.twofactor_auth.challenge() when possible, as it takes care of that as well.
Args:
code (str): two-factor authentication code.
Returns:
str: session token.
Examples:
>>> balena.twofactor_auth.verify('123456')
''