From 6ca3cedeb4f8b63053aca9b24328aa83db5e4599 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Mon, 22 Nov 2021 17:18:27 +0100 Subject: [PATCH] Migrate CompositionService, EventService and TaskService to enums Change-Id: Icb6e67f90c148f29477a9b373235529fa90983fe --- .../resources/compositionservice/constants.py | 89 +++++++++++++++---- .../resources/compositionservice/mappings.py | 40 --------- .../compositionservice/resourceblock.py | 6 +- sushy/resources/eventservice/constants.py | 45 ++++++---- sushy/resources/eventservice/eventservice.py | 9 +- sushy/resources/eventservice/mappings.py | 24 ----- sushy/resources/taskservice/constants.py | 15 +++- sushy/resources/taskservice/mappings.py | 25 ------ sushy/resources/taskservice/taskservice.py | 4 +- .../compositionservice/test_resourceblock.py | 4 +- .../eventservice/test_eventservice.py | 24 ++--- .../resources/taskservice/test_taskservice.py | 2 +- 12 files changed, 140 insertions(+), 147 deletions(-) delete mode 100644 sushy/resources/compositionservice/mappings.py delete mode 100644 sushy/resources/eventservice/mappings.py delete mode 100644 sushy/resources/taskservice/mappings.py diff --git a/sushy/resources/compositionservice/constants.py b/sushy/resources/compositionservice/constants.py index ad128592..790e518c 100644 --- a/sushy/resources/compositionservice/constants.py +++ b/sushy/resources/compositionservice/constants.py @@ -13,19 +13,78 @@ # Values come from the Redfish ResourceBlock json-schema. # https://redfish.dmtf.org/schemas/ResourceBlock.v1_3_0.json -# Composition state related constants -COMPOSITION_STATE_COMPOSING = 'Composing' -COMPOSITION_STATE_COMPOSED_AND_AVAILABLE = 'ComposedAndAvailable' -COMPOSITION_STATE_COMPOSED = 'Composed' -COMPOSITION_STATE_UNUSED = 'Unused' -COMPOSITION_STATE_FAILED = 'Failed' -COMPOSITION_STATE_UNAVAILABLE = 'Unavailable' +import enum -# Resource Block type related constants -RESOURCE_BLOCK_TYPE_COMPUTE = 'Compute' -RESOURCE_BLOCK_TYPE_PROCESSOR = 'Processor' -RESOURCE_BLOCK_TYPE_MEMORY = 'Memory' -RESOURCE_BLOCK_TYPE_NETWORK = 'Network' -RESOURCE_BLOCK_TYPE_STORAGE = 'Storage' -RESOURCE_BLOCK_TYPE_COMPUTERSYSTEM = 'ComputerSystem' -RESOURCE_BLOCK_TYPE_EXPANSION = 'Expansion' + +class CompositionState(enum.Enum): + COMPOSING = 'Composing' + """Intermediate state indicating composition is in progress.""" + + COMPOSED_AND_AVAILABLE = 'ComposedAndAvailable' + """Indicates the Resource Block is currently participating in one or + more compositions, and is available to be used in more compositions.""" + + COMPOSED = 'Composed' + """Final successful state of a Resource Block which has participated in + composition.""" + + UNUSED = 'Unused' + """Indicates the Resource Block is free and can participate in + composition.""" + + FAILED = 'Failed' + """The final composition resulted in failure and manual intervention may + be required to fix it.""" + + UNAVAILABLE = 'Unavailable' + """Indicates the Resource Block has been made unavailable by the + service, such as due to maintenance being performed on the Resource + Block.""" + + +# Backward compatibility +COMPOSITION_STATE_COMPOSING = CompositionState.COMPOSING +COMPOSITION_STATE_COMPOSED_AND_AVAILABLE = \ + CompositionState.COMPOSED_AND_AVAILABLE +COMPOSITION_STATE_COMPOSED = CompositionState.COMPOSED +COMPOSITION_STATE_UNUSED = CompositionState.UNUSED +COMPOSITION_STATE_FAILED = CompositionState.FAILED +COMPOSITION_STATE_UNAVAILABLE = CompositionState.UNAVAILABLE + + +class ResourceBlockType(enum.Enum): + COMPUTE = 'Compute' + """This Resource Block contains both Processor and Memory resources in a + manner that creates a compute complex.""" + + PROCESSOR = 'Processor' + """This Resource Block contains Processor resources.""" + + MEMORY = 'Memory' + """This Resource Block contains Memory resources.""" + + NETWORK = 'Network' + """This Resource Block contains Network resources, such as Ethernet + Interfaces.""" + + STORAGE = 'Storage' + """This Resource Block contains Storage resources, such as Storage and + Simple Storage.""" + + COMPUTER_SYSTEM = 'ComputerSystem' + """This Resource Block contains ComputerSystem resources.""" + + EXPANSION = 'Expansion' + """This Resource Block is capable of changing over time based on its + configuration. Different types of devices within this Resource Block + can be added and removed over time.""" + + +# Backward compatibility +RESOURCE_BLOCK_TYPE_COMPUTE = ResourceBlockType.COMPUTE +RESOURCE_BLOCK_TYPE_PROCESSOR = ResourceBlockType.PROCESSOR +RESOURCE_BLOCK_TYPE_MEMORY = ResourceBlockType.MEMORY +RESOURCE_BLOCK_TYPE_NETWORK = ResourceBlockType.NETWORK +RESOURCE_BLOCK_TYPE_STORAGE = ResourceBlockType.STORAGE +RESOURCE_BLOCK_TYPE_COMPUTERSYSTEM = ResourceBlockType.COMPUTER_SYSTEM +RESOURCE_BLOCK_TYPE_EXPANSION = ResourceBlockType.EXPANSION diff --git a/sushy/resources/compositionservice/mappings.py b/sushy/resources/compositionservice/mappings.py deleted file mode 100644 index 5e09f91b..00000000 --- a/sushy/resources/compositionservice/mappings.py +++ /dev/null @@ -1,40 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from sushy.resources.compositionservice import constants as comp_cons -from sushy import utils - - -COMPOSITION_STATE_VALUE_MAP = { - 'Composing': comp_cons.COMPOSITION_STATE_COMPOSING, - 'ComposedAndAvailable': comp_cons.COMPOSITION_STATE_COMPOSED_AND_AVAILABLE, - 'Composed': comp_cons.COMPOSITION_STATE_COMPOSED, - 'Unused': comp_cons.COMPOSITION_STATE_UNUSED, - 'Failed': comp_cons.COMPOSITION_STATE_FAILED, - 'Unavailable': comp_cons.COMPOSITION_STATE_UNAVAILABLE -} - -COMPOSITION_STATE_VALUE_MAP_REV = ( - utils.revert_dictionary(COMPOSITION_STATE_VALUE_MAP)) - -RESOURCE_BLOCK_TYPE_VALUE_MAP = { - 'Compute': comp_cons.RESOURCE_BLOCK_TYPE_COMPUTE, - 'Processor': comp_cons.RESOURCE_BLOCK_TYPE_PROCESSOR, - 'Memory': comp_cons.RESOURCE_BLOCK_TYPE_MEMORY, - 'Network': comp_cons.RESOURCE_BLOCK_TYPE_NETWORK, - 'Storage': comp_cons.RESOURCE_BLOCK_TYPE_STORAGE, - 'ComputerSystem': comp_cons.RESOURCE_BLOCK_TYPE_COMPUTERSYSTEM, - 'Expansion': comp_cons.RESOURCE_BLOCK_TYPE_EXPANSION -} - -RESOURCE_BLOCK_TYPE_VALUE_MAP_REV = ( - utils.revert_dictionary(RESOURCE_BLOCK_TYPE_VALUE_MAP)) diff --git a/sushy/resources/compositionservice/resourceblock.py b/sushy/resources/compositionservice/resourceblock.py index 5b19fd66..684573f7 100644 --- a/sushy/resources/compositionservice/resourceblock.py +++ b/sushy/resources/compositionservice/resourceblock.py @@ -18,7 +18,7 @@ import logging from sushy.resources import base from sushy.resources import common -from sushy.resources.compositionservice import mappings as res_maps +from sushy.resources.compositionservice import constants LOG = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class CompositionStatusField(base.CompositeField): composition_state = base.MappedField( 'CompositionState', - res_maps.COMPOSITION_STATE_VALUE_MAP, + constants.CompositionState, required=True) """Inform the client, state of the resource block""" @@ -67,7 +67,7 @@ class ResourceBlock(base.ResourceBase): resource_block_type = base.MappedField( 'ResourceBlockType', - res_maps.RESOURCE_BLOCK_TYPE_VALUE_MAP, + constants.ResourceBlockType, required=True) """The type of resource block""" diff --git a/sushy/resources/eventservice/constants.py b/sushy/resources/eventservice/constants.py index a1fe21d7..244e01b9 100644 --- a/sushy/resources/eventservice/constants.py +++ b/sushy/resources/eventservice/constants.py @@ -15,24 +15,39 @@ # http://redfish.dmtf.org/schemas/v1/Event.json#/definitions/EventType # https://redfish.dmtf.org/schemas/v1/EventService.v1_0_6.json -EVENT_TYPE_STATUS_CHANGE = "Status Change" -"""The status of a resource has changed""" +import enum -EVENT_TYPE_RESOURCE_ADDED = "Resource Added" -"""A resource has been added.""" -EVENT_TYPE_RESOURCE_REMOVED = "Resource Removed" -"""A resource has been removed""" +class EventType(enum.Enum): -EVENT_TYPE_RESOURCE_UPDATED = "Resource Updated" -"""A resource has been updated""" + STATUS_CHANGE = 'StatusChange' + """The status of a resource has changed.""" -EVENT_TYPE_ALERT = "Alert" -"""A condition requires attention""" + RESOURCE_UPDATED = 'ResourceUpdated' + """A resource has been updated.""" -EVENT_TYPE_METRIC_REPORT = "Metric Report" -"""The telemetry service is sending a metric report""" + RESOURCE_ADDED = 'ResourceAdded' + """A resource has been added.""" -EVENT_TYPE_OTHER = "Other" -"""Because EventType is deprecated as of Redfish Specification v1.6, -the event is based on a registry or resource but not an EventType.""" + RESOURCE_REMOVED = 'ResourceRemoved' + """A resource has been removed.""" + + ALERT = 'Alert' + """A condition requires attention.""" + + METRIC_REPORT = 'MetricReport' + """The telemetry service is sending a metric report.""" + + OTHER = 'Other' + """Because EventType is deprecated as of Redfish Specification v1.6, the + event is based on a registry or resource but not an EventType.""" + + +# Backward compatibility +EVENT_TYPE_STATUS_CHANGE = EventType.STATUS_CHANGE +EVENT_TYPE_RESOURCE_UPDATED = EventType.RESOURCE_UPDATED +EVENT_TYPE_RESOURCE_ADDED = EventType.RESOURCE_ADDED +EVENT_TYPE_RESOURCE_REMOVED = EventType.RESOURCE_REMOVED +EVENT_TYPE_ALERT = EventType.ALERT +EVENT_TYPE_METRIC_REPORT = EventType.METRIC_REPORT +EVENT_TYPE_OTHER = EventType.OTHER diff --git a/sushy/resources/eventservice/eventservice.py b/sushy/resources/eventservice/eventservice.py index f0327fed..b72bf43a 100644 --- a/sushy/resources/eventservice/eventservice.py +++ b/sushy/resources/eventservice/eventservice.py @@ -20,8 +20,8 @@ import logging from sushy import exceptions from sushy.resources import base from sushy.resources import common +from sushy.resources.eventservice import constants from sushy.resources.eventservice import eventdestination -from sushy.resources.eventservice import mappings as evs_maps LOG = logging.getLogger(__name__) @@ -130,11 +130,10 @@ class EventService(base.ResourceBase): if not self.event_types_for_subscription: LOG.warning('Could not figure out the Event types supported by ' 'the EventService %s', self.identity) - return set(evs_maps.EVENT_TYPE_VALUE_MAP.values()) + return set(constants.EventType) - return set([evs_maps.EVENT_TYPE_VALUE_MAP[v] for v in - set(evs_maps.EVENT_TYPE_VALUE_MAP). - intersection(self.event_types_for_subscription)]) + return {v for v in constants.EventType + if v.value in self.event_types_for_subscription} def _get_subscriptions_collection_path(self): """Helper function to find the EventDestinationCollections path""" diff --git a/sushy/resources/eventservice/mappings.py b/sushy/resources/eventservice/mappings.py deleted file mode 100644 index 8b9fd25e..00000000 --- a/sushy/resources/eventservice/mappings.py +++ /dev/null @@ -1,24 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from sushy.resources.eventservice import constants as evt_cons - - -EVENT_TYPE_VALUE_MAP = { - 'StatusChange': evt_cons.EVENT_TYPE_STATUS_CHANGE, - 'ResourceAdded': evt_cons.EVENT_TYPE_RESOURCE_ADDED, - 'ResourceRemoved': evt_cons.EVENT_TYPE_RESOURCE_REMOVED, - 'ResourceUpdated': evt_cons.EVENT_TYPE_RESOURCE_UPDATED, - 'Alert': evt_cons.EVENT_TYPE_ALERT, - 'MetricReport': evt_cons.EVENT_TYPE_METRIC_REPORT, - 'Other': evt_cons.EVENT_TYPE_OTHER -} diff --git a/sushy/resources/taskservice/constants.py b/sushy/resources/taskservice/constants.py index 899e6c62..cd0449ad 100644 --- a/sushy/resources/taskservice/constants.py +++ b/sushy/resources/taskservice/constants.py @@ -35,7 +35,16 @@ class TaskState(enum.Enum): KILLED = 'Killed' -# Overwrite Policy constants +class OverWritePolicy(enum.Enum): + """Overwrite Policy constants""" -OVERWRITE_POLICY_OLDEST = 'oldest completed' -OVERWRITE_POLICY_MANUAL = 'manual only' + MANUAL = 'Manual' + """Completed tasks are not automatically overwritten.""" + + OLDEST = 'Oldest' + """Oldest completed tasks are overwritten.""" + + +# Backward compatibility +OVERWRITE_POLICY_MANUAL = OverWritePolicy.MANUAL +OVERWRITE_POLICY_OLDEST = OverWritePolicy.OLDEST diff --git a/sushy/resources/taskservice/mappings.py b/sushy/resources/taskservice/mappings.py deleted file mode 100644 index 401f65f5..00000000 --- a/sushy/resources/taskservice/mappings.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright (c) 2020 Dell, Inc. or its subsidiaries -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from sushy.resources.taskservice import constants as ts_cons -from sushy import utils - -OVERWRITE_POLICY_VALUE_MAP = { - 'Oldest': ts_cons.OVERWRITE_POLICY_OLDEST, - 'Manual': ts_cons.OVERWRITE_POLICY_MANUAL, -} - -OVERWRITE_POLICY_VALUE_MAP_REV = ( - utils.revert_dictionary(OVERWRITE_POLICY_VALUE_MAP)) diff --git a/sushy/resources/taskservice/taskservice.py b/sushy/resources/taskservice/taskservice.py index c16f216b..08166e05 100644 --- a/sushy/resources/taskservice/taskservice.py +++ b/sushy/resources/taskservice/taskservice.py @@ -18,7 +18,7 @@ import logging from sushy.resources import base from sushy.resources import common -from sushy.resources.taskservice import mappings as ts_maps +from sushy.resources.taskservice import constants as ts_cons from sushy.resources.taskservice import task from sushy import utils @@ -40,7 +40,7 @@ class TaskService(base.ResourceBase): """The status of the task service""" overwrite_policy = base.MappedField( - 'CompletedTaskOverWritePolicy', ts_maps.OVERWRITE_POLICY_VALUE_MAP) + 'CompletedTaskOverWritePolicy', ts_cons.OverWritePolicy) """The overwrite policy for completed tasks""" event_on_task_state_change = base.Field( diff --git a/sushy/tests/unit/resources/compositionservice/test_resourceblock.py b/sushy/tests/unit/resources/compositionservice/test_resourceblock.py index 6abf6703..653f536e 100644 --- a/sushy/tests/unit/resources/compositionservice/test_resourceblock.py +++ b/sushy/tests/unit/resources/compositionservice/test_resourceblock.py @@ -39,7 +39,7 @@ class ResourceBlockTestCase(base.TestCase): def test__parse_attributes(self): self.res_block._parse_attributes(self.json_doc) self.assertEqual( - res_block_cons.COMPOSITION_STATE_COMPOSED, + res_block_cons.CompositionState.COMPOSED, self.res_block.composition_status.composition_state) self.assertEqual(1, self.res_block.composition_status.max_compositions) self.assertEqual( @@ -51,7 +51,7 @@ class ResourceBlockTestCase(base.TestCase): self.assertEqual('DriveBlock3', self.res_block.identity) self.assertEqual('Drive Block 3', self.res_block.name) self.assertEqual( - res_block_cons.RESOURCE_BLOCK_TYPE_STORAGE, + res_block_cons.ResourceBlockType.STORAGE, self.res_block.resource_block_type) self.assertEqual( res_cons.State.ENABLED, diff --git a/sushy/tests/unit/resources/eventservice/test_eventservice.py b/sushy/tests/unit/resources/eventservice/test_eventservice.py index 71013433..795ea63b 100644 --- a/sushy/tests/unit/resources/eventservice/test_eventservice.py +++ b/sushy/tests/unit/resources/eventservice/test_eventservice.py @@ -51,24 +51,24 @@ class EventServiceTestCase(base.TestCase): '/redfish/v1/EventService/Subscriptions/') def test__get_event_types_for_subscription(self): - expected = set([sushy.EVENT_TYPE_STATUS_CHANGE, - sushy.EVENT_TYPE_RESOURCE_ADDED, - sushy.EVENT_TYPE_RESOURCE_REMOVED, - sushy.EVENT_TYPE_RESOURCE_UPDATED, - sushy.EVENT_TYPE_ALERT]) + expected = set([sushy.EventType.STATUS_CHANGE, + sushy.EventType.RESOURCE_ADDED, + sushy.EventType.RESOURCE_REMOVED, + sushy.EventType.RESOURCE_UPDATED, + sushy.EventType.ALERT]) values = self.eventservice.get_event_types_for_subscription() self.assertEqual(expected, values) self.assertIsInstance(values, set) def test__no_event_types_for_subscription(self): - expected = set([sushy.EVENT_TYPE_STATUS_CHANGE, - sushy.EVENT_TYPE_RESOURCE_ADDED, - sushy.EVENT_TYPE_RESOURCE_REMOVED, - sushy.EVENT_TYPE_RESOURCE_UPDATED, - sushy.EVENT_TYPE_ALERT, - sushy.EVENT_TYPE_METRIC_REPORT, - sushy.EVENT_TYPE_OTHER]) + expected = set([sushy.EventType.STATUS_CHANGE, + sushy.EventType.RESOURCE_ADDED, + sushy.EventType.RESOURCE_REMOVED, + sushy.EventType.RESOURCE_UPDATED, + sushy.EventType.ALERT, + sushy.EventType.METRIC_REPORT, + sushy.EventType.OTHER]) self.eventservice.event_types_for_subscription = [] values = self.eventservice.get_event_types_for_subscription() diff --git a/sushy/tests/unit/resources/taskservice/test_taskservice.py b/sushy/tests/unit/resources/taskservice/test_taskservice.py index 23c7be2d..b9cda72a 100644 --- a/sushy/tests/unit/resources/taskservice/test_taskservice.py +++ b/sushy/tests/unit/resources/taskservice/test_taskservice.py @@ -43,7 +43,7 @@ class TaskServiceTestCase(base.TestCase): self.assertEqual(res_cons.State.ENABLED, self.tsk_serv.status.state) self.assertEqual(res_cons.Health.OK, self.tsk_serv.status.health) self.assertEqual(self.tsk_serv.overwrite_policy, - ts_cons.OVERWRITE_POLICY_MANUAL) + ts_cons.OverWritePolicy.MANUAL) @mock.patch.object(task, 'TaskCollection', autospec=True) def test_tasks(self, task_collection_mock):