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

CASMCMS-9225: Move HSM client to new paradigm #405

Open
wants to merge 1 commit into
base: casmcms-9225-09-ims-client
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# MIT License
#
# (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP
# (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"),
Expand All @@ -21,3 +21,7 @@
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#

from .client import HSMClient
from .exceptions import HWStateManagerException
from .inventory import Inventory
60 changes: 60 additions & 0 deletions src/bos/common/clients/hsm/base.py
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/"

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"?



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)
43 changes: 43 additions & 0 deletions src/bos/common/clients/hsm/client.py
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)
33 changes: 33 additions & 0 deletions src/bos/common/clients/hsm/exceptions.py
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.
"""
28 changes: 28 additions & 0 deletions src/bos/common/clients/hsm/groups.py
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'
105 changes: 105 additions & 0 deletions src/bos/common/clients/hsm/inventory.py
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]
28 changes: 28 additions & 0 deletions src/bos/common/clients/hsm/partitions.py
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'
Loading
Loading