xref: /openbmc/openbmc-test-automation/lib/bmc_redfish.py (revision 0ee7e3d2b81ba768f5522a77ec16b74c6739d0e0)
1#!/usr/bin/env python
2
3r"""
4Using python based redfish library.
5Refer: https://github.com/DMTF/python-redfish-library
6"""
7
8import redfish
9from robot.libraries.BuiltIn import BuiltIn
10
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
21    ROBOT_LIBRARY_SCOPE = "TEST SUITE"
22    ROBOT_EXIT_ON_FAILURE = True
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        """
35        self._base_url_ = "https://" + hostname
36        self._username_ = username
37        self._password_ = password
38        self._default_prefix_ = "/redfish/v1"
39
40    def __enter__(self):
41        return self
42
43    def __del__(self):
44        del self
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        """
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_)
66        self._robj_.login(auth=redfish.AuthMethod.SESSION)
67        self._session_key_ = self._robj_.get_session_key()
68        self._session_location_ = self._robj_.get_session_location()
69
70    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
88    def get(self, resource_path, *args, **kwargs):
89        r"""
90        Perform a GET request and return response.
91
92        Description of argument(s):
93        resource_path    URI resource absolute path (e.g. "/redfish/v1/Systems/1").
94        args/kwargs      These are passed directly to the corresponding
95                         RestClientBase method.
96        """
97        self._rest_response_ = self._robj_.get(resource_path, *args, **kwargs)
98        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        """
110        self._rest_response_ = self._robj_.post('/redfish/v1/' + resource_path,
111                                                *args, **kwargs)
112        return self._rest_response_
113
114    def patch(self, resource_path, *args, **kwargs):
115        r"""
116        Perform a POST request.
117
118        Description of argument(s):
119        resource_path    URI resource relative path
120        args/kwargs      These are passed directly to the corresponding
121                         RestClientBase method.
122        """
123        self._rest_response_ = self._robj_.patch('/redfish/v1/' + resource_path,
124                                                 *args, **kwargs)
125        return self._rest_response_
126
127    def put(self, resource_path, actions, attr_data):
128        r"""
129        Perform a PUT request.
130
131        Description of argument(s):
132        resource_path    URI resource relative path.
133        args/kwargs      These are passed directly to the corresponding
134                         RestClientBase method.
135        """
136        self._rest_response_ = self._robj_.put('/redfish/v1/' + resource_path,
137                                               *args, **kwargs)
138        return self._rest_response_
139
140    def delete(self, resource_path):
141        r"""
142        Perform a DELETE request.
143
144        Description of argument(s):
145        resource_path  URI resource absolute path
146                       (e.g. "/redfish/v1/SessionService/Sessions/8d1a9wiiNL").
147        """
148        self._rest_response_ = self._robj_.delete(resource_path)
149        return self._rest_response_
150
151    def logout(self):
152        r"""
153        Logout redfish connection session.
154        """
155        self._robj_.logout()
156