Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application get all by organization #368

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ hesitate to open an issue in GitHub](https://github.com/balena-io/balena-sdk-pyt
- [generate_provisioning_key(slug_or_uuid_or_id, key_name, description, expiry_date)](#application.generate_provisioning_key) ⇒ <code>str</code>
- [get(slug_or_uuid_or_id, options, context)](#application.get) ⇒ [<code>TypeApplication</code>](#typeapplication)
- [get_all(options, context)](#application.get_all) ⇒ [<code>List[TypeApplication]</code>](#typeapplication)
- [get_all_by_organization(org_handle_or_id, options)](#application.get_all_by_organization) ⇒ [<code>List[TypeApplication]</code>](#typeapplication)
- [get_all_directly_accessible(options)](#application.get_all_directly_accessible) ⇒ [<code>List[TypeApplication]</code>](#typeapplication)
- [get_by_name(app_name, options, context)](#application.get_by_name) ⇒ [<code>TypeApplication</code>](#typeapplication)
- [get_by_owner(app_name, owner, options)](#application.get_by_owner) ⇒ [<code>TypeApplication</code>](#typeapplication)
Expand Down Expand Up @@ -443,13 +444,30 @@ Get all applications
context (Optional[str]): extra access filters, None or 'directly_accessible'

#### Returns:
List[APIKeyType]: user API key
List[TypeApplication]: application info.

#### Examples:
```python
>>> balena.models.application.get_all()
```

<a name="application.get_all_by_organization"></a>
### Function: get_all_by_organization(org_handle_or_id, options) ⇒ [<code>List[TypeApplication]</code>](#typeapplication)

Get all applications of an organization.

#### Args:
org_handle_or_id (Union[str, int]): handle or id of the organization.
options (AnyObject): extra pine options to use.

#### Returns:
List[TypeApplication]: application info.

#### Examples:
```python
>>> balena.models.application.get_all_by_organization('myorg')
```

<a name="application.get_all_directly_accessible"></a>
### Function: get_all_directly_accessible(options) ⇒ [<code>List[TypeApplication]</code>](#typeapplication)

Expand Down
40 changes: 39 additions & 1 deletion balena/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from math import isinf
from typing import List, Literal, Optional, Union, cast
from urllib.parse import urljoin
from deprecated import deprecated

from .. import exceptions
from ..balena_auth import request
Expand All @@ -25,6 +26,7 @@
with_supervisor_locked_error,
)
from .device_type import DeviceType
from .organization import Organization


class Application:
Expand All @@ -39,6 +41,7 @@ def __init__(self, pine: PineClient, settings: Settings, load_inner_models=True)

self.__pine = pine
self.__settings = settings
self.__organization = Organization(pine, settings)
self.tags = ApplicationTag(pine, self)
self.config_var = ApplicationConfigVariable(pine, self)
self.env_var = ApplicationEnvVariable(pine, self)
Expand Down Expand Up @@ -154,7 +157,7 @@ def get_all(
context (Optional[str]): extra access filters, None or 'directly_accessible'

Returns:
List[APIKeyType]: user API key
List[TypeApplication]: application info.

Examples:
>>> balena.models.application.get_all()
Expand Down Expand Up @@ -366,6 +369,41 @@ def get_by_name(

return apps[0]

def get_all_by_organization(
self,
org_handle_or_id: Union[str, int],
options: AnyObject = {},
) -> List[TypeApplication]:
"""
Get all applications of an organization.

Args:
org_handle_or_id (Union[str, int]): handle or id of the organization.
options (AnyObject): extra pine options to use.

Returns:
List[TypeApplication]: application info.

Examples:
>>> balena.models.application.get_all_by_organization('myorg')
"""
org = self.__organization.get(org_handle_or_id, {"$select": "id"})

return self.__pine.get(
{
"resource": "application",
"options": merge(
{
"$filter": {
"organization": org["id"],
},
},
options,
),
}
)

@deprecated("get_by_owner will be removed in a future release, use get_all_by_organization instead")
def get_by_owner(self, app_name: str, owner: str, options: AnyObject = {}) -> TypeApplication:
"""
Get a single application using the appname and the handle of the owning organization.
Expand Down
14 changes: 13 additions & 1 deletion tests/functional/models/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class TestApplication(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.helper = TestHelper()
cls.organization_name = cls.helper.default_organization["name"]
cls.balena = cls.helper.balena
cls.org_handle = cls.helper.default_organization["handle"]
cls.org_id = cls.helper.default_organization["id"]
cls.app_slug = f"{cls.org_handle}/FooBar"
cls.helper.wipe_application()

Expand Down Expand Up @@ -60,6 +60,18 @@ def test_05_get(self):

self.assertEqual(self.balena.models.application.get(self.app_slug)["app_name"], "FooBar")

def test_06_get_all_by_organization(self):
with self.assertRaises(self.helper.balena_exceptions.OrganizationNotFound):
self.balena.models.application.get_all_by_organization("OrgNotExist")

self.assertEqual(
self.balena.models.application.get_all_by_organization(self.org_handle)[0]["app_name"], "FooBar"
)

self.assertEqual(
self.balena.models.application.get_all_by_organization(self.org_id)[0]["app_name"], "FooBar"
)

def test_06_get_by_owner(self):
with self.assertRaises(self.helper.balena_exceptions.ApplicationNotFound):
self.balena.models.application.get_by_owner("AppNotExist", self.helper.credentials["user_id"])
Expand Down
Loading