-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CASMCMS-9225: Add paging to component list requests; use context mana…
…gers for all requests
- Loading branch information
1 parent
f692270
commit ab78213
Showing
97 changed files
with
3,535 additions
and
1,919 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ results | |
.project | ||
.settings | ||
.version | ||
.venv | ||
.idea | ||
.pydevproject | ||
ansible/bos_deploy.retry | ||
.tox | ||
|
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
Empty file.
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,62 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a | ||
# copy of this software and associated documentation files (the "Software"), | ||
# to deal in the Software without restriction, including without limitation | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
# and/or sell copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
from abc import ABC | ||
from typing import Type, TypeVar | ||
|
||
from bos.common.clients.endpoints import BaseGenericEndpoint | ||
from bos.common.utils import RetrySessionManager | ||
|
||
ClientEndpoint = TypeVar('ClientEndpoint', bound=BaseGenericEndpoint) | ||
|
||
|
||
class APIClient(RetrySessionManager, ABC): | ||
""" | ||
As a subclass of RetrySessionManager, this class can be used as a context manager, | ||
and will have a requests session available as self.requests_session | ||
This context manager is used to provide API endpoints, via subclassing. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._endpoint_values: dict[Type[ClientEndpoint], ClientEndpoint] = {} | ||
|
||
def get_endpoint(self, | ||
endpoint_type: Type[ClientEndpoint]) -> ClientEndpoint: | ||
""" | ||
Endpoints are created only as needed, and passed the manager retry session. | ||
""" | ||
if endpoint_type not in self._endpoint_values: | ||
self._endpoint_values[endpoint_type] = endpoint_type( | ||
self.requests_session) | ||
return self._endpoint_values[endpoint_type] | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb) -> bool | None: | ||
""" | ||
The only cleanup we need to do when exiting the context manager is to clear out | ||
our list of API clients. Our call to super().__exit__ will take care of closing | ||
out the underlying request session. | ||
""" | ||
self._endpoint_values.clear() | ||
return super().__exit__(exc_type, exc_val, exc_tb) |
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,25 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a | ||
# copy of this software and associated documentation files (the "Software"), | ||
# to deal in the Software without restriction, including without limitation | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
# and/or sell copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
|
||
from .client import BOSClient |
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,121 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a | ||
# copy of this software and associated documentation files (the "Software"), | ||
# to deal in the Software without restriction, including without limitation | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
# and/or sell copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
from abc import ABC | ||
import logging | ||
|
||
from bos.common.clients.endpoints import BaseEndpoint | ||
from bos.common.tenant_utils import get_new_tenant_header | ||
from bos.common.utils import PROTOCOL | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
API_VERSION = 'v2' | ||
SERVICE_NAME = 'cray-bos' | ||
BASE_BOS_ENDPOINT = f"{PROTOCOL}://{SERVICE_NAME}/{API_VERSION}" | ||
|
||
|
||
class BaseBosEndpoint(BaseEndpoint, ABC): | ||
""" | ||
This base class provides generic access to the BOS API. | ||
The individual endpoint needs to be overridden for a specific endpoint. | ||
""" | ||
BASE_ENDPOINT = BASE_BOS_ENDPOINT | ||
|
||
|
||
class BaseBosNonTenantAwareEndpoint(BaseBosEndpoint, ABC): | ||
""" | ||
This base class provides generic access to the BOS API for non-tenant-aware endpoints | ||
The individual endpoint needs to be overridden for a specific endpoint. | ||
""" | ||
|
||
def get_item(self, item_id): | ||
"""Get information for a single BOS item""" | ||
return self.get(uri=item_id) | ||
|
||
def get_items(self, **kwargs): | ||
"""Get information for all BOS items""" | ||
return self.get(params=kwargs) | ||
|
||
def update_item(self, item_id, data): | ||
"""Update information for a single BOS item""" | ||
return self.patch(uri=item_id, json=data) | ||
|
||
def update_items(self, data): | ||
"""Update information for multiple BOS items""" | ||
return self.patch(json=data) | ||
|
||
def put_items(self, data): | ||
"""Put information for multiple BOS Items""" | ||
return self.put(json=data) | ||
|
||
def delete_items(self, **kwargs): | ||
"""Delete information for multiple BOS items""" | ||
return self.delete(params=kwargs) | ||
|
||
|
||
class BaseBosTenantAwareEndpoint(BaseBosEndpoint, ABC): | ||
""" | ||
This base class provides generic access to the BOS API for tenant aware endpoints. | ||
The individual endpoint needs to be overridden for a specific endpoint. | ||
""" | ||
|
||
def get_item(self, item_id, tenant): | ||
"""Get information for a single BOS item""" | ||
return self.get(uri=item_id, headers=get_new_tenant_header(tenant)) | ||
|
||
def get_items(self, **kwargs): | ||
"""Get information for all BOS items""" | ||
headers = None | ||
if "tenant" in kwargs: | ||
tenant = kwargs.pop("tenant") | ||
headers = get_new_tenant_header(tenant) | ||
return self.get(params=kwargs, headers=headers) | ||
|
||
def update_item(self, item_id, tenant, data): | ||
"""Update information for a single BOS item""" | ||
return self.patch(uri=item_id, | ||
json=data, | ||
headers=get_new_tenant_header(tenant)) | ||
|
||
def update_items(self, tenant, data): | ||
"""Update information for multiple BOS items""" | ||
return self.patch(json=data, headers=get_new_tenant_header(tenant)) | ||
|
||
def post_item(self, item_id, tenant, data=None): | ||
"""Post information for a single BOS item""" | ||
return self.post(uri=item_id, | ||
json=data, | ||
headers=get_new_tenant_header(tenant)) | ||
|
||
def put_items(self, tenant, data): | ||
"""Put information for multiple BOS items""" | ||
return self.put(json=data, headers=get_new_tenant_header(tenant)) | ||
|
||
def delete_items(self, **kwargs): | ||
"""Delete information for multiple BOS items""" | ||
headers = None | ||
if "tenant" in kwargs: | ||
tenant = kwargs.pop("tenant") | ||
headers = get_new_tenant_header(tenant) | ||
return self.delete(params=kwargs, headers=headers) |
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,43 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a | ||
# copy of this software and associated documentation files (the "Software"), | ||
# to deal in the Software without restriction, including without limitation | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
# and/or sell copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
from bos.common.clients.api_client import APIClient | ||
|
||
from .components import ComponentEndpoint | ||
from .sessions import SessionEndpoint | ||
from .session_templates import SessionTemplateEndpoint | ||
|
||
|
||
class BOSClient(APIClient): | ||
|
||
@property | ||
def components(self) -> ComponentEndpoint: | ||
return self.get_endpoint(ComponentEndpoint) | ||
|
||
@property | ||
def sessions(self) -> SessionEndpoint: | ||
return self.get_endpoint(SessionEndpoint) | ||
|
||
@property | ||
def session_templates(self) -> SessionTemplateEndpoint: | ||
return self.get_endpoint(SessionTemplateEndpoint) |
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
Oops, something went wrong.