George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | Using python based redfish library. |
| 5 | Refer: https://github.com/DMTF/python-redfish-library |
| 6 | """ |
| 7 | |
| 8 | import redfish |
George Keishing | 2296e8c | 2019-02-01 05:49:58 -0600 | [diff] [blame] | 9 | from robot.libraries.BuiltIn import BuiltIn |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class HTTPSBadRequestError(Exception): |
| 13 | r""" |
| 14 | BMC redfish generic raised method for error(s). |
| 15 | """ |
| 16 | pass |
| 17 | |
| 18 | |
| 19 | class bmc_redfish(object): |
| 20 | |
George Keishing | 46e64a3 | 2019-02-05 22:45:10 -0600 | [diff] [blame] | 21 | ROBOT_LIBRARY_SCOPE = "TEST SUITE" |
George Keishing | 2823d2a | 2019-02-05 22:19:19 -0600 | [diff] [blame] | 22 | ROBOT_EXIT_ON_FAILURE = True |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 23 | |
| 24 | def __init__(self, hostname, username, password, *args, **kwargs): |
| 25 | r""" |
| 26 | Establish session connection to host. |
| 27 | |
| 28 | Description of argument(s): |
| 29 | hostname The host name or IP address of the server. |
| 30 | username The username to be used to connect to the server. |
| 31 | password The password to be used to connect to the server. |
| 32 | args/kwargs Additional parms which are passed directly |
| 33 | to the redfish_client function. |
| 34 | """ |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 35 | self._base_url_ = "https://" + hostname |
| 36 | self._username_ = username |
| 37 | self._password_ = password |
| 38 | self._default_prefix_ = "/redfish/v1" |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 39 | |
| 40 | def __enter__(self): |
| 41 | return self |
| 42 | |
George Keishing | 2823d2a | 2019-02-05 22:19:19 -0600 | [diff] [blame] | 43 | def __del__(self): |
George Keishing | 46e64a3 | 2019-02-05 22:45:10 -0600 | [diff] [blame] | 44 | del self |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 45 | |
| 46 | def login(self, *args, **kwargs): |
| 47 | r""" |
| 48 | Call the corresponding RestClientBase method and return the result. |
| 49 | |
| 50 | Description of argument(s): |
| 51 | args/kwargs These are passed directly to the corresponding |
| 52 | RestClientBase method. |
| 53 | """ |
George Keishing | 4c39401 | 2019-02-01 06:03:02 -0600 | [diff] [blame] | 54 | |
| 55 | for arg in args: |
| 56 | hostname = self._base_url_.strip("https://") |
| 57 | # Class object constructor reinitialized. |
| 58 | self.__init__(hostname=hostname, |
| 59 | username=arg['username'], |
| 60 | password=arg['password']) |
| 61 | |
| 62 | self._robj_ = redfish.redfish_client(base_url=self._base_url_, |
| 63 | username=self._username_, |
| 64 | password=self._password_, |
| 65 | default_prefix=self._default_prefix_) |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 66 | self._robj_.login(auth=redfish.AuthMethod.SESSION) |
George Keishing | 4c39401 | 2019-02-01 06:03:02 -0600 | [diff] [blame] | 67 | self._session_location_ = self._robj_.get_session_location() |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 68 | |
| 69 | def get(self, resource_path, *args, **kwargs): |
| 70 | r""" |
| 71 | Perform a GET request and return response. |
| 72 | |
| 73 | Description of argument(s): |
George Keishing | 6510cfb | 2019-01-31 12:28:36 -0600 | [diff] [blame] | 74 | resource_path URI resource absolute path (e.g. "/redfish/v1/Systems/1"). |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 75 | args/kwargs These are passed directly to the corresponding |
| 76 | RestClientBase method. |
| 77 | """ |
George Keishing | 6510cfb | 2019-01-31 12:28:36 -0600 | [diff] [blame] | 78 | self._rest_response_ = self._robj_.get(resource_path, *args, **kwargs) |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 79 | return self._rest_response_ |
| 80 | |
| 81 | def post(self, resource_path, *args, **kwargs): |
| 82 | r""" |
| 83 | Perform a POST request. |
| 84 | |
| 85 | Description of argument(s): |
| 86 | resource_path URI resource relative path |
| 87 | (e.g. "Systems/1/Actions/ComputerSystem.Reset"). |
| 88 | args/kwargs These are passed directly to the corresponding |
| 89 | RestClientBase method. |
| 90 | """ |
| 91 | self._rest_response_ = self._robj_.post('/redfish/v1/' + resource_path, |
| 92 | *args, **kwargs) |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 93 | return self._rest_response_ |
| 94 | |
| 95 | def patch(self, resource_path, *args, **kwargs): |
| 96 | r""" |
| 97 | Perform a POST request. |
| 98 | |
| 99 | Description of argument(s): |
| 100 | resource_path URI resource relative path |
| 101 | args/kwargs These are passed directly to the corresponding |
| 102 | RestClientBase method. |
| 103 | """ |
| 104 | self._rest_response_ = self._robj_.patch('/redfish/v1/' + resource_path, |
| 105 | *args, **kwargs) |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 106 | return self._rest_response_ |
| 107 | |
| 108 | def put(self, resource_path, actions, attr_data): |
| 109 | r""" |
| 110 | Perform a PUT request. |
| 111 | |
| 112 | Description of argument(s): |
| 113 | resource_path URI resource relative path. |
| 114 | args/kwargs These are passed directly to the corresponding |
| 115 | RestClientBase method. |
| 116 | """ |
| 117 | self._rest_response_ = self._robj_.put('/redfish/v1/' + resource_path, |
| 118 | *args, **kwargs) |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 119 | return self._rest_response_ |
| 120 | |
| 121 | def delete(self, resource_path): |
| 122 | r""" |
| 123 | Perform a DELETE request. |
| 124 | |
| 125 | Description of argument(s): |
George Keishing | 5d46755 | 2019-02-08 23:30:48 -0600 | [diff] [blame] | 126 | resource_path URI resource absolute path |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 127 | (e.g. "/redfish/v1/SessionService/Sessions/8d1a9wiiNL"). |
| 128 | """ |
| 129 | self._rest_response_ = self._robj_.delete(resource_path) |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 130 | return self._rest_response_ |
| 131 | |
| 132 | def logout(self): |
| 133 | r""" |
| 134 | Logout redfish connection session. |
| 135 | """ |
| 136 | self._robj_.logout() |