From 14625a214a548e406eac778440bf7634454808f4 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 5 Aug 2023 20:52:55 +0200 Subject: [PATCH] [PR #6814/47865284 backport][stable-7] Adding DeleteVolumes functionality (#7066) Adding DeleteVolumes functionality (#6814) * Adding DeleteAllVolumes functionality * Adding changelog fragment and sanity fix * Sanity Fix * Updating as per PR suggestions * Sanity fix * Adjust version_added. --------- Co-authored-by: Kushal Co-authored-by: Felix Fontein (cherry picked from commit 478652843fd52d1fee809c41a1a61ad31525e67c) Co-authored-by: TSKushal <44438079+TSKushal@users.noreply.github.com> --- ...-redfish-config-add-delete-all-volumes.yml | 2 + plugins/module_utils/redfish_utils.py | 62 +++++++++++++++++++ plugins/modules/redfish_config.py | 36 ++++++++++- 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/6814-redfish-config-add-delete-all-volumes.yml diff --git a/changelogs/fragments/6814-redfish-config-add-delete-all-volumes.yml b/changelogs/fragments/6814-redfish-config-add-delete-all-volumes.yml new file mode 100644 index 0000000000..8a2214d09a --- /dev/null +++ b/changelogs/fragments/6814-redfish-config-add-delete-all-volumes.yml @@ -0,0 +1,2 @@ +minor_changes: + - redfish_config - add ``DeleteAllVolumes`` command to allow deletion of all volumes on servers (https://github.com/ansible-collections/community.general/pull/6814). diff --git a/plugins/module_utils/redfish_utils.py b/plugins/module_utils/redfish_utils.py index c1542f3420..48e6f1272e 100644 --- a/plugins/module_utils/redfish_utils.py +++ b/plugins/module_utils/redfish_utils.py @@ -3411,3 +3411,65 @@ class RedfishUtils(object): fan_percent_min_config = hpe.get('FanPercentMinimum') result["fan_percent_min"] = fan_percent_min_config return result + + def delete_volumes(self, storage_subsystem_id, volume_ids): + # Find the Storage resource from the requested ComputerSystem resource + response = self.get_request(self.root_uri + self.systems_uri) + if response['ret'] is False: + return response + data = response['data'] + storage_uri = data.get('Storage', {}).get('@odata.id') + if storage_uri is None: + return {'ret': False, 'msg': 'Storage resource not found'} + + # Get Storage Collection + response = self.get_request(self.root_uri + storage_uri) + if response['ret'] is False: + return response + data = response['data'] + + # Collect Storage Subsystems + self.storage_subsystems_uris = [i['@odata.id'] for i in response['data'].get('Members', [])] + if not self.storage_subsystems_uris: + return { + 'ret': False, + 'msg': "StorageCollection's Members array is either empty or missing"} + + # Matching Storage Subsystem ID with user input + self.storage_subsystem_uri = "" + for storage_subsystem_uri in self.storage_subsystems_uris: + if storage_subsystem_uri.split("/")[-2] == storage_subsystem_id: + self.storage_subsystem_uri = storage_subsystem_uri + + if not self.storage_subsystem_uri: + return { + 'ret': False, + 'msg': "Provided Storage Subsystem ID %s does not exist on the server" % storage_subsystem_id} + + # Get Volume Collection + response = self.get_request(self.root_uri + self.storage_subsystem_uri) + if response['ret'] is False: + return response + data = response['data'] + + response = self.get_request(self.root_uri + data['Volumes']['@odata.id']) + if response['ret'] is False: + return response + data = response['data'] + + # Collect Volumes + self.volume_uris = [i['@odata.id'] for i in response['data'].get('Members', [])] + if not self.volume_uris: + return { + 'ret': True, 'changed': False, + 'msg': "VolumeCollection's Members array is either empty or missing"} + + # Delete each volume + for volume in self.volume_uris: + if volume.split("/")[-1] in volume_ids: + response = self.delete_request(self.root_uri + volume) + if response['ret'] is False: + return response + + return {'ret': True, 'changed': True, + 'msg': "The following volumes were deleted: %s" % str(volume_ids)} diff --git a/plugins/modules/redfish_config.py b/plugins/modules/redfish_config.py index 9f31870e32..c17dabe0e7 100644 --- a/plugins/modules/redfish_config.py +++ b/plugins/modules/redfish_config.py @@ -130,7 +130,21 @@ options: type: dict default: {} version_added: '5.7.0' - + storage_subsystem_id: + required: false + description: + - Id of the Storage Subsystem on which the volume is to be created. + type: str + default: '' + version_added: '7.3.0' + volume_ids: + required: false + description: + - List of IDs of volumes to be deleted. + type: list + default: [] + elements: str + version_added: '7.3.0' author: - "Jose Delarosa (@jose-delarosa)" - "T S Kushal (@TSKushal)" @@ -272,6 +286,16 @@ EXAMPLES = ''' baseuri: "{{ baseuri }}" username: "{{ username }}" password: "{{ password }}" + + - name: Delete All Volumes + community.general.redfish_config: + category: Systems + command: DeleteVolumes + baseuri: "{{ baseuri }}" + username: "{{ username }}" + password: "{{ password }}" + storage_subsystem_id: "DExxxxxx" + volume_ids: ["volume1", "volume2"] ''' RETURN = ''' @@ -290,7 +314,7 @@ from ansible.module_utils.common.text.converters import to_native # More will be added as module features are expanded CATEGORY_COMMANDS_ALL = { "Systems": ["SetBiosDefaultSettings", "SetBiosAttributes", "SetBootOrder", - "SetDefaultBootOrder", "EnableSecureBoot"], + "SetDefaultBootOrder", "EnableSecureBoot", "DeleteVolumes"], "Manager": ["SetNetworkProtocols", "SetManagerNic", "SetHostInterface"], "Sessions": ["SetSessionService"], } @@ -323,6 +347,8 @@ def main(): hostinterface_config=dict(type='dict', default={}), hostinterface_id=dict(), sessions_config=dict(type='dict', default={}), + storage_subsystem_id=dict(type='str', default=''), + volume_ids=dict(type='list', default=[], elements='str') ), required_together=[ ('username', 'password'), @@ -372,6 +398,10 @@ def main(): # Sessions config options sessions_config = module.params['sessions_config'] + # Volume deletion options + storage_subsystem_id = module.params['storage_subsystem_id'] + volume_ids = module.params['volume_ids'] + # Build root URI root_uri = "https://" + module.params['baseuri'] rf_utils = RedfishUtils(creds, root_uri, timeout, module, @@ -405,6 +435,8 @@ def main(): result = rf_utils.set_default_boot_order() elif command == "EnableSecureBoot": result = rf_utils.enable_secure_boot() + elif command == "DeleteVolumes": + result = rf_utils.delete_volumes(storage_subsystem_id, volume_ids) elif category == "Manager": # execute only if we find a Manager service resource