From 4890c69a3dade51da776bec9a4384891ba47a977 Mon Sep 17 00:00:00 2001 From: Jose Delarosa Date: Mon, 1 Apr 2019 14:58:06 -0500 Subject: [PATCH] Add new Redfish facts module to handle Dell-specific OEM extensions (#54360) * Add new Redfish facts module to handle Dell-specific OEM extensions - Extends the existing RedfishUtils class and leverages its methods * Add timeout parameter * Removed version_added for timeout paramater --- .../redfish/idrac_redfish_facts.py | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 lib/ansible/modules/remote_management/redfish/idrac_redfish_facts.py diff --git a/lib/ansible/modules/remote_management/redfish/idrac_redfish_facts.py b/lib/ansible/modules/remote_management/redfish/idrac_redfish_facts.py new file mode 100644 index 00000000000..e86b112193a --- /dev/null +++ b/lib/ansible/modules/remote_management/redfish/idrac_redfish_facts.py @@ -0,0 +1,165 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2019 Dell EMC Inc. +# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'status': ['preview'], + 'supported_by': 'community', + 'metadata_version': '1.1'} + +DOCUMENTATION = ''' +--- +module: idrac_redfish_facts +version_added: "2.8" +short_description: Manages servers through iDRAC using Dell Redfish APIs +description: + - Builds Redfish URIs locally and sends them to remote iDRAC controllers to + get information back. + - For use with Dell iDRAC operations that require Redfish OEM extensions +options: + category: + required: true + description: + - Category to execute on iDRAC controller + command: + required: true + description: + - List of commands to execute on iDRAC controller + baseuri: + required: true + description: + - Base URI of iDRAC controller + username: + required: true + description: + - User for authentication with iDRAC controller + password: + required: true + description: + - Password for authentication with iDRAC controller + timeout: + description: + - Timeout in seconds for URL requests to OOB controller + default: 10 + type: int + +author: "Jose Delarosa (@jose-delarosa)" +''' + +EXAMPLES = ''' + - name: Get Manager attributes with a default of 20 seconds + idrac_redfish_command: + category: Manager + command: GetManagerAttributes + baseuri: "{{ baseuri }}" + username: "{{ username }}" + password: "{{ password }}" + timeout: 20 +''' + +RETURN = ''' +msg: + description: different results depending on task + returned: always + type: dict + sample: List of Manager attributes +''' + +import re +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.redfish_utils import RedfishUtils, HEADERS +from ansible.module_utils._text import to_native + + +class IdracRedfishUtils(RedfishUtils): + + def get_manager_attributes(self): + result = {} + manager_attributes = {} + key = "Attributes" + + response = self.get_request(self.root_uri + self.manager_uri + "/" + key) + if response['ret'] is False: + return response + result['ret'] = True + data = response['data'] + + if key not in data: + return {'ret': False, 'msg': "Key %s not found" % key} + + for attribute in data[key].items(): + manager_attributes[attribute[0]] = attribute[1] + result["entries"] = manager_attributes + return result + + +CATEGORY_COMMANDS_ALL = { + "Manager": ["GetManagerAttributes"] +} + + +def main(): + result = {} + module = AnsibleModule( + argument_spec=dict( + category=dict(required=True), + command=dict(required=True, type='list'), + baseuri=dict(required=True), + username=dict(required=True), + password=dict(required=True, no_log=True), + timeout=dict(type='int', default=10) + ), + supports_check_mode=False + ) + + category = module.params['category'] + command_list = module.params['command'] + + # admin credentials used for authentication + creds = {'user': module.params['username'], + 'pswd': module.params['password']} + + # timeout + timeout = module.params['timeout'] + + # Build root URI + root_uri = "https://" + module.params['baseuri'] + rf_uri = "/redfish/v1/" + rf_utils = IdracRedfishUtils(creds, root_uri, timeout) + + # Check that Category is valid + if category not in CATEGORY_COMMANDS_ALL: + module.fail_json(msg=to_native("Invalid Category '%s'. Valid Categories = %s" % (category, CATEGORY_COMMANDS_ALL.keys()))) + + # Check that all commands are valid + for cmd in command_list: + # Fail if even one command given is invalid + if cmd not in CATEGORY_COMMANDS_ALL[category]: + module.fail_json(msg=to_native("Invalid Command '%s'. Valid Commands = %s" % (cmd, CATEGORY_COMMANDS_ALL[category]))) + + # Organize by Categories / Commands + + if category == "Manager": + # execute only if we find a Manager resource + result = rf_utils._find_managers_resource(rf_uri) + if result['ret'] is False: + module.fail_json(msg=to_native(result['msg'])) + + for command in command_list: + if command == "GetManagerAttributes": + result = rf_utils.get_manager_attributes() + + # Return data back or fail with proper message + if result['ret'] is True: + del result['ret'] + module.exit_json(ansible_facts=dict(redfish_facts=result)) + else: + module.fail_json(msg=to_native(result['msg'])) + + +if __name__ == '__main__': + main()