1#!/usr/bin/env python 2 3r""" 4See class prolog below for details. 5""" 6 7import sys 8import re 9from redfish_plus import redfish_plus 10from robot.libraries.BuiltIn import BuiltIn 11 12import func_args as fa 13import gen_print as gp 14 15 16class bmc_redfish(redfish_plus): 17 r""" 18 bmc_redfish is a child class of redfish_plus that is designed to provide 19 benefits specifically for using redfish to communicate with an OpenBMC. 20 21 See the prologs of the methods below for details. 22 """ 23 24 def __init__(self, *args, **kwargs): 25 r""" 26 Do BMC-related redfish initialization. 27 28 Presently, older versions of BMC code may not support redfish 29 requests. This can lead to unsightly error text being printed out for 30 programs that may use lib/bmc_redfish_resource.robot even though they 31 don't necessarily intend to make redfish requests. 32 33 This class method will make an attempt to tolerate this situation. At 34 some future point, when all BMCs can be expected to support redfish, 35 this class method may be considered for deletion. If it is deleted, 36 the self.__inited__ test code in the login() class method below should 37 likewise be deleted. 38 """ 39 self.__inited__ = False 40 try: 41 super(bmc_redfish, self).__init__(*args, **kwargs) 42 self.__inited__ = True 43 except ValueError as get_exception: 44 except_type, except_value, except_traceback = sys.exc_info() 45 regex = r"The HTTP status code was not valid:[\r\n]+status:[ ]+502" 46 result = re.match(regex, str(except_value), flags=re.MULTILINE) 47 if not result: 48 gp.lprint_var(except_type) 49 gp.lprint_varx("except_value", str(except_value)) 50 raise(get_exception) 51 52 def login(self, *args, **kwargs): 53 r""" 54 Assign BMC default values for username, password and auth arguments 55 and call parent class login method. 56 57 Description of argument(s): 58 args See parent class method prolog for details. 59 kwargs See parent class method prolog for details. 60 """ 61 62 if not self.__inited__: 63 message = "bmc_redfish.__init__() was never successfully run.\n" 64 raise ValueError(message) 65 # Assign default values for username, password, auth where necessary. 66 openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}") 67 openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}") 68 username, args, kwargs = fa.pop_arg(openbmc_username, *args, **kwargs) 69 password, args, kwargs = fa.pop_arg(openbmc_password, *args, **kwargs) 70 auth, args, kwargs = fa.pop_arg('session', *args, **kwargs) 71 72 super(bmc_redfish, self).login(username, password, auth, 73 *args, **kwargs) 74 75 def get_properties(self, *args, **kwargs): 76 r""" 77 Return dictionary of attributes for a given path. 78 79 The difference between calling this function and calling get() 80 directly is that this function returns ONLY the dictionary portion of 81 the response object. 82 83 Example robot code: 84 85 ${properties}= Get Properties /redfish/v1/Systems/system/ 86 Rprint Vars 1 properties 87 88 Output: 89 90 properties: 91 [PowerState]: Off 92 [Processors]: 93 [@odata.id]: /redfish/v1/Systems/system/Processors 94 [SerialNumber]: 1234567 95 ... 96 97 Description of argument(s): 98 args See parent class get() prolog for details. 99 kwargs See parent class get() prolog for details. 100 """ 101 102 resp = self.get(*args, **kwargs) 103 return resp.dict if hasattr(resp, 'dict') else {} 104 105 def get_attribute(self, path, attribute, default=None, *args, **kwargs): 106 r""" 107 Get and return the named attribute from the properties for a given 108 path. 109 110 This method has the following advantages over calling get_properties 111 directly: 112 - The caller can specify a default value to be returned if the 113 attribute does not exist. 114 115 Example robot code: 116 117 ${attribute}= Get Attribute /redfish/v1/AccountService 118 ... MaxPasswordLength default=600 119 Rprint Vars attribute 120 121 Output: 122 123 attribute: 31 124 125 Description of argument(s): 126 path The path (e.g. 127 "/redfish/v1/AccountService"). 128 attribute The name of the attribute to be retrieved 129 (e.g. "MaxPasswordLength"). 130 default The default value to be returned if the 131 attribute does not exist (e.g. ""). 132 args See parent class get() prolog for details. 133 kwargs See parent class get() prolog for details. 134 """ 135 136 return self.get_properties(path, *args, **kwargs).get(attribute, 137 default) 138 139 def get_session_info(self): 140 r""" 141 Get and return session info as a tuple consisting of session_key and 142 session_location. 143 """ 144 145 return self.get_session_key(), self.get_session_location() 146