| 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: ocapi_info |
| 11 |
version_added: 6.3.0 |
| 12 |
short_description: Manages Out-Of-Band controllers using Open Composable API (OCAPI) |
| 13 |
description: |
| 14 |
- Builds OCAPI 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 |
options: |
| 19 |
category: |
| 20 |
required: true |
| 21 |
description: |
| 22 |
- Category to execute on OOB controller. |
| 23 |
type: str |
| 24 |
command: |
| 25 |
required: true |
| 26 |
description: |
| 27 |
- Command to execute on OOB controller. |
| 28 |
type: str |
| 29 |
baseuri: |
| 30 |
required: true |
| 31 |
description: |
| 32 |
- Base URI of OOB controller. |
| 33 |
type: str |
| 34 |
proxy_slot_number: |
| 35 |
description: For proxied inband requests, the slot number of the IOM. Only applies if O(baseuri) is a proxy server. |
| 36 |
type: int |
| 37 |
username: |
| 38 |
required: true |
| 39 |
description: |
| 40 |
- Username for authenticating to OOB controller. |
| 41 |
type: str |
| 42 |
password: |
| 43 |
required: true |
| 44 |
description: |
| 45 |
- Password for authenticating to OOB controller. |
| 46 |
type: str |
| 47 |
timeout: |
| 48 |
description: |
| 49 |
- Timeout in seconds for URL requests to OOB controller. |
| 50 |
default: 10 |
| 51 |
type: int |
| 52 |
job_name: |
| 53 |
description: |
| 54 |
- Name of job for fetching status. |
| 55 |
type: str |
| 56 |
|
| 57 |
|
| 58 |
author: "Mike Moerk (@mikemoerk)" |
| 59 |
""" |
| 60 |
|
| 61 |
EXAMPLES = r""" |
| 62 |
- name: Get job status |
| 63 |
community.general.ocapi_info: |
| 64 |
category: Status |
| 65 |
command: JobStatus |
| 66 |
baseuri: "http://iom1.wdc.com" |
| 67 |
jobName: FirmwareUpdate |
| 68 |
username: "{{ username }}" |
| 69 |
password: "{{ password }}" |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
msg: |
| 74 |
description: Message with action result or error description. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: "Action was successful" |
| 78 |
|
| 79 |
percentComplete: |
| 80 |
description: Percent complete of the relevant operation. Applies to O(command=JobStatus). |
| 81 |
returned: when supported |
| 82 |
type: int |
| 83 |
sample: 99 |
| 84 |
|
| 85 |
operationStatus: |
| 86 |
description: Status of the relevant operation. Applies to O(command=JobStatus). See OCAPI documentation for details. |
| 87 |
returned: when supported |
| 88 |
type: str |
| 89 |
sample: "Activate needed" |
| 90 |
|
| 91 |
operationStatusId: |
| 92 |
description: Integer value of status (corresponds to operationStatus). Applies to O(command=JobStatus). See OCAPI documentation |
| 93 |
for details. |
| 94 |
returned: when supported |
| 95 |
type: int |
| 96 |
sample: 65540 |
| 97 |
|
| 98 |
operationHealth: |
| 99 |
description: Health of the operation. Applies to O(command=JobStatus). See OCAPI documentation for details. |
| 100 |
returned: when supported |
| 101 |
type: str |
| 102 |
sample: "OK" |
| 103 |
|
| 104 |
operationHealthId: |
| 105 |
description: >- |
| 106 |
Integer value for health of the operation (corresponds to RV(operationHealth)). Applies to O(command=JobStatus). See OCAPI |
| 107 |
documentation for details. |
| 108 |
returned: when supported |
| 109 |
type: str |
| 110 |
sample: "OK" |
| 111 |
|
| 112 |
details: |
| 113 |
description: Details of the relevant operation. Applies to O(command=JobStatus). |
| 114 |
returned: when supported |
| 115 |
type: list |
| 116 |
elements: str |
| 117 |
|
| 118 |
status: |
| 119 |
description: Dictionary containing status information. See OCAPI documentation for details. |
| 120 |
returned: when supported |
| 121 |
type: dict |
| 122 |
sample: |
| 123 |
{ |
| 124 |
"Details": [ |
| 125 |
"None" |
| 126 |
], |
| 127 |
"Health": [ |
| 128 |
{ |
| 129 |
"ID": 5, |
| 130 |
"Name": "OK" |
| 131 |
} |
| 132 |
], |
| 133 |
"State": { |
| 134 |
"ID": 16, |
| 135 |
"Name": "In service" |
| 136 |
} |
| 137 |
} |
| 138 |
""" |
| 139 |
|
| 140 |
from urllib.parse import urljoin |
| 141 |
|
| 142 |
from ansible.module_utils.basic import AnsibleModule |
| 143 |
from ansible.module_utils.common.text.converters import to_native |
| 144 |
|
| 145 |
from ansible_collections.community.general.plugins.module_utils._ocapi_utils import OcapiUtils |
| 146 |
|
| 147 |
# More will be added as module features are expanded |
| 148 |
CATEGORY_COMMANDS_ALL = {"Jobs": ["JobStatus"]} |
| 149 |
|
| 150 |
|
| 151 |
def main(): |
| 152 |
result = {} |
| 153 |
module = AnsibleModule( |
| 154 |
argument_spec=dict( |
| 155 |
category=dict(required=True), |
| 156 |
command=dict(required=True, type="str"), |
| 157 |
job_name=dict(type="str"), |
| 158 |
baseuri=dict(required=True, type="str"), |
| 159 |
proxy_slot_number=dict(type="int"), |
| 160 |
username=dict(required=True), |
| 161 |
password=dict(required=True, no_log=True), |
| 162 |
timeout=dict(type="int", default=10), |
| 163 |
), |
| 164 |
supports_check_mode=True, |
| 165 |
) |
| 166 |
|
| 167 |
category = module.params["category"] |
| 168 |
command = module.params["command"] |
| 169 |
|
| 170 |
# admin credentials used for authentication |
| 171 |
creds = {"user": module.params["username"], "pswd": module.params["password"]} |
| 172 |
|
| 173 |
# timeout |
| 174 |
timeout = module.params["timeout"] |
| 175 |
|
| 176 |
base_uri = f"https://{module.params['baseuri']}" |
| 177 |
proxy_slot_number = module.params.get("proxy_slot_number") |
| 178 |
ocapi_utils = OcapiUtils(creds, base_uri, proxy_slot_number, timeout, module) |
| 179 |
|
| 180 |
# Check that Category is valid |
| 181 |
if category not in CATEGORY_COMMANDS_ALL: |
| 182 |
module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") |
| 183 |
|
| 184 |
# Check that the command is valid |
| 185 |
if command not in CATEGORY_COMMANDS_ALL[category]: |
| 186 |
module.fail_json(msg=f"Invalid Command '{command}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") |
| 187 |
|
| 188 |
# Organize by Categories / Commands |
| 189 |
if category == "Jobs": |
| 190 |
if command == "JobStatus": |
| 191 |
if module.params.get("job_name") is None: |
| 192 |
module.fail_json(msg="job_name required for JobStatus command.") |
| 193 |
job_uri = urljoin(base_uri, f"Jobs/{module.params['job_name']}") |
| 194 |
result = ocapi_utils.get_job_status(job_uri) |
| 195 |
|
| 196 |
if result["ret"] is False: |
| 197 |
module.fail_json(msg=to_native(result["msg"])) |
| 198 |
else: |
| 199 |
del result["ret"] |
| 200 |
changed = False |
| 201 |
session = result.get("session", dict()) |
| 202 |
kwargs = { |
| 203 |
"changed": changed, |
| 204 |
"session": session, |
| 205 |
"msg": "Action was successful." |
| 206 |
if not module.check_mode |
| 207 |
else result.get("msg", "No action performed in check mode."), |
| 208 |
} |
| 209 |
result_keys = [result_key for result_key in result if result_key not in kwargs] |
| 210 |
for result_key in result_keys: |
| 211 |
kwargs[result_key] = result[result_key] |
| 212 |
module.exit_json(**kwargs) |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
main() |