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