George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | BMC redfish utility functions. |
| 5 | """ |
| 6 | |
| 7 | import json |
| 8 | from robot.libraries.BuiltIn import BuiltIn |
| 9 | |
| 10 | |
| 11 | class bmc_redfish_utils(object): |
| 12 | |
| 13 | def __init__(self): |
| 14 | r""" |
| 15 | Initialize the bmc_redfish_utils object. |
| 16 | """ |
| 17 | # Obtain a reference to the global redfish object. |
| 18 | self._redfish_ = BuiltIn().get_library_instance('redfish') |
| 19 | |
| 20 | def get_attribute(self, resource_path, attribute): |
| 21 | r""" |
| 22 | Get resource attribute. |
| 23 | |
| 24 | Description of argument(s): |
| 25 | resource_path URI resource absolute path (e.g. "/redfish/v1/Systems/1"). |
| 26 | attribute Name of the attribute (e.g. 'PowerState'). |
| 27 | """ |
| 28 | |
| 29 | resp = self._redfish_.get(resource_path) |
| 30 | if attribute in resp.dict: |
| 31 | return resp.dict[attribute] |
| 32 | |
| 33 | return None |
| 34 | |
| 35 | def list_request(self, resource_path): |
| 36 | r""" |
| 37 | Perform a GET list request and return available resource paths. |
| 38 | |
| 39 | Description of argument(s): |
| 40 | resource_path URI resource absolute path |
| 41 | (e.g. "/redfish/v1/SessionService/Sessions"). |
| 42 | """ |
| 43 | |
| 44 | global resource_list |
| 45 | resource_list = [] |
| 46 | |
| 47 | self._rest_response_ = self._redfish_.get(resource_path) |
| 48 | |
| 49 | # Return empty list. |
| 50 | if self._rest_response_.status != 200: |
| 51 | return resource_list |
| 52 | |
| 53 | self.walk_nested_dict(self._rest_response_.dict) |
| 54 | |
| 55 | if not resource_list: |
| 56 | return uri_path |
| 57 | |
| 58 | for resource in resource_list: |
| 59 | self._rest_response_ = self._redfish_.get(resource) |
| 60 | if self._rest_response_.status != 200: |
| 61 | continue |
| 62 | self.walk_nested_dict(self._rest_response_.dict) |
| 63 | |
| 64 | resource_list.sort() |
| 65 | return resource_list |
| 66 | |
| 67 | def enumerate_request(self, resource_path): |
| 68 | r""" |
| 69 | Perform a GET enumerate request and return available resource paths. |
| 70 | |
| 71 | Description of argument(s): |
| 72 | resource_path URI resource absolute path |
| 73 | (e.g. "/redfish/v1/SessionService/Sessions"). |
| 74 | """ |
| 75 | |
| 76 | url_list = self.list_request(resource_path) |
| 77 | |
| 78 | resource_dict = {} |
| 79 | |
| 80 | # Return empty dict. |
| 81 | if not url_list: |
| 82 | return resource_dict |
| 83 | |
| 84 | for resource in url_list: |
George Keishing | 48156e7 | 2019-02-18 13:05:55 -0600 | [diff] [blame] | 85 | # JsonSchemas data are not required in enumeration. |
| 86 | # Example: '/redfish/v1/JsonSchemas/' and sub resources. |
| 87 | if 'JsonSchemas' in resource: |
| 88 | continue |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 89 | self._rest_response_ = self._redfish_.get(resource) |
| 90 | if self._rest_response_.status != 200: |
| 91 | continue |
| 92 | resource_dict[resource] = self._rest_response_.dict |
| 93 | |
| 94 | return json.dumps(resource_dict, sort_keys=True, |
| 95 | indent=4, separators=(',', ': ')) |
| 96 | |
| 97 | def walk_nested_dict(self, data): |
| 98 | r""" |
| 99 | Parse through the nested dictionary and get the resource id paths. |
| 100 | |
| 101 | Description of argument(s): |
| 102 | data Nested dictionary data from response message. |
| 103 | """ |
| 104 | |
| 105 | for key, value in data.items(): |
| 106 | if isinstance(value, dict): |
| 107 | self.walk_nested_dict(value) |
| 108 | else: |
| 109 | if 'Members' == key: |
| 110 | if isinstance(value, list): |
| 111 | for index in value: |
| 112 | if index['@odata.id'] not in resource_list: |
| 113 | resource_list.append(index['@odata.id']) |
| 114 | if '@odata.id' == key: |
| 115 | if value not in resource_list and not value.endswith('/'): |
| 116 | resource_list.append(value) |