| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) Ansible project |
| 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: xcc_redfish_command |
| 11 |
short_description: Manages Lenovo Out-Of-Band controllers using Redfish APIs |
| 12 |
version_added: 2.4.0 |
| 13 |
description: |
| 14 |
- Builds Redfish URIs locally and sends them to remote OOB controllers to perform an action or get information back or update |
| 15 |
a configuration attribute. |
| 16 |
- Manages virtual media. |
| 17 |
- Supports getting information back using GET method. |
| 18 |
- Supports updating a configuration attribute using PATCH method. |
| 19 |
- Supports performing an action using POST method. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
- community.general._redfish |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: none |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
category: |
| 30 |
required: true |
| 31 |
description: |
| 32 |
- Category to execute on OOB controller. |
| 33 |
type: str |
| 34 |
command: |
| 35 |
required: true |
| 36 |
description: |
| 37 |
- List of commands to execute on OOB controller. |
| 38 |
type: list |
| 39 |
elements: str |
| 40 |
baseuri: |
| 41 |
required: true |
| 42 |
description: |
| 43 |
- Base URI of OOB controller. |
| 44 |
type: str |
| 45 |
username: |
| 46 |
description: |
| 47 |
- Username for authentication with OOB controller. |
| 48 |
type: str |
| 49 |
password: |
| 50 |
description: |
| 51 |
- Password for authentication with OOB controller. |
| 52 |
type: str |
| 53 |
auth_token: |
| 54 |
description: |
| 55 |
- Security token for authentication with OOB controller. |
| 56 |
type: str |
| 57 |
timeout: |
| 58 |
description: |
| 59 |
- Timeout in seconds for URL requests to OOB controller. |
| 60 |
default: 10 |
| 61 |
type: int |
| 62 |
resource_id: |
| 63 |
description: |
| 64 |
- The ID of the System, Manager or Chassis to modify. |
| 65 |
type: str |
| 66 |
virtual_media: |
| 67 |
description: |
| 68 |
- The options for VirtualMedia commands. |
| 69 |
type: dict |
| 70 |
suboptions: |
| 71 |
media_types: |
| 72 |
description: |
| 73 |
- The list of media types appropriate for the image. |
| 74 |
type: list |
| 75 |
elements: str |
| 76 |
default: [] |
| 77 |
image_url: |
| 78 |
description: |
| 79 |
- The URL of the image to insert or eject. |
| 80 |
type: str |
| 81 |
inserted: |
| 82 |
description: |
| 83 |
- Indicates if the image is treated as inserted on command completion. |
| 84 |
type: bool |
| 85 |
default: true |
| 86 |
write_protected: |
| 87 |
description: |
| 88 |
- Indicates if the media is treated as write-protected. |
| 89 |
type: bool |
| 90 |
default: true |
| 91 |
username: |
| 92 |
description: |
| 93 |
- The username for accessing the image URL. |
| 94 |
type: str |
| 95 |
password: |
| 96 |
description: |
| 97 |
- The password for accessing the image URL. |
| 98 |
type: str |
| 99 |
transfer_protocol_type: |
| 100 |
description: |
| 101 |
- The network protocol to use with the image. |
| 102 |
type: str |
| 103 |
transfer_method: |
| 104 |
description: |
| 105 |
- The transfer method to use with the image. |
| 106 |
type: str |
| 107 |
resource_uri: |
| 108 |
description: |
| 109 |
- The resource URI to get or patch or post. |
| 110 |
type: str |
| 111 |
request_body: |
| 112 |
description: |
| 113 |
- The request body to patch or post. |
| 114 |
type: dict |
| 115 |
validate_certs: |
| 116 |
version_added: 10.6.0 |
| 117 |
ca_path: |
| 118 |
version_added: 10.6.0 |
| 119 |
ciphers: |
| 120 |
version_added: 10.6.0 |
| 121 |
|
| 122 |
author: "Yuyan Pan (@panyy3)" |
| 123 |
""" |
| 124 |
|
| 125 |
EXAMPLES = r""" |
| 126 |
- name: Insert Virtual Media |
| 127 |
community.general.xcc_redfish_command: |
| 128 |
category: Manager |
| 129 |
command: VirtualMediaInsert |
| 130 |
baseuri: "{{ baseuri }}" |
| 131 |
username: "{{ username }}" |
| 132 |
password: "{{ password }}" |
| 133 |
virtual_media: |
| 134 |
image_url: "http://example.com/images/SomeLinux-current.iso" |
| 135 |
media_types: |
| 136 |
- CD |
| 137 |
- DVD |
| 138 |
resource_id: "1" |
| 139 |
|
| 140 |
- name: Eject Virtual Media |
| 141 |
community.general.xcc_redfish_command: |
| 142 |
category: Manager |
| 143 |
command: VirtualMediaEject |
| 144 |
baseuri: "{{ baseuri }}" |
| 145 |
username: "{{ username }}" |
| 146 |
password: "{{ password }}" |
| 147 |
virtual_media: |
| 148 |
image_url: "http://example.com/images/SomeLinux-current.iso" |
| 149 |
resource_id: "1" |
| 150 |
|
| 151 |
- name: Eject all Virtual Media |
| 152 |
community.general.xcc_redfish_command: |
| 153 |
category: Manager |
| 154 |
command: VirtualMediaEject |
| 155 |
baseuri: "{{ baseuri }}" |
| 156 |
username: "{{ username }}" |
| 157 |
password: "{{ password }}" |
| 158 |
resource_id: "1" |
| 159 |
|
| 160 |
- name: Get ComputeSystem Oem property SystemStatus via GetResource command |
| 161 |
community.general.xcc_redfish_command: |
| 162 |
category: Raw |
| 163 |
command: GetResource |
| 164 |
baseuri: "{{ baseuri }}" |
| 165 |
username: "{{ username }}" |
| 166 |
password: "{{ password }}" |
| 167 |
resource_uri: "/redfish/v1/Systems/1" |
| 168 |
register: result |
| 169 |
- ansible.builtin.debug: |
| 170 |
msg: "{{ result.redfish_facts.data.Oem.Lenovo.SystemStatus }}" |
| 171 |
|
| 172 |
- name: Get Oem DNS setting via GetResource command |
| 173 |
community.general.xcc_redfish_command: |
| 174 |
category: Raw |
| 175 |
command: GetResource |
| 176 |
baseuri: "{{ baseuri }}" |
| 177 |
username: "{{ username }}" |
| 178 |
password: "{{ password }}" |
| 179 |
resource_uri: "/redfish/v1/Managers/1/NetworkProtocol/Oem/Lenovo/DNS" |
| 180 |
register: result |
| 181 |
|
| 182 |
- name: Print fetched information |
| 183 |
ansible.builtin.debug: |
| 184 |
msg: "{{ result.redfish_facts.data }}" |
| 185 |
|
| 186 |
- name: Get Lenovo FoD key collection resource via GetCollectionResource command |
| 187 |
community.general.xcc_redfish_command: |
| 188 |
category: Raw |
| 189 |
command: GetCollectionResource |
| 190 |
baseuri: "{{ baseuri }}" |
| 191 |
username: "{{ username }}" |
| 192 |
password: "{{ password }}" |
| 193 |
resource_uri: "/redfish/v1/Managers/1/Oem/Lenovo/FoD/Keys" |
| 194 |
register: result |
| 195 |
|
| 196 |
- name: Print fetched information |
| 197 |
ansible.builtin.debug: |
| 198 |
msg: "{{ result.redfish_facts.data_list }}" |
| 199 |
|
| 200 |
- name: Update ComputeSystem property AssetTag via PatchResource command |
| 201 |
community.general.xcc_redfish_command: |
| 202 |
category: Raw |
| 203 |
command: PatchResource |
| 204 |
baseuri: "{{ baseuri }}" |
| 205 |
username: "{{ username }}" |
| 206 |
password: "{{ password }}" |
| 207 |
resource_uri: "/redfish/v1/Systems/1" |
| 208 |
request_body: |
| 209 |
AssetTag: "new_asset_tag" |
| 210 |
|
| 211 |
- name: Perform BootToBIOSSetup action via PostResource command |
| 212 |
community.general.xcc_redfish_command: |
| 213 |
category: Raw |
| 214 |
command: PostResource |
| 215 |
baseuri: "{{ baseuri }}" |
| 216 |
username: "{{ username }}" |
| 217 |
password: "{{ password }}" |
| 218 |
resource_uri: "/redfish/v1/Systems/1/Actions/Oem/LenovoComputerSystem.BootToBIOSSetup" |
| 219 |
request_body: {} |
| 220 |
|
| 221 |
- name: Perform SecureBoot.ResetKeys action via PostResource command |
| 222 |
community.general.xcc_redfish_command: |
| 223 |
category: Raw |
| 224 |
command: PostResource |
| 225 |
baseuri: "{{ baseuri }}" |
| 226 |
username: "{{ username }}" |
| 227 |
password: "{{ password }}" |
| 228 |
resource_uri: "/redfish/v1/Systems/1/SecureBoot/Actions/SecureBoot.ResetKeys" |
| 229 |
request_body: |
| 230 |
ResetKeysType: DeleteAllKeys |
| 231 |
|
| 232 |
- name: Create session |
| 233 |
community.general.redfish_command: |
| 234 |
category: Sessions |
| 235 |
command: CreateSession |
| 236 |
baseuri: "{{ baseuri }}" |
| 237 |
username: "{{ username }}" |
| 238 |
password: "{{ password }}" |
| 239 |
register: result |
| 240 |
|
| 241 |
- name: Update Manager DateTimeLocalOffset property using security token for auth |
| 242 |
community.general.xcc_redfish_command: |
| 243 |
category: Raw |
| 244 |
command: PatchResource |
| 245 |
baseuri: "{{ baseuri }}" |
| 246 |
auth_token: "{{ result.session.token }}" |
| 247 |
resource_uri: "/redfish/v1/Managers/1" |
| 248 |
request_body: |
| 249 |
DateTimeLocalOffset: "+08:00" |
| 250 |
|
| 251 |
- name: Delete session using security token created by CreateSesssion above |
| 252 |
community.general.redfish_command: |
| 253 |
category: Sessions |
| 254 |
command: DeleteSession |
| 255 |
baseuri: "{{ baseuri }}" |
| 256 |
auth_token: "{{ result.session.token }}" |
| 257 |
session_uri: "{{ result.session.uri }}" |
| 258 |
""" |
| 259 |
|
| 260 |
RETURN = r""" |
| 261 |
msg: |
| 262 |
description: A message related to the performed action(s). |
| 263 |
returned: when failure or action/update success |
| 264 |
type: str |
| 265 |
sample: "Action was successful" |
| 266 |
redfish_facts: |
| 267 |
description: Resource content. |
| 268 |
returned: when command == GetResource or command == GetCollectionResource |
| 269 |
type: dict |
| 270 |
sample: |
| 271 |
{ |
| 272 |
"redfish_facts": { |
| 273 |
"data": { |
| 274 |
"@odata.etag": "\"3179bf00d69f25a8b3c\"", |
| 275 |
"@odata.id": "/redfish/v1/Managers/1/NetworkProtocol/Oem/Lenovo/DNS", |
| 276 |
"@odata.type": "#LenovoDNS.v1_0_0.LenovoDNS", |
| 277 |
"DDNS": [ |
| 278 |
{ |
| 279 |
"DDNSEnable": true, |
| 280 |
"DomainName": "", |
| 281 |
"DomainNameSource": "DHCP" |
| 282 |
} |
| 283 |
], |
| 284 |
"DNSEnable": true, |
| 285 |
"Description": "This resource is used to represent a DNS resource for a Redfish implementation.", |
| 286 |
"IPv4Address1": "10.103.62.178", |
| 287 |
"IPv4Address2": "0.0.0.0", |
| 288 |
"IPv4Address3": "0.0.0.0", |
| 289 |
"IPv6Address1": "::", |
| 290 |
"IPv6Address2": "::", |
| 291 |
"IPv6Address3": "::", |
| 292 |
"Id": "LenovoDNS", |
| 293 |
"PreferredAddresstype": "IPv4" |
| 294 |
}, |
| 295 |
"ret": true |
| 296 |
} |
| 297 |
} |
| 298 |
""" |
| 299 |
|
| 300 |
from ansible.module_utils.basic import AnsibleModule |
| 301 |
from ansible.module_utils.common.text.converters import to_native |
| 302 |
|
| 303 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import ( |
| 304 |
REDFISH_COMMON_ARGUMENT_SPEC, |
| 305 |
RedfishUtils, |
| 306 |
) |
| 307 |
|
| 308 |
|
| 309 |
class XCCRedfishUtils(RedfishUtils): |
| 310 |
@staticmethod |
| 311 |
def _find_empty_virt_media_slot(resources, media_types, media_match_strict=True): |
| 312 |
for uri, data in resources.items(): |
| 313 |
# check MediaTypes |
| 314 |
if "MediaTypes" in data and media_types: |
| 315 |
if not set(media_types).intersection(set(data["MediaTypes"])): |
| 316 |
continue |
| 317 |
else: |
| 318 |
if media_match_strict: |
| 319 |
continue |
| 320 |
if "RDOC" in uri: |
| 321 |
continue |
| 322 |
if "Remote" in uri: |
| 323 |
continue |
| 324 |
# if ejected, 'Inserted' should be False and 'ImageName' cleared |
| 325 |
if not data.get("Inserted", False) and not data.get("ImageName"): |
| 326 |
return uri, data |
| 327 |
return None, None |
| 328 |
|
| 329 |
def virtual_media_eject_one(self, image_url): |
| 330 |
# read the VirtualMedia resources from systems |
| 331 |
response = self.get_request(self.root_uri + self.systems_uri) |
| 332 |
if response["ret"] is False: |
| 333 |
return response |
| 334 |
data = response["data"] |
| 335 |
if "VirtualMedia" not in data: |
| 336 |
# read the VirtualMedia resources from manager |
| 337 |
response = self.get_request(self.root_uri + self.manager_uri) |
| 338 |
if response["ret"] is False: |
| 339 |
return response |
| 340 |
data = response["data"] |
| 341 |
if "VirtualMedia" not in data: |
| 342 |
return {"ret": False, "msg": "VirtualMedia resource not found"} |
| 343 |
virt_media_uri = data["VirtualMedia"]["@odata.id"] |
| 344 |
response = self.get_request(self.root_uri + virt_media_uri) |
| 345 |
if response["ret"] is False: |
| 346 |
return response |
| 347 |
data = response["data"] |
| 348 |
virt_media_list = [] |
| 349 |
for member in data["Members"]: |
| 350 |
virt_media_list.append(member["@odata.id"]) |
| 351 |
resources, headers = self._read_virt_media_resources(virt_media_list) |
| 352 |
|
| 353 |
# find the VirtualMedia resource to eject |
| 354 |
uri, data, eject = self._find_virt_media_to_eject(resources, image_url) |
| 355 |
if uri and eject: |
| 356 |
if "Actions" not in data or "#VirtualMedia.EjectMedia" not in data["Actions"]: |
| 357 |
# try to eject via PATCH if no EjectMedia action found |
| 358 |
h = headers[uri] |
| 359 |
if "allow" in h: |
| 360 |
methods = [m.strip() for m in h.get("allow").split(",")] |
| 361 |
if "PATCH" not in methods: |
| 362 |
# if Allow header present and PATCH missing, return error |
| 363 |
return {"ret": False, "msg": "#VirtualMedia.EjectMedia action not found and PATCH not allowed"} |
| 364 |
return self.virtual_media_eject_via_patch(uri) |
| 365 |
else: |
| 366 |
# POST to the EjectMedia Action |
| 367 |
action = data["Actions"]["#VirtualMedia.EjectMedia"] |
| 368 |
if "target" not in action: |
| 369 |
return {"ret": False, "msg": "target URI property missing from Action #VirtualMedia.EjectMedia"} |
| 370 |
action_uri = action["target"] |
| 371 |
# empty payload for Eject action |
| 372 |
payload = {} |
| 373 |
# POST to action |
| 374 |
response = self.post_request(self.root_uri + action_uri, payload) |
| 375 |
if response["ret"] is False: |
| 376 |
return response |
| 377 |
return {"ret": True, "changed": True, "msg": "VirtualMedia ejected"} |
| 378 |
elif uri and not eject: |
| 379 |
# already ejected: return success but changed=False |
| 380 |
return {"ret": True, "changed": False, "msg": f"VirtualMedia image '{image_url}' already ejected"} |
| 381 |
else: |
| 382 |
# return failure (no resources matching image_url found) |
| 383 |
return { |
| 384 |
"ret": False, |
| 385 |
"changed": False, |
| 386 |
"msg": f"No VirtualMedia resource found with image '{image_url}' inserted", |
| 387 |
} |
| 388 |
|
| 389 |
def virtual_media_eject(self, options): |
| 390 |
if options: |
| 391 |
image_url = options.get("image_url") |
| 392 |
if image_url: # eject specified one media |
| 393 |
return self.virtual_media_eject_one(image_url) |
| 394 |
|
| 395 |
# eject all inserted media when no image_url specified |
| 396 |
# read the VirtualMedia resources from systems |
| 397 |
response = self.get_request(self.root_uri + self.systems_uri) |
| 398 |
if response["ret"] is False: |
| 399 |
return response |
| 400 |
data = response["data"] |
| 401 |
if "VirtualMedia" not in data: |
| 402 |
# read the VirtualMedia resources from manager |
| 403 |
response = self.get_request(self.root_uri + self.manager_uri) |
| 404 |
if response["ret"] is False: |
| 405 |
return response |
| 406 |
data = response["data"] |
| 407 |
if "VirtualMedia" not in data: |
| 408 |
return {"ret": False, "msg": "VirtualMedia resource not found"} |
| 409 |
# read all the VirtualMedia resources |
| 410 |
virt_media_uri = data["VirtualMedia"]["@odata.id"] |
| 411 |
response = self.get_request(self.root_uri + virt_media_uri) |
| 412 |
if response["ret"] is False: |
| 413 |
return response |
| 414 |
data = response["data"] |
| 415 |
virt_media_list = [] |
| 416 |
for member in data["Members"]: |
| 417 |
virt_media_list.append(member["@odata.id"]) |
| 418 |
resources, headers = self._read_virt_media_resources(virt_media_list) |
| 419 |
|
| 420 |
# eject all inserted media one by one |
| 421 |
ejected_media_list = [] |
| 422 |
for data in resources.values(): |
| 423 |
if data.get("Image") and data.get("Inserted", True): |
| 424 |
returndict = self.virtual_media_eject_one(data.get("Image")) |
| 425 |
if not returndict["ret"]: |
| 426 |
return returndict |
| 427 |
ejected_media_list.append(data.get("Image")) |
| 428 |
|
| 429 |
if len(ejected_media_list) == 0: |
| 430 |
# no media inserted: return success but changed=False |
| 431 |
return {"ret": True, "changed": False, "msg": "No VirtualMedia image inserted"} |
| 432 |
else: |
| 433 |
return {"ret": True, "changed": True, "msg": f"VirtualMedia {ejected_media_list!s} ejected"} |
| 434 |
|
| 435 |
def virtual_media_insert(self, options): |
| 436 |
param_map = { |
| 437 |
"Inserted": "inserted", |
| 438 |
"WriteProtected": "write_protected", |
| 439 |
"UserName": "username", |
| 440 |
"Password": "password", |
| 441 |
"TransferProtocolType": "transfer_protocol_type", |
| 442 |
"TransferMethod": "transfer_method", |
| 443 |
} |
| 444 |
image_url = options.get("image_url") |
| 445 |
if not image_url: |
| 446 |
return {"ret": False, "msg": "image_url option required for VirtualMediaInsert"} |
| 447 |
media_types = options.get("media_types") |
| 448 |
|
| 449 |
# read the VirtualMedia resources from systems |
| 450 |
response = self.get_request(self.root_uri + self.systems_uri) |
| 451 |
if response["ret"] is False: |
| 452 |
return response |
| 453 |
data = response["data"] |
| 454 |
if "VirtualMedia" not in data: |
| 455 |
# read the VirtualMedia resources from manager |
| 456 |
response = self.get_request(self.root_uri + self.manager_uri) |
| 457 |
if response["ret"] is False: |
| 458 |
return response |
| 459 |
data = response["data"] |
| 460 |
if "VirtualMedia" not in data: |
| 461 |
return {"ret": False, "msg": "VirtualMedia resource not found"} |
| 462 |
virt_media_uri = data["VirtualMedia"]["@odata.id"] |
| 463 |
response = self.get_request(self.root_uri + virt_media_uri) |
| 464 |
if response["ret"] is False: |
| 465 |
return response |
| 466 |
data = response["data"] |
| 467 |
virt_media_list = [] |
| 468 |
for member in data["Members"]: |
| 469 |
virt_media_list.append(member["@odata.id"]) |
| 470 |
resources, headers = self._read_virt_media_resources(virt_media_list) |
| 471 |
|
| 472 |
# see if image already inserted; if so, nothing to do |
| 473 |
if self._virt_media_image_inserted(resources, image_url): |
| 474 |
return {"ret": True, "changed": False, "msg": f"VirtualMedia '{image_url}' already inserted"} |
| 475 |
|
| 476 |
# find an empty slot to insert the media |
| 477 |
# try first with strict media_type matching |
| 478 |
uri, data = self._find_empty_virt_media_slot(resources, media_types, media_match_strict=True) |
| 479 |
if not uri: |
| 480 |
# if not found, try without strict media_type matching |
| 481 |
uri, data = self._find_empty_virt_media_slot(resources, media_types, media_match_strict=False) |
| 482 |
if not uri: |
| 483 |
return { |
| 484 |
"ret": False, |
| 485 |
"msg": f"Unable to find an available VirtualMedia resource {f'supporting {media_types}' if media_types else ''}", |
| 486 |
} |
| 487 |
|
| 488 |
# confirm InsertMedia action found |
| 489 |
if "Actions" not in data or "#VirtualMedia.InsertMedia" not in data["Actions"]: |
| 490 |
# try to insert via PATCH if no InsertMedia action found |
| 491 |
h = headers[uri] |
| 492 |
if "allow" in h: |
| 493 |
methods = [m.strip() for m in h.get("allow").split(",")] |
| 494 |
if "PATCH" not in methods: |
| 495 |
# if Allow header present and PATCH missing, return error |
| 496 |
return {"ret": False, "msg": "#VirtualMedia.InsertMedia action not found and PATCH not allowed"} |
| 497 |
return self.virtual_media_insert_via_patch(options, param_map, uri, data) |
| 498 |
|
| 499 |
# get the action property |
| 500 |
action = data["Actions"]["#VirtualMedia.InsertMedia"] |
| 501 |
if "target" not in action: |
| 502 |
return {"ret": False, "msg": "target URI missing from Action #VirtualMedia.InsertMedia"} |
| 503 |
action_uri = action["target"] |
| 504 |
# get ActionInfo or AllowableValues |
| 505 |
ai = self._get_all_action_info_values(action) |
| 506 |
# construct payload |
| 507 |
payload = self._insert_virt_media_payload(options, param_map, data, ai) |
| 508 |
# POST to action |
| 509 |
response = self.post_request(self.root_uri + action_uri, payload) |
| 510 |
if response["ret"] is False: |
| 511 |
return response |
| 512 |
return {"ret": True, "changed": True, "msg": "VirtualMedia inserted"} |
| 513 |
|
| 514 |
def raw_get_resource(self, resource_uri): |
| 515 |
if resource_uri is None: |
| 516 |
return {"ret": False, "msg": "resource_uri is missing"} |
| 517 |
response = self.get_request(self.root_uri + resource_uri) |
| 518 |
if response["ret"] is False: |
| 519 |
return response |
| 520 |
data = response["data"] |
| 521 |
return {"ret": True, "data": data} |
| 522 |
|
| 523 |
def raw_get_collection_resource(self, resource_uri): |
| 524 |
if resource_uri is None: |
| 525 |
return {"ret": False, "msg": "resource_uri is missing"} |
| 526 |
response = self.get_request(self.root_uri + resource_uri) |
| 527 |
if response["ret"] is False: |
| 528 |
return response |
| 529 |
if "Members" not in response["data"]: |
| 530 |
return {"ret": False, "msg": "Specified resource_uri doesn't have Members property"} |
| 531 |
member_list = [i["@odata.id"] for i in response["data"].get("Members", [])] |
| 532 |
|
| 533 |
# get member resource one by one |
| 534 |
data_list = [] |
| 535 |
for member_uri in member_list: |
| 536 |
uri = self.root_uri + member_uri |
| 537 |
response = self.get_request(uri) |
| 538 |
if response["ret"] is False: |
| 539 |
return response |
| 540 |
data = response["data"] |
| 541 |
data_list.append(data) |
| 542 |
|
| 543 |
return {"ret": True, "data_list": data_list} |
| 544 |
|
| 545 |
def raw_patch_resource(self, resource_uri, request_body): |
| 546 |
if resource_uri is None: |
| 547 |
return {"ret": False, "msg": "resource_uri is missing"} |
| 548 |
if request_body is None: |
| 549 |
return {"ret": False, "msg": "request_body is missing"} |
| 550 |
# check whether resource_uri existing or not |
| 551 |
response = self.get_request(self.root_uri + resource_uri) |
| 552 |
if response["ret"] is False: |
| 553 |
return response |
| 554 |
original_etag = response["data"]["@odata.etag"] |
| 555 |
|
| 556 |
# check validity of keys in request_body |
| 557 |
data = response["data"] |
| 558 |
for key in request_body.keys(): |
| 559 |
if key not in data: |
| 560 |
return {"ret": False, "msg": f"Key {key} not found. Supported key list: {list(data)}"} |
| 561 |
|
| 562 |
# perform patch |
| 563 |
response = self.patch_request(self.root_uri + resource_uri, request_body) |
| 564 |
if response["ret"] is False: |
| 565 |
return response |
| 566 |
|
| 567 |
# check whether changed or not |
| 568 |
current_etag = "" |
| 569 |
if "data" in response and "@odata.etag" in response["data"]: |
| 570 |
current_etag = response["data"]["@odata.etag"] |
| 571 |
if current_etag != original_etag: |
| 572 |
return {"ret": True, "changed": True} |
| 573 |
else: |
| 574 |
return {"ret": True, "changed": False} |
| 575 |
|
| 576 |
def raw_post_resource(self, resource_uri, request_body): |
| 577 |
if resource_uri is None: |
| 578 |
return {"ret": False, "msg": "resource_uri is missing"} |
| 579 |
resource_uri_has_actions = True |
| 580 |
if "/Actions/" not in resource_uri: |
| 581 |
resource_uri_has_actions = False |
| 582 |
if request_body is None: |
| 583 |
return {"ret": False, "msg": "request_body is missing"} |
| 584 |
# get action base uri data for further checking |
| 585 |
action_base_uri = resource_uri.split("/Actions/")[0] |
| 586 |
response = self.get_request(self.root_uri + action_base_uri) |
| 587 |
if response["ret"] is False: |
| 588 |
return response |
| 589 |
if "Actions" not in response["data"]: |
| 590 |
if resource_uri_has_actions: |
| 591 |
return {"ret": False, "msg": f"Actions property not found in {action_base_uri}"} |
| 592 |
else: |
| 593 |
response["data"]["Actions"] = {} |
| 594 |
|
| 595 |
# check resouce_uri with target uri found in action base uri data |
| 596 |
action_found = False |
| 597 |
action_info_uri = None |
| 598 |
action_target_uri_list = [] |
| 599 |
for key in response["data"]["Actions"].keys(): |
| 600 |
if action_found: |
| 601 |
break |
| 602 |
if not key.startswith("#"): |
| 603 |
continue |
| 604 |
if "target" in response["data"]["Actions"][key]: |
| 605 |
if resource_uri == response["data"]["Actions"][key]["target"]: |
| 606 |
action_found = True |
| 607 |
if "@Redfish.ActionInfo" in response["data"]["Actions"][key]: |
| 608 |
action_info_uri = response["data"]["Actions"][key]["@Redfish.ActionInfo"] |
| 609 |
else: |
| 610 |
action_target_uri_list.append(response["data"]["Actions"][key]["target"]) |
| 611 |
if not action_found and "Oem" in response["data"]["Actions"]: |
| 612 |
for key in response["data"]["Actions"]["Oem"].keys(): |
| 613 |
if action_found: |
| 614 |
break |
| 615 |
if not key.startswith("#"): |
| 616 |
continue |
| 617 |
if "target" in response["data"]["Actions"]["Oem"][key]: |
| 618 |
if resource_uri == response["data"]["Actions"]["Oem"][key]["target"]: |
| 619 |
action_found = True |
| 620 |
if "@Redfish.ActionInfo" in response["data"]["Actions"]["Oem"][key]: |
| 621 |
action_info_uri = response["data"]["Actions"]["Oem"][key]["@Redfish.ActionInfo"] |
| 622 |
else: |
| 623 |
action_target_uri_list.append(response["data"]["Actions"]["Oem"][key]["target"]) |
| 624 |
|
| 625 |
if not action_found and resource_uri_has_actions: |
| 626 |
return { |
| 627 |
"ret": False, |
| 628 |
"msg": ( |
| 629 |
f"Specified resource_uri is not a supported action target uri, please specify a supported target uri instead. " |
| 630 |
f"Supported uri: {action_target_uri_list}" |
| 631 |
), |
| 632 |
} |
| 633 |
|
| 634 |
# check request_body with parameter name defined by @Redfish.ActionInfo |
| 635 |
if action_info_uri is not None: |
| 636 |
response = self.get_request(self.root_uri + action_info_uri) |
| 637 |
if response["ret"] is False: |
| 638 |
return response |
| 639 |
for key in request_body.keys(): |
| 640 |
key_found = False |
| 641 |
for para in response["data"]["Parameters"]: |
| 642 |
if key == para["Name"]: |
| 643 |
key_found = True |
| 644 |
break |
| 645 |
if not key_found: |
| 646 |
return { |
| 647 |
"ret": False, |
| 648 |
"msg": ( |
| 649 |
f"Invalid property {key} found in request_body. " |
| 650 |
f"Please refer to @Redfish.ActionInfo Parameters: {response['data']['Parameters']}" |
| 651 |
), |
| 652 |
} |
| 653 |
|
| 654 |
# perform post |
| 655 |
response = self.post_request(self.root_uri + resource_uri, request_body) |
| 656 |
if response["ret"] is False: |
| 657 |
return response |
| 658 |
return {"ret": True, "changed": True} |
| 659 |
|
| 660 |
|
| 661 |
# More will be added as module features are expanded |
| 662 |
CATEGORY_COMMANDS_ALL = { |
| 663 |
"Manager": ["VirtualMediaInsert", "VirtualMediaEject"], |
| 664 |
"Raw": ["GetResource", "GetCollectionResource", "PatchResource", "PostResource"], |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
def main(): |
| 669 |
result = {} |
| 670 |
argument_spec = dict( |
| 671 |
category=dict(required=True), |
| 672 |
command=dict(required=True, type="list", elements="str"), |
| 673 |
baseuri=dict(required=True), |
| 674 |
username=dict(), |
| 675 |
password=dict(no_log=True), |
| 676 |
auth_token=dict(no_log=True), |
| 677 |
timeout=dict(type="int", default=10), |
| 678 |
resource_id=dict(), |
| 679 |
virtual_media=dict( |
| 680 |
type="dict", |
| 681 |
options=dict( |
| 682 |
media_types=dict(type="list", elements="str", default=[]), |
| 683 |
image_url=dict(), |
| 684 |
inserted=dict(type="bool", default=True), |
| 685 |
write_protected=dict(type="bool", default=True), |
| 686 |
username=dict(), |
| 687 |
password=dict(no_log=True), |
| 688 |
transfer_protocol_type=dict(), |
| 689 |
transfer_method=dict(), |
| 690 |
), |
| 691 |
), |
| 692 |
resource_uri=dict(), |
| 693 |
request_body=dict( |
| 694 |
type="dict", |
| 695 |
), |
| 696 |
) |
| 697 |
argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC) |
| 698 |
module = AnsibleModule( |
| 699 |
argument_spec, |
| 700 |
required_together=[ |
| 701 |
("username", "password"), |
| 702 |
], |
| 703 |
required_one_of=[ |
| 704 |
("username", "auth_token"), |
| 705 |
], |
| 706 |
mutually_exclusive=[ |
| 707 |
("username", "auth_token"), |
| 708 |
], |
| 709 |
supports_check_mode=False, |
| 710 |
) |
| 711 |
|
| 712 |
category = module.params["category"] |
| 713 |
command_list = module.params["command"] |
| 714 |
|
| 715 |
# admin credentials used for authentication |
| 716 |
creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]} |
| 717 |
|
| 718 |
# timeout |
| 719 |
timeout = module.params["timeout"] |
| 720 |
|
| 721 |
# System, Manager or Chassis ID to modify |
| 722 |
resource_id = module.params["resource_id"] |
| 723 |
|
| 724 |
# VirtualMedia options |
| 725 |
virtual_media = module.params["virtual_media"] |
| 726 |
|
| 727 |
# resource_uri |
| 728 |
resource_uri = module.params["resource_uri"] |
| 729 |
|
| 730 |
# request_body |
| 731 |
request_body = module.params["request_body"] |
| 732 |
|
| 733 |
# Build root URI |
| 734 |
root_uri = f"https://{module.params['baseuri']}" |
| 735 |
rf_utils = XCCRedfishUtils(creds, root_uri, timeout, module, resource_id=resource_id, data_modification=True) |
| 736 |
|
| 737 |
# Check that Category is valid |
| 738 |
if category not in CATEGORY_COMMANDS_ALL: |
| 739 |
module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") |
| 740 |
|
| 741 |
# Check that all commands are valid |
| 742 |
for cmd in command_list: |
| 743 |
# Fail if even one command given is invalid |
| 744 |
if cmd not in CATEGORY_COMMANDS_ALL[category]: |
| 745 |
module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") |
| 746 |
|
| 747 |
# Organize by Categories / Commands |
| 748 |
if category == "Manager": |
| 749 |
# For virtual media resource locates on Systems service |
| 750 |
result = rf_utils._find_systems_resource() |
| 751 |
if result["ret"] is False: |
| 752 |
module.fail_json(msg=result["msg"]) |
| 753 |
# For virtual media resource locates on Managers service |
| 754 |
result = rf_utils._find_managers_resource() |
| 755 |
if result["ret"] is False: |
| 756 |
module.fail_json(msg=result["msg"]) |
| 757 |
|
| 758 |
for command in command_list: |
| 759 |
if command == "VirtualMediaInsert": |
| 760 |
result = rf_utils.virtual_media_insert(virtual_media) |
| 761 |
elif command == "VirtualMediaEject": |
| 762 |
result = rf_utils.virtual_media_eject(virtual_media) |
| 763 |
elif category == "Raw": |
| 764 |
for command in command_list: |
| 765 |
if command == "GetResource": |
| 766 |
result = rf_utils.raw_get_resource(resource_uri) |
| 767 |
elif command == "GetCollectionResource": |
| 768 |
result = rf_utils.raw_get_collection_resource(resource_uri) |
| 769 |
elif command == "PatchResource": |
| 770 |
result = rf_utils.raw_patch_resource(resource_uri, request_body) |
| 771 |
elif command == "PostResource": |
| 772 |
result = rf_utils.raw_post_resource(resource_uri, request_body) |
| 773 |
|
| 774 |
# Return data back or fail with proper message |
| 775 |
if result["ret"] is True: |
| 776 |
if command == "GetResource" or command == "GetCollectionResource": |
| 777 |
module.exit_json(redfish_facts=result) |
| 778 |
else: |
| 779 |
changed = result.get("changed", True) |
| 780 |
msg = result.get("msg", "Action was successful") |
| 781 |
module.exit_json(changed=changed, msg=msg) |
| 782 |
else: |
| 783 |
module.fail_json(msg=to_native(result["msg"])) |
| 784 |
|
| 785 |
|
| 786 |
if __name__ == "__main__": |
| 787 |
main() |