-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support integration ids in all operations
- Loading branch information
Showing
10 changed files
with
232 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from typing import List | ||
from typing import Set | ||
|
||
from httpx import HTTPStatusError | ||
|
||
from armis_sdk.core.armis_error import ArmisError | ||
from armis_sdk.core.armis_error import ResponseError | ||
from armis_sdk.core.base_entity_client import BaseEntityClient | ||
from armis_sdk.entities.site import Site | ||
|
||
|
||
class SiteIntegrationsClient( | ||
BaseEntityClient | ||
): # pylint: disable=too-few-public-methods | ||
""" | ||
A client for interacting with a site's integrations. | ||
The primary entity for this client is [Site][armis_sdk.entities.site.Site]. | ||
""" | ||
|
||
async def update(self, site: Site): | ||
"""Update a site's integrations. | ||
Args: | ||
site: The site to update. | ||
Raises: | ||
ResponseError: If an error occurs while communicating with the API. | ||
ArmisError: If `site.integration_ids` is not set. | ||
Example: | ||
```python linenums="1" hl_lines="9" | ||
import asyncio | ||
from armis_sdk.clients.site_integrations_client import SiteIntegrationsClient | ||
async def main(): | ||
site_integrations_client = SiteIntegrationsClient() | ||
site = Site(id=1, integration_ids=[1, 2, 3]) | ||
await site_integrations_client.update(site) | ||
asyncio.run(main()) | ||
``` | ||
""" | ||
|
||
if site.integration_ids is None: | ||
raise ArmisError("The property 'integration_ids' must be set.") | ||
|
||
new_ids = set(site.integration_ids) | ||
current_ids = set(await self._list(site.id)) | ||
|
||
await self._insert(site.id, new_ids - current_ids) | ||
await self._delete(site.id, current_ids - new_ids) | ||
|
||
async def _delete(self, site_id: int, integration_ids: Set[int]): | ||
if not integration_ids: | ||
return | ||
|
||
errors = [] | ||
async with self._armis_client.client() as client: | ||
for integration_id in integration_ids: | ||
response = await client.delete( | ||
f"/api/v1/sites/{site_id}/integrations-ids/{integration_id}/" | ||
) | ||
try: | ||
response.raise_for_status() | ||
except HTTPStatusError as error: | ||
errors.append(error) | ||
|
||
if errors: | ||
raise ResponseError( | ||
"Error while deleting integration ids " | ||
f"{integration_ids!r} from site {site_id!r} ", | ||
response_errors=errors, | ||
) | ||
|
||
async def _insert(self, site_id: int, integration_ids: Set[int]): | ||
if not integration_ids: | ||
return | ||
|
||
errors = [] | ||
async with self._armis_client.client() as client: | ||
for integration_id in integration_ids: | ||
response = await client.post( | ||
f"/api/v1/sites/{site_id}/integrations-ids/", | ||
json={"integrationId": integration_id}, | ||
) | ||
try: | ||
response.raise_for_status() | ||
except HTTPStatusError as error: | ||
errors.append(error) | ||
|
||
if errors: | ||
raise ResponseError( | ||
"Error while inserting integration ids " | ||
f"{integration_ids!r} to site {site_id!r} ", | ||
response_errors=errors, | ||
) | ||
|
||
async def _list(self, site_id: int) -> List[int]: | ||
async with self._armis_client.client() as client: | ||
response = await client.get(f"/api/v1/sites/{site_id}/integrations-ids/") | ||
data = self._get_data(response) | ||
return data["integrationIds"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
::: armis_sdk.clients.site_integrations_client.SiteIntegrationsClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
import pytest_httpx | ||
|
||
from armis_sdk.clients.site_integrations_client import SiteIntegrationsClient | ||
from armis_sdk.core.armis_error import ArmisError | ||
from armis_sdk.entities.site import Site | ||
|
||
pytest_plugins = ["tests.plugins.setup_plugin"] | ||
|
||
|
||
@pytest.mark.usefixtures("setup_env_variables", "authorized") | ||
async def test_update(httpx_mock: pytest_httpx.HTTPXMock): | ||
# List current ids | ||
httpx_mock.add_response( | ||
url="https://mock_tenant.armis.com/api/v1/sites/1/integrations-ids/", | ||
method="GET", | ||
json={"data": {"integrationIds": [1, 2, 3, 4]}}, | ||
) | ||
|
||
# Add new ids | ||
httpx_mock.add_response( | ||
url="https://mock_tenant.armis.com/api/v1/sites/1/integrations-ids/", | ||
method="POST", | ||
match_json={"integrationId": 5}, | ||
) | ||
httpx_mock.add_response( | ||
url="https://mock_tenant.armis.com/api/v1/sites/1/integrations-ids/", | ||
method="POST", | ||
match_json={"integrationId": 6}, | ||
) | ||
|
||
# Remove old ids | ||
httpx_mock.add_response( | ||
url="https://mock_tenant.armis.com/api/v1/sites/1/integrations-ids/1/", | ||
method="DELETE", | ||
) | ||
httpx_mock.add_response( | ||
url="https://mock_tenant.armis.com/api/v1/sites/1/integrations-ids/3/", | ||
method="DELETE", | ||
) | ||
|
||
site_integrations_client = SiteIntegrationsClient() | ||
|
||
site = Site(id=1, name="mock_site", integration_ids=[2, 4, 5, 6]) | ||
|
||
await site_integrations_client.update(site) | ||
|
||
|
||
@pytest.mark.usefixtures("setup_env_variables") | ||
async def test_update_with_no_integrations_set(): | ||
site = Site(id=1, name="mock_site") | ||
site_integrations_client = SiteIntegrationsClient() | ||
|
||
with pytest.raises(ArmisError, match="The property 'integration_ids' must be set."): | ||
await site_integrations_client.update(site) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters