From 435bd91d2e406a227b20ce5f42c858253e0a97c3 Mon Sep 17 00:00:00 2001 From: XuLei Ren Date: Sat, 15 Feb 2020 21:00:55 +0800 Subject: [PATCH] Add new command ClearSessions (#65600) * Add ClearSessions command to clear all active sessions. * Delete trailing whitespaces --- lib/ansible/module_utils/redfish_utils.py | 18 ++++++++++++++++++ .../redfish/redfish_command.py | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/ansible/module_utils/redfish_utils.py b/lib/ansible/module_utils/redfish_utils.py index 9c943946038..b16ddf07bf8 100644 --- a/lib/ansible/module_utils/redfish_utils.py +++ b/lib/ansible/module_utils/redfish_utils.py @@ -1169,6 +1169,24 @@ class RedfishUtils(object): result["entries"] = sessions_results return result + def clear_sessions(self): + response = self.get_request(self.root_uri + self.sessions_uri) + if response['ret'] is False: + return response + data = response['data'] + + # if no active sessions, return as success + if data['Members@odata.count'] == 0: + return {'ret': True, 'changed': False, 'msg': "There is no active sessions"} + + # loop to delete every active session + for session in data[u'Members']: + response = self.delete_request(self.root_uri + session[u'@odata.id']) + if response['ret'] is False: + return response + + return {'ret': True, 'changed': True, 'msg': "Clear all sessions successfully"} + def get_firmware_update_capabilities(self): result = {} response = self.get_request(self.root_uri + self.update_uri) diff --git a/lib/ansible/modules/remote_management/redfish/redfish_command.py b/lib/ansible/modules/remote_management/redfish/redfish_command.py index 97d00b5f846..e490043a2a1 100644 --- a/lib/ansible/modules/remote_management/redfish/redfish_command.py +++ b/lib/ansible/modules/remote_management/redfish/redfish_command.py @@ -294,6 +294,14 @@ EXAMPLES = ''' username: "{{ username }}" password: "{{ password }}" timeout: 20 + + - name: Clear Sessions + redfish_command: + category: Sessions + command: ClearSessions + baseuri: "{{ baseuri }}" + username: "{{ username }}" + password: "{{ password }}" ''' RETURN = ''' @@ -317,6 +325,7 @@ CATEGORY_COMMANDS_ALL = { "Accounts": ["AddUser", "EnableUser", "DeleteUser", "DisableUser", "UpdateUserRole", "UpdateUserPassword", "UpdateUserName", "UpdateAccountServiceProperties"], + "Sessions": ["ClearSessions"], "Manager": ["GracefulRestart", "ClearLogs"], } @@ -433,6 +442,16 @@ def main(): if command in led_commands: result = rf_utils.manage_indicator_led(command) + elif category == "Sessions": + # execute only if we find SessionService resources + resource = rf_utils._find_sessionservice_resource() + if resource['ret'] is False: + module.fail_json(msg=resource['msg']) + + for command in command_list: + if command == "ClearSessions": + result = rf_utils.clear_sessions() + elif category == "Manager": MANAGER_COMMANDS = { "GracefulRestart": rf_utils.restart_manager_gracefully,