Debian Codegraph @

ansible_collections/community/general/plugins/modules/wdc_redfish_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2022 Western Digital Corporation
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: wdc_redfish_info
11 short_description: Manages WDC UltraStar Data102 Out-Of-Band controllers using Redfish APIs
12 version_added: 5.4.0
13 description:
14 - Builds Redfish URIs locally and sends them to remote OOB controllers to get information back.
15 extends_documentation_fragment:
16 - community.general._attributes
17 - community.general._attributes.info_module
18 - community.general._redfish
19 options:
20 category:
21 required: true
22 description:
23 - Category to execute on OOB controller.
24 type: str
25 command:
26 required: true
27 description:
28 - List of commands to execute on OOB controller.
29 type: list
30 elements: str
31 baseuri:
32 description:
33 - Base URI of OOB controller. Must include this or O(ioms).
34 type: str
35 ioms:
36 description:
37 - List of IOM FQDNs for the enclosure. Must include this or O(baseuri).
38 type: list
39 elements: str
40 username:
41 description:
42 - User for authentication with OOB controller.
43 type: str
44 password:
45 description:
46 - Password for authentication with OOB controller.
47 type: str
48 auth_token:
49 description:
50 - Security token for authentication with OOB controller.
51 type: str
52 timeout:
53 description:
54 - Timeout in seconds for URL requests to OOB controller.
55 default: 10
56 type: int
57 validate_certs:
58 version_added: 10.6.0
59 ca_path:
60 version_added: 10.6.0
61 ciphers:
62 version_added: 10.6.0
63
64 notes:
65 - In the inventory, you can specify baseuri or ioms. See the EXAMPLES section.
66 - Ioms is a list of FQDNs for the enclosure's IOMs.
67 author: Mike Moerk (@mikemoerk)
68 """
69
70 EXAMPLES = r"""
71 - name: Get Simple Update Status with individual IOMs specified
72 community.general.wdc_redfish_info:
73 category: Update
74 command: SimpleUpdateStatus
75 ioms:
76 - iom1.wdc.com
77 - iom2.wdc.com
78 username: "{{ username }}"
79 password: "{{ password }}"
80 register: result
81
82 - name: Print fetched information
83 ansible.builtin.debug:
84 msg: "{{ result.redfish_facts.simple_update_status.entries | to_nice_json }}"
85
86 - name: Get Simple Update Status with baseuri specified
87 community.general.wdc_redfish_info:
88 category: Update
89 command: SimpleUpdateStatus
90 baseuri: "iom1.wdc.com"
91 username: "{{ username }}"
92 password: "{{ password }}"
93 register: result
94
95 - name: Print fetched information
96 ansible.builtin.debug:
97 msg: "{{ result.redfish_facts.simple_update_status.entries | to_nice_json }}"
98 """
99
100 RETURN = r"""
101 Description:
102 description: Firmware update status description.
103 returned: always
104 type: str
105 sample: Ready for FW update
106 ErrorCode:
107 description: Numeric error code for firmware update status. Non-zero indicates an error condition.
108 returned: always
109 type: int
110 sample: 0
111 EstimatedRemainingMinutes:
112 description: Estimated number of minutes remaining in firmware update operation.
113 returned: always
114 type: int
115 sample: 20
116 StatusCode:
117 description: Firmware update status code.
118 returned: always
119 type: int
120 sample: 2
121 """
122
123 from ansible.module_utils.basic import AnsibleModule
124 from ansible.module_utils.common.text.converters import to_native
125
126 from ansible_collections.community.general.plugins.module_utils._redfish_utils import REDFISH_COMMON_ARGUMENT_SPEC
127 from ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils import WdcRedfishUtils
128
129 CATEGORY_COMMANDS_ALL = {"Update": ["SimpleUpdateStatus"]}
130
131
132 def main():
133 result = {}
134 argument_spec = dict(
135 category=dict(required=True),
136 command=dict(required=True, type="list", elements="str"),
137 ioms=dict(type="list", elements="str"),
138 baseuri=dict(),
139 username=dict(),
140 password=dict(no_log=True),
141 auth_token=dict(no_log=True),
142 timeout=dict(type="int", default=10),
143 )
144 argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC)
145 module = AnsibleModule(
146 argument_spec,
147 required_together=[
148 ("username", "password"),
149 ],
150 required_one_of=[("username", "auth_token"), ("baseuri", "ioms")],
151 mutually_exclusive=[
152 ("username", "auth_token"),
153 ],
154 supports_check_mode=True,
155 )
156
157 category = module.params["category"]
158 command_list = module.params["command"]
159
160 # admin credentials used for authentication
161 creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]}
162
163 # timeout
164 timeout = module.params["timeout"]
165
166 # Check that Category is valid
167 if category not in CATEGORY_COMMANDS_ALL:
168 module.fail_json(
169 msg=f"Invalid Category '{category}'. Valid Categories = {sorted(CATEGORY_COMMANDS_ALL.keys())}"
170 )
171
172 # Check that all commands are valid
173 for cmd in command_list:
174 # Fail if even one command given is invalid
175 if cmd not in CATEGORY_COMMANDS_ALL[category]:
176 module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}")
177
178 # Build root URI(s)
179 if module.params.get("baseuri") is not None:
180 root_uris = [f"https://{module.params['baseuri']}"]
181 else:
182 root_uris = [f"https://{iom}" for iom in module.params["ioms"]]
183 rf_utils = WdcRedfishUtils(creds, root_uris, timeout, module, resource_id=None, data_modification=False)
184
185 # Organize by Categories / Commands
186
187 if category == "Update":
188 # execute only if we find UpdateService resources
189 resource = rf_utils._find_updateservice_resource()
190 if resource["ret"] is False:
191 module.fail_json(msg=resource["msg"])
192 for command in command_list:
193 if command == "SimpleUpdateStatus":
194 simple_update_status_result = rf_utils.get_simple_update_status()
195 if simple_update_status_result["ret"] is False:
196 module.fail_json(
197 msg=to_native(result["msg"])
198 ) # TODO result=={}, maybe simple_update_status_result?
199 else:
200 del simple_update_status_result["ret"]
201 result["simple_update_status"] = simple_update_status_result
202 module.exit_json(changed=False, redfish_facts=result)
203
204
205 if __name__ == "__main__":
206 main()