George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | BMC redfish utility functions. |
| 5 | """ |
| 6 | |
| 7 | import json |
George Keishing | 3a6f073 | 2020-07-13 14:21:23 -0500 | [diff] [blame] | 8 | import re |
George Keishing | 43909f3 | 2024-08-20 22:08:02 +0530 | [diff] [blame] | 9 | from json.decoder import JSONDecodeError |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 10 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 11 | import gen_print as gp |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 12 | from robot.libraries.BuiltIn import BuiltIn |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 13 | |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 14 | MTLS_ENABLED = BuiltIn().get_variable_value("${MTLS_ENABLED}") |
| 15 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 16 | |
| 17 | class bmc_redfish_utils(object): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 18 | ROBOT_LIBRARY_SCOPE = "TEST SUITE" |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 19 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 20 | def __init__(self): |
| 21 | r""" |
| 22 | Initialize the bmc_redfish_utils object. |
| 23 | """ |
| 24 | # Obtain a reference to the global redfish object. |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 25 | self.__inited__ = False |
George Keishing | 7049fba | 2025-01-16 10:44:46 +0530 | [diff] [blame] | 26 | try: |
| 27 | self._redfish_ = BuiltIn().get_library_instance("redfish") |
| 28 | except RuntimeError as e: |
| 29 | BuiltIn().log_to_console( |
| 30 | "get_library_instance: No active redfish instance found." |
| 31 | ) |
| 32 | # Handling init exception at worse to raise on error. |
| 33 | raise e |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 34 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 35 | if MTLS_ENABLED == "True": |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 36 | self.__inited__ = True |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 37 | else: |
George Keishing | b4e3ad4 | 2025-03-20 10:32:40 +0530 | [diff] [blame] | 38 | # initialized if only login succeed. |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 39 | self._redfish_.login() |
George Keishing | b4e3ad4 | 2025-03-20 10:32:40 +0530 | [diff] [blame] | 40 | self.__inited__ = True |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 41 | |
George Keishing | 374e684 | 2019-02-20 08:57:18 -0600 | [diff] [blame] | 42 | def get_redfish_session_info(self): |
| 43 | r""" |
| 44 | Returns redfish sessions info dictionary. |
| 45 | |
| 46 | { |
| 47 | 'key': 'yLXotJnrh5nDhXj5lLiH' , |
| 48 | 'location': '/redfish/v1/SessionService/Sessions/nblYY4wlz0' |
| 49 | } |
| 50 | """ |
| 51 | session_dict = { |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 52 | "key": self._redfish_.get_session_key(), |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 53 | "location": self._redfish_.get_session_location(), |
George Keishing | 374e684 | 2019-02-20 08:57:18 -0600 | [diff] [blame] | 54 | } |
| 55 | return session_dict |
| 56 | |
Sandhya Somashekar | 37122b6 | 2019-06-18 06:02:02 -0500 | [diff] [blame] | 57 | def get_attribute(self, resource_path, attribute, verify=None): |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 58 | r""" |
| 59 | Get resource attribute. |
| 60 | |
| 61 | Description of argument(s): |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 62 | resource_path URI resource absolute path (e.g. |
| 63 | "/redfish/v1/Systems/1"). |
| 64 | attribute Name of the attribute (e.g. 'PowerState'). |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 65 | """ |
George Keishing | 43909f3 | 2024-08-20 22:08:02 +0530 | [diff] [blame] | 66 | try: |
| 67 | resp = self._redfish_.get(resource_path) |
| 68 | except JSONDecodeError as e: |
| 69 | BuiltIn().log_to_console( |
| 70 | "get_attribute: JSONDecodeError, re-trying" |
| 71 | ) |
| 72 | resp = self._redfish_.get(resource_path) |
Sandhya Somashekar | 37122b6 | 2019-06-18 06:02:02 -0500 | [diff] [blame] | 73 | |
| 74 | if verify: |
| 75 | if resp.dict[attribute] == verify: |
| 76 | return resp.dict[attribute] |
| 77 | else: |
| 78 | raise ValueError("Attribute value is not equal") |
| 79 | elif attribute in resp.dict: |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 80 | return resp.dict[attribute] |
| 81 | |
| 82 | return None |
| 83 | |
George Keishing | c3c05c2 | 2019-02-19 01:04:54 -0600 | [diff] [blame] | 84 | def get_properties(self, resource_path): |
| 85 | r""" |
| 86 | Returns dictionary of attributes for the resource. |
| 87 | |
| 88 | Description of argument(s): |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 89 | resource_path URI resource absolute path (e.g. |
Sandhya Somashekar | 37122b6 | 2019-06-18 06:02:02 -0500 | [diff] [blame] | 90 | /redfish/v1/Systems/1"). |
George Keishing | c3c05c2 | 2019-02-19 01:04:54 -0600 | [diff] [blame] | 91 | """ |
| 92 | |
| 93 | resp = self._redfish_.get(resource_path) |
| 94 | return resp.dict |
| 95 | |
George Keishing | 789c3b4 | 2020-07-14 08:44:47 -0500 | [diff] [blame] | 96 | def get_members_uri(self, resource_path, attribute): |
| 97 | r""" |
| 98 | Returns the list of valid path which has a given attribute. |
| 99 | |
| 100 | Description of argument(s): |
| 101 | resource_path URI resource base path (e.g. |
| 102 | '/redfish/v1/Systems/', |
| 103 | '/redfish/v1/Chassis/'). |
| 104 | attribute Name of the attribute (e.g. 'PowerSupplies'). |
| 105 | """ |
| 106 | |
George Keishing | d5f179e | 2020-07-14 16:07:31 -0500 | [diff] [blame] | 107 | # Set quiet variable to keep subordinate get() calls quiet. |
| 108 | quiet = 1 |
| 109 | |
George Keishing | 789c3b4 | 2020-07-14 08:44:47 -0500 | [diff] [blame] | 110 | # Get the member id list. |
| 111 | # e.g. ['/redfish/v1/Chassis/foo', '/redfish/v1/Chassis/bar'] |
| 112 | resource_path_list = self.get_member_list(resource_path) |
George Keishing | 789c3b4 | 2020-07-14 08:44:47 -0500 | [diff] [blame] | 113 | |
| 114 | valid_path_list = [] |
| 115 | |
| 116 | for path_idx in resource_path_list: |
| 117 | # Get all the child object path under the member id e.g. |
| 118 | # ['/redfish/v1/Chassis/foo/Power','/redfish/v1/Chassis/bar/Power'] |
| 119 | child_path_list = self.list_request(path_idx) |
George Keishing | 789c3b4 | 2020-07-14 08:44:47 -0500 | [diff] [blame] | 120 | |
| 121 | # Iterate and check if path object has the attribute. |
| 122 | for child_path_idx in child_path_list: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 123 | if ( |
| 124 | ("JsonSchemas" in child_path_idx) |
| 125 | or ("SessionService" in child_path_idx) |
| 126 | or ("#" in child_path_idx) |
| 127 | ): |
George Keishing | 6396bc6 | 2020-07-15 06:56:46 -0500 | [diff] [blame] | 128 | continue |
George Keishing | 789c3b4 | 2020-07-14 08:44:47 -0500 | [diff] [blame] | 129 | if self.get_attribute(child_path_idx, attribute): |
| 130 | valid_path_list.append(child_path_idx) |
| 131 | |
George Keishing | d5f179e | 2020-07-14 16:07:31 -0500 | [diff] [blame] | 132 | BuiltIn().log_to_console(valid_path_list) |
George Keishing | 789c3b4 | 2020-07-14 08:44:47 -0500 | [diff] [blame] | 133 | return valid_path_list |
| 134 | |
George Keishing | 3a6f073 | 2020-07-13 14:21:23 -0500 | [diff] [blame] | 135 | def get_endpoint_path_list(self, resource_path, end_point_prefix): |
| 136 | r""" |
| 137 | Returns list with entries ending in "/endpoint". |
| 138 | |
| 139 | Description of argument(s): |
| 140 | resource_path URI resource base path (e.g. "/redfish/v1/Chassis/"). |
George Keishing | e68cbfb | 2020-08-12 11:11:58 -0500 | [diff] [blame] | 141 | end_point_prefix Name of the endpoint (e.g. 'Power'). |
George Keishing | 3a6f073 | 2020-07-13 14:21:23 -0500 | [diff] [blame] | 142 | |
| 143 | Find all list entries ending in "/endpoint" combination such as |
| 144 | /redfish/v1/Chassis/<foo>/Power |
| 145 | /redfish/v1/Chassis/<bar>/Power |
| 146 | """ |
| 147 | |
| 148 | end_point_list = self.list_request(resource_path) |
| 149 | |
| 150 | # Regex to match entries ending in "/prefix" with optional underscore. |
George Keishing | d2f210a | 2022-02-23 10:44:10 -0600 | [diff] [blame] | 151 | regex = ".*/" + end_point_prefix + "[_]?[0-9]*$" |
George Keishing | 3a6f073 | 2020-07-13 14:21:23 -0500 | [diff] [blame] | 152 | return [x for x in end_point_list if re.match(regex, x, re.IGNORECASE)] |
| 153 | |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 154 | def get_target_actions(self, resource_path, target_attribute): |
| 155 | r""" |
| 156 | Returns resource target entry of the searched target attribute. |
| 157 | |
| 158 | Description of argument(s): |
| 159 | resource_path URI resource absolute path |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 160 | (e.g. "/redfish/v1/Systems/system"). |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 161 | target_attribute Name of the attribute (e.g. 'ComputerSystem.Reset'). |
| 162 | |
| 163 | Example: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 164 | "Actions": { |
| 165 | "#ComputerSystem.Reset": { |
| 166 | "ResetType@Redfish.AllowableValues": [ |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 167 | "On", |
| 168 | "ForceOff", |
| 169 | "GracefulRestart", |
| 170 | "GracefulShutdown" |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 171 | ], |
| 172 | "target": "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" |
| 173 | } |
| 174 | } |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 175 | """ |
| 176 | |
| 177 | global target_list |
| 178 | target_list = [] |
| 179 | |
| 180 | resp_dict = self.get_attribute(resource_path, "Actions") |
| 181 | if resp_dict is None: |
| 182 | return None |
| 183 | |
| 184 | # Recursively search the "target" key in the nested dictionary. |
| 185 | # Populate the target_list of target entries. |
| 186 | self.get_key_value_nested_dict(resp_dict, "target") |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 187 | # Return the matching target URL entry. |
| 188 | for target in target_list: |
| 189 | # target "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 190 | attribute_in_uri = target.rsplit("/", 1)[-1] |
Anusha Dathatri | adfdb60 | 2021-04-07 01:50:24 -0500 | [diff] [blame] | 191 | # attribute_in_uri "ComputerSystem.Reset" |
| 192 | if target_attribute == attribute_in_uri: |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 193 | return target |
| 194 | |
| 195 | return None |
| 196 | |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 197 | def get_member_list(self, resource_path): |
| 198 | r""" |
| 199 | Perform a GET list request and return available members entries. |
| 200 | |
| 201 | Description of argument(s): |
| 202 | resource_path URI resource absolute path |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 203 | (e.g. "/redfish/v1/SessionService/Sessions"). |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 204 | |
| 205 | "Members": [ |
| 206 | { |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 207 | "@odata.id": "/redfish/v1/SessionService/Sessions/Z5HummWPZ7" |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 208 | } |
| 209 | { |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 210 | "@odata.id": "/redfish/v1/SessionService/Sessions/46CmQmEL7H" |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 211 | } |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 212 | ], |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 213 | """ |
| 214 | |
| 215 | member_list = [] |
| 216 | resp_list_dict = self.get_attribute(resource_path, "Members") |
| 217 | if resp_list_dict is None: |
| 218 | return member_list |
| 219 | |
| 220 | for member_id in range(0, len(resp_list_dict)): |
| 221 | member_list.append(resp_list_dict[member_id]["@odata.id"]) |
| 222 | |
| 223 | return member_list |
| 224 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 225 | def list_request(self, resource_path): |
| 226 | r""" |
| 227 | Perform a GET list request and return available resource paths. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 228 | Description of argument(s): |
| 229 | resource_path URI resource absolute path |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 230 | (e.g. "/redfish/v1/SessionService/Sessions"). |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 231 | """ |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 232 | gp.qprint_executing(style=gp.func_line_style_short) |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 233 | # Set quiet variable to keep subordinate get() calls quiet. |
| 234 | quiet = 1 |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 235 | self.__pending_enumeration = set() |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 236 | self._rest_response_ = self._redfish_.get( |
| 237 | resource_path, valid_status_codes=[200, 404, 500] |
| 238 | ) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 239 | |
| 240 | # Return empty list. |
| 241 | if self._rest_response_.status != 200: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 242 | return self.__pending_enumeration |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 243 | self.walk_nested_dict(self._rest_response_.dict) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 244 | if not self.__pending_enumeration: |
| 245 | return resource_path |
| 246 | for resource in self.__pending_enumeration.copy(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 247 | self._rest_response_ = self._redfish_.get( |
| 248 | resource, valid_status_codes=[200, 404, 500] |
| 249 | ) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 250 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 251 | if self._rest_response_.status != 200: |
| 252 | continue |
| 253 | self.walk_nested_dict(self._rest_response_.dict) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 254 | return list(sorted(self.__pending_enumeration)) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 255 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 256 | def enumerate_request( |
| 257 | self, resource_path, return_json=1, include_dead_resources=False |
| 258 | ): |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 259 | r""" |
| 260 | Perform a GET enumerate request and return available resource paths. |
| 261 | |
| 262 | Description of argument(s): |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 263 | resource_path URI resource absolute path (e.g. |
| 264 | "/redfish/v1/SessionService/Sessions"). |
| 265 | return_json Indicates whether the result should be |
| 266 | returned as a json string or as a |
| 267 | dictionary. |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 268 | include_dead_resources Check and return a list of dead/broken URI |
| 269 | resources. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 270 | """ |
| 271 | |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 272 | gp.qprint_executing(style=gp.func_line_style_short) |
| 273 | |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 274 | return_json = int(return_json) |
| 275 | |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 276 | # Set quiet variable to keep subordinate get() calls quiet. |
| 277 | quiet = 1 |
| 278 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 279 | # Variable to hold enumerated data. |
| 280 | self.__result = {} |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 281 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 282 | # Variable to hold the pending list of resources for which enumeration. |
| 283 | # is yet to be obtained. |
| 284 | self.__pending_enumeration = set() |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 285 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 286 | self.__pending_enumeration.add(resource_path) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 287 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 288 | # Variable having resources for which enumeration is completed. |
| 289 | enumerated_resources = set() |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 290 | |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 291 | if include_dead_resources: |
| 292 | dead_resources = {} |
| 293 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 294 | resources_to_be_enumerated = (resource_path,) |
| 295 | |
| 296 | while resources_to_be_enumerated: |
| 297 | for resource in resources_to_be_enumerated: |
Anusha Dathatri | 6d2d42f | 2019-11-20 06:17:51 -0600 | [diff] [blame] | 298 | # JsonSchemas, SessionService or URLs containing # are not |
| 299 | # required in enumeration. |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 300 | # Example: '/redfish/v1/JsonSchemas/' and sub resources. |
Anusha Dathatri | cdb77db | 2019-09-10 08:10:29 -0500 | [diff] [blame] | 301 | # '/redfish/v1/SessionService' |
ganesanb | 4d43028 | 2023-04-27 14:33:23 +0000 | [diff] [blame] | 302 | # '/redfish/v1/Managers/${MANAGER_ID}#/Oem' |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 303 | if ( |
| 304 | ("JsonSchemas" in resource) |
| 305 | or ("SessionService" in resource) |
| 306 | or ("PostCodes" in resource) |
| 307 | or ("Registries" in resource) |
| 308 | or ("Journal" in resource) |
| 309 | or ("#" in resource) |
| 310 | ): |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 311 | continue |
| 312 | |
George Keishing | 43909f3 | 2024-08-20 22:08:02 +0530 | [diff] [blame] | 313 | try: |
| 314 | self._rest_response_ = self._redfish_.get( |
| 315 | resource, valid_status_codes=[200, 404, 405, 500] |
| 316 | ) |
| 317 | except JSONDecodeError as e: |
| 318 | BuiltIn().log_to_console( |
| 319 | "enumerate_request: JSONDecodeError, re-trying" |
| 320 | ) |
| 321 | self._rest_response_ = self._redfish_.get( |
| 322 | resource, valid_status_codes=[200, 404, 405, 500] |
| 323 | ) |
| 324 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 325 | # Enumeration is done for available resources ignoring the |
| 326 | # ones for which response is not obtained. |
| 327 | if self._rest_response_.status != 200: |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 328 | if include_dead_resources: |
| 329 | try: |
| 330 | dead_resources[self._rest_response_.status].append( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 331 | resource |
| 332 | ) |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 333 | except KeyError: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 334 | dead_resources[self._rest_response_.status] = [ |
| 335 | resource |
| 336 | ] |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 337 | continue |
| 338 | |
| 339 | self.walk_nested_dict(self._rest_response_.dict, url=resource) |
| 340 | |
| 341 | enumerated_resources.update(set(resources_to_be_enumerated)) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 342 | resources_to_be_enumerated = tuple( |
| 343 | self.__pending_enumeration - enumerated_resources |
| 344 | ) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 345 | |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 346 | if return_json: |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 347 | if include_dead_resources: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 348 | return ( |
| 349 | json.dumps( |
| 350 | self.__result, |
| 351 | sort_keys=True, |
| 352 | indent=4, |
| 353 | separators=(",", ": "), |
| 354 | ), |
| 355 | dead_resources, |
| 356 | ) |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 357 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 358 | return json.dumps( |
| 359 | self.__result, |
| 360 | sort_keys=True, |
| 361 | indent=4, |
| 362 | separators=(",", ": "), |
| 363 | ) |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 364 | else: |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 365 | if include_dead_resources: |
| 366 | return self.__result, dead_resources |
| 367 | else: |
| 368 | return self.__result |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 369 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 370 | def walk_nested_dict(self, data, url=""): |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 371 | r""" |
| 372 | Parse through the nested dictionary and get the resource id paths. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 373 | Description of argument(s): |
| 374 | data Nested dictionary data from response message. |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 375 | url Resource for which the response is obtained in data. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 376 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 377 | url = url.rstrip("/") |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 378 | |
| 379 | for key, value in data.items(): |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 380 | # Recursion if nested dictionary found. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 381 | if isinstance(value, dict): |
| 382 | self.walk_nested_dict(value) |
| 383 | else: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 384 | # Value contains a list of dictionaries having member data. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 385 | if "Members" == key: |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 386 | if isinstance(value, list): |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 387 | for memberDict in value: |
Anusha Dathatri | 0d305d3 | 2021-07-08 00:56:11 -0500 | [diff] [blame] | 388 | if isinstance(memberDict, str): |
| 389 | self.__pending_enumeration.add(memberDict) |
Reshma S | c487392 | 2024-11-27 15:32:15 +0530 | [diff] [blame] | 390 | elif ( |
| 391 | isinstance(memberDict, dict) |
| 392 | and "@odata.id" in memberDict |
| 393 | ): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 394 | self.__pending_enumeration.add( |
| 395 | memberDict["@odata.id"] |
| 396 | ) |
Reshma S | c487392 | 2024-11-27 15:32:15 +0530 | [diff] [blame] | 397 | else: |
| 398 | self.__pending_enumeration.add(memberDict[1]) |
Anusha Dathatri | 0d305d3 | 2021-07-08 00:56:11 -0500 | [diff] [blame] | 399 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 400 | if "@odata.id" == key: |
| 401 | value = value.rstrip("/") |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 402 | # Data for the given url. |
| 403 | if value == url: |
| 404 | self.__result[url] = data |
| 405 | # Data still needs to be looked up, |
| 406 | else: |
| 407 | self.__pending_enumeration.add(value) |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 408 | |
| 409 | def get_key_value_nested_dict(self, data, key): |
| 410 | r""" |
| 411 | Parse through the nested dictionary and get the searched key value. |
| 412 | |
| 413 | Description of argument(s): |
| 414 | data Nested dictionary data from response message. |
| 415 | key Search dictionary key element. |
| 416 | """ |
| 417 | |
| 418 | for k, v in data.items(): |
| 419 | if isinstance(v, dict): |
| 420 | self.get_key_value_nested_dict(v, key) |
| 421 | |
| 422 | if k == key: |
| 423 | target_list.append(v) |