Sushil Singh | 27ef253 | 2021-06-15 23:04:58 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import requests |
| 4 | import urllib.request |
| 5 | from urllib3.exceptions import InsecureRequestWarning |
| 6 | from getpass import getpass |
| 7 | import sys |
| 8 | import os |
| 9 | import ssl |
| 10 | import json |
| 11 | import random |
| 12 | import secrets |
| 13 | import string |
| 14 | |
| 15 | from robot.api import logger |
| 16 | from robot.libraries.BuiltIn import BuiltIn |
| 17 | from robot.api.deco import keyword |
| 18 | |
| 19 | |
| 20 | class redfish_request(object): |
| 21 | |
| 22 | @staticmethod |
| 23 | def generate_clientid(): |
| 24 | r""" |
| 25 | Generate 10 character unique id. |
| 26 | |
| 27 | e.g. "oMBhLv2Q9e" |
| 28 | |
| 29 | """ |
| 30 | |
| 31 | clientid = ''.join(secrets.choice( |
| 32 | string.ascii_letters + string.digits) for i in range(10)) |
| 33 | clientid = ''.join(str(i) for i in clientid) |
| 34 | |
| 35 | return clientid |
| 36 | |
| 37 | @staticmethod |
| 38 | def form_url(url): |
| 39 | r""" |
| 40 | Form a complete path for user url. |
| 41 | |
| 42 | Description of argument(s): |
| 43 | url Url passed by user e.g. /redfish/v1/Systems/system. |
| 44 | """ |
| 45 | |
| 46 | openbmc_host = \ |
| 47 | BuiltIn().get_variable_value("${OPENBMC_HOST}", default="") |
| 48 | https_port = BuiltIn().get_variable_value("${HTTPS_PORT}", default="") |
| 49 | form_url = \ |
| 50 | "https://" + str(openbmc_host) + ":" + str(https_port) + str(url) |
| 51 | |
| 52 | return form_url |
| 53 | |
| 54 | @staticmethod |
| 55 | def log_console(response): |
| 56 | r""" |
| 57 | Print function for console. |
| 58 | |
| 59 | Description of argument(s): |
| 60 | response Response from requests. |
| 61 | """ |
| 62 | |
| 63 | logger.console(msg='', newline=True) |
| 64 | logger.info("Response : [%s]" % response.status_code, |
| 65 | also_console=True) |
| 66 | logger.console(msg='', newline=True) |
| 67 | |
| 68 | def request_login(self, headers, url, credential, timeout=10): |
| 69 | r""" |
| 70 | Redfish request to create a session. |
| 71 | |
| 72 | Description of argument(s): |
| 73 | headers By default headers is assigned as application/json. |
| 74 | If user assign the headers, |
| 75 | then default headers is not considered. |
| 76 | url Requested path from user. |
| 77 | credential User has to assign the credential like username and |
| 78 | password. |
| 79 | UserName = xxxxxxxx Password = xxxxxxxx |
| 80 | Client id, user need to assign None in order to auto |
| 81 | generate, else user can assign any value. |
| 82 | timeout By default timeout is set to 10 seconds. |
| 83 | If user assign the timeout, then default timeout |
| 84 | value is not considered. |
| 85 | """ |
| 86 | |
| 87 | if headers == "None": |
| 88 | headers = dict() |
| 89 | headers['Content-Type'] = 'application/json' |
| 90 | |
| 91 | client_id = credential['Oem']['OpenBMC'].get('ClientID', "None") |
| 92 | |
| 93 | if "None" == client_id: |
| 94 | self.clientid = redfish_request.generate_clientid() |
| 95 | credential['Oem']['OpenBMC']['ClientID'] = self.clientid |
| 96 | |
| 97 | logger.console(msg='', newline=True) |
| 98 | requests.packages.urllib3.\ |
| 99 | disable_warnings(category=InsecureRequestWarning) |
| 100 | response = redfish_request.request_post(self, headers=headers, |
| 101 | url=url, data=credential) |
| 102 | |
| 103 | return response |
| 104 | |
| 105 | def request_get(self, headers, url, timeout=10, verify=False): |
| 106 | r""" |
| 107 | Redfish get request. |
| 108 | |
| 109 | Description of argument(s): |
| 110 | headers By default headers is assigned as application/json. |
| 111 | If user assign the headers, then default headers is not |
| 112 | considered. |
| 113 | url Requested path from user. |
| 114 | timeout By default timeout is set to 10 seconds. |
| 115 | If user assign the timeout, then default timeout value |
| 116 | is not considered. |
| 117 | verify By default verify is set to false means no certificate |
| 118 | verification is performed |
| 119 | else in case of true, certificate needs to be verified. |
| 120 | If user assign the verify, then default verify value |
| 121 | is not considered. |
| 122 | """ |
| 123 | |
| 124 | if headers.get('Content-Type', None) is None: |
| 125 | headers['Content-Type'] = 'application/json' |
| 126 | |
| 127 | url = redfish_request.form_url(url) |
| 128 | |
| 129 | logger.console(msg='', newline=True) |
| 130 | msg = "Request Method : GET ,headers = " + \ |
| 131 | json.dumps(headers) + " ,uri = " + str(url) + " ,timeout = " + \ |
| 132 | str(timeout) + " ,verify = " + str(verify) |
| 133 | logger.info(msg, also_console=True) |
| 134 | |
| 135 | response = requests.get(url, headers=headers, |
| 136 | timeout=timeout, verify=verify) |
| 137 | redfish_request.log_console(response) |
| 138 | |
| 139 | return response |
| 140 | |
| 141 | def request_patch(self, headers, url, data=None, timeout=10, verify=False): |
| 142 | r""" |
| 143 | Redfish patch request. |
| 144 | |
| 145 | Description of argument(s): |
| 146 | headers By default headers is assigned as application/json. |
| 147 | If user assign the headers, then default headers is not |
| 148 | considered. |
| 149 | url Requested path from user. |
| 150 | data By default data is None. |
| 151 | If user assign the data, then default data value is not |
| 152 | considered. |
| 153 | timeout By default timeout is set to 10 seconds. |
| 154 | If user assign the timeout, then default timeout value |
| 155 | is not considered. |
| 156 | verify By default verify is set to false means no certificate |
| 157 | verification is performed |
| 158 | else in case of true, certificate needs to be verified. |
| 159 | If user assign the verify, then default verify value |
| 160 | is not considered. |
| 161 | """ |
| 162 | |
| 163 | if headers.get('Content-Type', None) is None: |
| 164 | headers['Content-Type'] = 'application/json' |
| 165 | |
| 166 | url = redfish_request.form_url(url) |
| 167 | |
| 168 | logger.console(msg='', newline=True) |
| 169 | msg = "Request Method : PATCH ,headers = " + \ |
| 170 | json.dumps(headers) + " ,uri = " + str(url) + " ,data = " + \ |
| 171 | json.dumps(data) + " ,timeout = " + str(timeout) + \ |
| 172 | " ,verify = " + str(verify) |
| 173 | logger.info(msg, also_console=True) |
| 174 | |
| 175 | response = requests.patch(url, headers=headers, data=data, |
| 176 | timeout=timeout, verify=verify) |
| 177 | redfish_request.log_console(response) |
| 178 | |
| 179 | return response |
| 180 | |
| 181 | def request_post(self, headers, url, data=None, timeout=10, verify=False): |
| 182 | r""" |
| 183 | Redfish post request. |
| 184 | |
| 185 | Description of argument(s): |
| 186 | headers By default headers is assigned as application/json. |
| 187 | If user assign the headers, then default headers is not |
| 188 | considered. |
| 189 | url Requested path from user. |
| 190 | data By default data is None. |
| 191 | If user assign the data, then default data value is not |
| 192 | considered. |
| 193 | timeout By default timeout is set to 10 seconds. |
| 194 | If user assign the timeout, then default timeout value |
| 195 | is not considered. |
| 196 | verify By default verify is set to false means no |
| 197 | certificate verification is performed |
| 198 | else in case of true, certificate needs to be verified. |
| 199 | If user assign the verify, then default verify value |
| 200 | is not considered. |
| 201 | """ |
| 202 | |
| 203 | if headers.get('Content-Type', None) is None: |
| 204 | headers['Content-Type'] = 'application/json' |
| 205 | |
| 206 | url = redfish_request.form_url(url) |
| 207 | |
| 208 | logger.console(msg='', newline=True) |
| 209 | msg = "Request Method : POST ,headers = " + \ |
| 210 | json.dumps(headers) + " ,uri = " + str(url) + " ,data = " + \ |
| 211 | json.dumps(data) + " ,timeout = " + str(timeout) + \ |
| 212 | " ,verify = " + str(verify) |
| 213 | logger.info(msg, also_console=True) |
| 214 | |
| 215 | response = requests.post(url, headers=headers, data=json.dumps(data), |
| 216 | timeout=timeout, verify=verify) |
| 217 | redfish_request.log_console(response) |
| 218 | |
| 219 | return response |
| 220 | |
| 221 | def request_put(self, headers, url, files=None, data=None, |
| 222 | timeout=10, verify=False): |
| 223 | r""" |
| 224 | Redfish put request. |
| 225 | |
| 226 | Description of argument(s): |
| 227 | headers By default headers is assigned as application/json. |
| 228 | If user assign the headers, then default headers is not |
| 229 | considered. |
| 230 | url Requested path from user. |
| 231 | files By default files is None. |
| 232 | If user assign the files, then default files value |
| 233 | is not considered. |
| 234 | data By default data is None. |
| 235 | If user pass the data, then default data value is not |
| 236 | considered. |
| 237 | timeout By default timeout is set to 10 seconds. |
| 238 | If user pass the timeout, then default timeout value |
| 239 | is not considered. |
| 240 | verify By default verify is set to false means no |
| 241 | certificate verification is performed |
| 242 | else in case of true, certificate needs to be verified. |
| 243 | If user assign the verify, then default verify value |
| 244 | is not considered. |
| 245 | """ |
| 246 | |
| 247 | if headers.get('Content-Type', None) is None: |
| 248 | headers['Content-Type'] = 'application/json' |
| 249 | |
| 250 | url = redfish_request.form_url(url) |
| 251 | |
| 252 | logger.console(msg='', newline=True) |
| 253 | msg = "Request Method : PUT ,headers = " + \ |
| 254 | json.dumps(headers) + " ,uri = " + str(url) + " ,data = " + \ |
| 255 | json.dumps(data) + " ,timeout = " + str(timeout) + \ |
| 256 | " ,verify = " + str(verify) |
| 257 | logger.info(msg, also_console=True) |
| 258 | |
| 259 | response = requests.put(url, headers=headers, files=files, data=data, |
| 260 | timeout=timeout, verify=verify) |
| 261 | redfish_request.log_console(response) |
| 262 | |
| 263 | return response |
| 264 | |
| 265 | def request_delete(self, headers, url, data=None, timeout=10, verify=False): |
| 266 | r""" |
| 267 | Redfish delete request. |
| 268 | |
| 269 | Description of argument(s): |
| 270 | headers By default headers is assigned as application/json. |
| 271 | If user pass the headers then default header is not |
| 272 | considered. |
| 273 | url Requested path from user. |
| 274 | data By default data is None. |
| 275 | If user pass the data, then default data value is not |
| 276 | considered. |
| 277 | timeout By default timeout is set to 10 seconds. |
| 278 | If user pass the timeout, then default timeout value |
| 279 | is not considered. |
| 280 | verify By default verify is set to false means no |
| 281 | certificate verification is performed |
| 282 | else in case of true, certificate needs to be verified. |
| 283 | If user assign the verify, then default verify value |
| 284 | is not considered. |
| 285 | """ |
| 286 | |
| 287 | if headers.get('Content-Type', None) is None: |
| 288 | headers['Content-Type'] = 'application/json' |
| 289 | |
| 290 | url = redfish_request.form_url(url) |
| 291 | |
| 292 | logger.console(msg='', newline=True) |
| 293 | msg = "Request Method : DELETE ,headers = " + \ |
| 294 | json.dumps(headers) + " ,uri = " + str(url) + " ,data = " + \ |
| 295 | json.dumps(data) + " ,timeout = " + str(timeout) + \ |
| 296 | " ,verify = " + str(verify) |
| 297 | logger.console(msg='', newline=True) |
| 298 | |
| 299 | response = requests.delete(url, headers=headers, data=data, |
| 300 | timeout=timeout, verify=verify) |
| 301 | redfish_request.log_console(response) |
| 302 | |
| 303 | return response |
| 304 | |
| 305 | @staticmethod |
| 306 | def dict_parse(variable, lookup_dict): |
| 307 | r""" |
| 308 | Find a variable in dict. |
| 309 | |
| 310 | Description of argument(s): |
| 311 | variable Variable that need to be searched in dict. |
| 312 | lookup_dict Disctionay contains variables. |
| 313 | """ |
| 314 | |
| 315 | result = lookup_dict.get(variable, None) |
| 316 | return result |
| 317 | |
| 318 | @staticmethod |
| 319 | def get_target_actions(target_attribute, response): |
| 320 | r""" |
| 321 | Get target entry of the searched target attribute. |
| 322 | |
| 323 | Description of argument(s): |
| 324 | target_attribute Name of the attribute (e.g. 'Manager.Reset'). |
| 325 | response Response from url. |
| 326 | |
| 327 | 'Actions' : { |
| 328 | '#Manager.Reset' : { |
| 329 | '@Redfish.ActionInfo' : '/redfish/v1/Managers/bmc/ResetActionInfo', |
| 330 | 'target' : '/redfish/v1/Managers/bmc/Actions/Manager.Reset' |
| 331 | } |
| 332 | } |
| 333 | """ |
| 334 | |
| 335 | lookup_list = ["Actions", "#" + attribute, "target"] |
| 336 | for lookup_item in lookup_list: |
| 337 | response = redfish_request.dict_parse(lookup_item, response) |
| 338 | if response is not None and type(response) is dict(): |
| 339 | continue |
| 340 | else: |
| 341 | return response |
| 342 | return None |
| 343 | |
| 344 | @staticmethod |
| 345 | def get_attribute(attribute, data): |
| 346 | r""" |
| 347 | Get resource attribute. |
| 348 | |
| 349 | Description of argument(s): |
| 350 | attribute Pass the attribute needs to be searched. |
| 351 | data Pass the request response. |
| 352 | """ |
| 353 | |
| 354 | value = data.get(attribute, None) |
| 355 | return value |