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