-
Notifications
You must be signed in to change notification settings - Fork 1
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
CASMCMS-9225: Move HSM client to new paradigm #405
Open
mharding-hpe
wants to merge
1
commit into
casmcms-9225-09-ims-client
Choose a base branch
from
casmcms-9225-10-hsm-client
base: casmcms-9225-09-ims-client
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contansts for api version and "hsm"?