blob: eeab8243ef7a003944ff7804ceb0ec1c366ec655 [file] [log] [blame]
George Keishinge62d8b02018-11-29 12:01:56 -06001#!/usr/bin/env python
2
3r"""
4Using python based redfish library.
5Refer: https://github.com/DMTF/python-redfish-library
6"""
7
8import redfish
George Keishing2296e8c2019-02-01 05:49:58 -06009from robot.libraries.BuiltIn import BuiltIn
George Keishinge62d8b02018-11-29 12:01:56 -060010
11
12class HTTPSBadRequestError(Exception):
13 r"""
14 BMC redfish generic raised method for error(s).
15 """
16 pass
17
18
19class bmc_redfish(object):
20
George Keishing46e64a32019-02-05 22:45:10 -060021 ROBOT_LIBRARY_SCOPE = "TEST SUITE"
George Keishing2823d2a2019-02-05 22:19:19 -060022 ROBOT_EXIT_ON_FAILURE = True
George Keishinge62d8b02018-11-29 12:01:56 -060023
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 Keishinge62d8b02018-11-29 12:01:56 -060035 self._base_url_ = "https://" + hostname
36 self._username_ = username
37 self._password_ = password
38 self._default_prefix_ = "/redfish/v1"
George Keishinge62d8b02018-11-29 12:01:56 -060039
40 def __enter__(self):
41 return self
42
George Keishing2823d2a2019-02-05 22:19:19 -060043 def __del__(self):
George Keishing46e64a32019-02-05 22:45:10 -060044 del self
George Keishinge62d8b02018-11-29 12:01:56 -060045
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 Keishing4c394012019-02-01 06:03:02 -060054
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 Keishinge62d8b02018-11-29 12:01:56 -060066 self._robj_.login(auth=redfish.AuthMethod.SESSION)
George Keishing374e6842019-02-20 08:57:18 -060067 self._session_key_ = self._robj_.get_session_key()
George Keishing4c394012019-02-01 06:03:02 -060068 self._session_location_ = self._robj_.get_session_location()
George Keishinge62d8b02018-11-29 12:01:56 -060069
George Keishing374e6842019-02-20 08:57:18 -060070 def set_session_key(self, session_key):
71 r"""
72 Update the session key instance.
73
74 session_key Redfish valid session key.
75 """
76 self._robj_.set_session_key(session_key)
77
78 def set_session_location(self, session_location):
79 r"""
80 Update the session location instance.
81
82 session_location Redfish valid session location.
83 Example:
84 /redfish/v1/SessionService/Sessions/j04tD83QQn
85 """
86 self._robj_.set_session_location(session_location)
87
George Keishinge62d8b02018-11-29 12:01:56 -060088 def get(self, resource_path, *args, **kwargs):
89 r"""
90 Perform a GET request and return response.
91
92 Description of argument(s):
George Keishing6510cfb2019-01-31 12:28:36 -060093 resource_path URI resource absolute path (e.g. "/redfish/v1/Systems/1").
George Keishinge62d8b02018-11-29 12:01:56 -060094 args/kwargs These are passed directly to the corresponding
95 RestClientBase method.
96 """
George Keishing6510cfb2019-01-31 12:28:36 -060097 self._rest_response_ = self._robj_.get(resource_path, *args, **kwargs)
George Keishinge62d8b02018-11-29 12:01:56 -060098 return self._rest_response_
99
100 def post(self, resource_path, *args, **kwargs):
101 r"""
102 Perform a POST request.
103
104 Description of argument(s):
105 resource_path URI resource relative path
106 (e.g. "Systems/1/Actions/ComputerSystem.Reset").
107 args/kwargs These are passed directly to the corresponding
108 RestClientBase method.
109 """
George Keishingc2b176e2019-03-02 23:31:30 -0600110 self._rest_response_ = self._robj_.post(resource_path, *args, **kwargs)
George Keishinge62d8b02018-11-29 12:01:56 -0600111 return self._rest_response_
112
113 def patch(self, resource_path, *args, **kwargs):
114 r"""
115 Perform a POST request.
116
117 Description of argument(s):
118 resource_path URI resource relative path
119 args/kwargs These are passed directly to the corresponding
120 RestClientBase method.
121 """
George Keishingc2b176e2019-03-02 23:31:30 -0600122 self._rest_response_ = self._robj_.patch(resource_path, *args, **kwargs)
George Keishinge62d8b02018-11-29 12:01:56 -0600123 return self._rest_response_
124
125 def put(self, resource_path, actions, attr_data):
126 r"""
127 Perform a PUT request.
128
129 Description of argument(s):
130 resource_path URI resource relative path.
131 args/kwargs These are passed directly to the corresponding
132 RestClientBase method.
133 """
George Keishingc2b176e2019-03-02 23:31:30 -0600134 self._rest_response_ = self._robj_.put(resource_path, *args, **kwargs)
George Keishinge62d8b02018-11-29 12:01:56 -0600135 return self._rest_response_
136
137 def delete(self, resource_path):
138 r"""
139 Perform a DELETE request.
140
141 Description of argument(s):
George Keishing5d467552019-02-08 23:30:48 -0600142 resource_path URI resource absolute path
George Keishinge62d8b02018-11-29 12:01:56 -0600143 (e.g. "/redfish/v1/SessionService/Sessions/8d1a9wiiNL").
144 """
145 self._rest_response_ = self._robj_.delete(resource_path)
George Keishinge62d8b02018-11-29 12:01:56 -0600146 return self._rest_response_
147
148 def logout(self):
149 r"""
150 Logout redfish connection session.
151 """
152 self._robj_.logout()