-
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: Move HSM client to new paradigm
- Loading branch information
1 parent
32a97ec
commit 4e70ca4
Showing
16 changed files
with
439 additions
and
273 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,60 @@ | ||
# | ||
# 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 json import JSONDecodeError | ||
|
||
from requests.exceptions import HTTPError | ||
from requests.exceptions import ConnectionError as RequestsConnectionError | ||
from urllib3.exceptions import MaxRetryError | ||
|
||
from bos.common.clients.endpoints import ApiResponseError, BaseEndpoint, JsonData, RequestsMethod | ||
from bos.common.utils import PROTOCOL | ||
|
||
from .exceptions import HWStateManagerException | ||
|
||
SERVICE_NAME = 'cray-smd' | ||
ENDPOINT = f"{PROTOCOL}://{SERVICE_NAME}/hsm/v2/" | ||
|
||
|
||
class BaseHsmEndpoint(BaseEndpoint, ABC): | ||
""" | ||
This base class provides generic access to the HSM API. | ||
The individual endpoint needs to be overridden for a specific endpoint. | ||
""" | ||
BASE_ENDPOINT = ENDPOINT | ||
|
||
def request(self, | ||
method: RequestsMethod, | ||
/, | ||
*, | ||
uri: str = "", | ||
**kwargs) -> JsonData: | ||
try: | ||
return super().request(method, uri=uri, **kwargs) | ||
except (ApiResponseError, RequestsConnectionError, HTTPError, | ||
JSONDecodeError, MaxRetryError) as err: | ||
raise HWStateManagerException(err) from err | ||
|
||
def list(self, params=None): | ||
return self.get(params=params) |
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 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 .groups import GroupsEndpoint | ||
from .partitions import PartitionsEndpoint | ||
from .state_components import StateComponentsEndpoint | ||
|
||
|
||
class HSMClient(APIClient): | ||
|
||
@property | ||
def groups(self) -> GroupsEndpoint: | ||
return self.get_endpoint(GroupsEndpoint) | ||
|
||
@property | ||
def partitions(self) -> PartitionsEndpoint: | ||
return self.get_endpoint(PartitionsEndpoint) | ||
|
||
@property | ||
def state_components(self) -> StateComponentsEndpoint: | ||
return self.get_endpoint(StateComponentsEndpoint) |
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,33 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
|
||
class HWStateManagerException(Exception): | ||
""" | ||
An error unique to interacting with the HWStateManager service; | ||
should the service be unable to fulfill a given request (timeout, | ||
no components, service 503s, etc.); this exception is raised. It is | ||
intended to be further subclassed for more specific kinds of errors | ||
in the future should they arise. | ||
""" |
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,28 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2021-2022, 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 .base import BaseHsmEndpoint | ||
|
||
|
||
class GroupsEndpoint(BaseHsmEndpoint): | ||
ENDPOINT = 'groups' |
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 @@ | ||
# | ||
# 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 collections import defaultdict | ||
import logging | ||
|
||
from .client import HSMClient | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Inventory: | ||
""" | ||
Inventory handles the generation of a hardware inventory in a similar manner to how the | ||
dynamic inventory is generated for CFS. To reduce the number of calls to HSM, everything is | ||
cached for repeated checks, stored both as overall inventory and separate group types to allow | ||
use in finding BOS's base list of nodes, and lazily loaded to prevent extra calls when no limit | ||
is used. | ||
""" | ||
|
||
def __init__(self, hsm_client: HSMClient, partition=None): | ||
self._partition = partition # Can be specified to limit to roles/components query | ||
self._inventory = None | ||
self._groups = None | ||
self._partitions = None | ||
self._roles = None | ||
self.hsm_client = hsm_client | ||
|
||
@property | ||
def groups(self): | ||
if self._groups is None: | ||
data = self.hsm_client.groups.list() | ||
groups = {} | ||
for group in data: | ||
groups[group['label']] = set( | ||
group.get('members', {}).get('ids', [])) | ||
self._groups = groups | ||
return self._groups | ||
|
||
@property | ||
def partitions(self): | ||
if self._partitions is None: | ||
data = self.hsm_client.partitions.list() | ||
partitions = {} | ||
for partition in data: | ||
partitions[partition['name']] = set( | ||
partition.get('members', {}).get('ids', [])) | ||
self._partitions = partitions | ||
return self._partitions | ||
|
||
@property | ||
def roles(self): | ||
if self._roles is None: | ||
params = {} | ||
if self._partition: | ||
params['partition'] = self._partition | ||
data = self.hsm_client.state_components.list(params=params) | ||
roles = defaultdict(set) | ||
for component in data['Components']: | ||
role = '' | ||
if 'Role' in component: | ||
role = str(component['Role']) | ||
roles[role].add(component['ID']) | ||
if 'SubRole' in component: | ||
subrole = role + '_' + str(component['SubRole']) | ||
roles[subrole].add(component['ID']) | ||
self._roles = roles | ||
return self._roles | ||
|
||
@property | ||
def inventory(self): | ||
if self._inventory is None: | ||
inventory = {} | ||
inventory.update(self.groups) | ||
inventory.update(self.partitions) | ||
inventory.update(self.roles) | ||
self._inventory = inventory | ||
LOGGER.info(self._inventory) | ||
return self._inventory | ||
|
||
def __contains__(self, key): | ||
return key in self.inventory | ||
|
||
def __getitem__(self, key): | ||
return self.inventory[key] |
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,28 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2021-2022, 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 .base import BaseHsmEndpoint | ||
|
||
|
||
class PartitionsEndpoint(BaseHsmEndpoint): | ||
ENDPOINT = 'partitions' |
Oops, something went wrong.