Guest User

Untitled

a guest
Jun 21st, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. import logging
  2. import json
  3. import redfish.ris.tpdefs
  4. from redfish import AuthMethod, redfish_logger, redfish_client
  5.  
  6. #Config logger used by HPE Restful library
  7. #LOGGERFILE = "RedfishApi.log"
  8. #LOGGERFORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
  9. #LOGGER = redfish_logger(LOGGERFILE, LOGGERFORMAT, logging.INFO)
  10. #LOGGER.info("HPE Redfish API examples")
  11.  
  12. class RedfishObject(object):
  13. def __init__(self, host, login_account, login_password, log):
  14. try:
  15. self.redfish_client = redfish_client(base_url=host, \
  16. username=login_account, password=login_password, \
  17. default_prefix="/redfish/v1")
  18. except:
  19. raise
  20. self.LOGGER = log
  21. self.typepath = redfish.ris.tpdefs.Typesandpathdefines()
  22. self.typepath.getgen(url=host, logger=self.LOGGER)
  23. self.typepath.defs.redfishchange()
  24. self.redfish_client.login(auth=AuthMethod.SESSION)
  25. self.SYSTEMS_RESOURCES = self.get_resource_directory()
  26. self.MESSAGE_REGISTRIES = self.get_base_registry()
  27.  
  28. def delete_obj(self):
  29. try:
  30. self.redfish_client.logout()
  31. except AttributeError as excp:
  32. pass
  33.  
  34. def search_for_type(self, type):
  35. instances = []
  36.  
  37. for item in self.SYSTEMS_RESOURCES["resources"]:
  38. foundsettings = False
  39.  
  40. if "@odata.type" in item and type.lower() in \
  41. item["@odata.type"].lower():
  42. for entry in self.SYSTEMS_RESOURCES["resources"]:
  43. if (item["@odata.id"] + "/settings/").lower() == \
  44. (entry["@odata.id"]).lower():
  45. foundsettings = True
  46.  
  47. if not foundsettings:
  48. instances.append(item)
  49.  
  50. if not instances:
  51. self.LOGGER.error("\t'%s' resource or feature is not " \
  52. "supported on this system\n" % type)
  53. return instances
  54.  
  55. def error_handler(self, response):
  56. if not self.MESSAGE_REGISTRIES:
  57. self.LOGGER.error("ERROR: No message registries found.")
  58.  
  59. try:
  60. message = json.loads(response.text)
  61. newmessage = message["error"]["@Message.ExtendedInfo"][0]\
  62. ["MessageId"].split(".")
  63. except:
  64. self.LOGGER.info("\tNo extended error information returned by " \
  65. "iLO.\n")
  66. return
  67.  
  68. for err_mesg in self.MESSAGE_REGISTRIES:
  69. if err_mesg != newmessage[0]:
  70. continue
  71. else:
  72. for err_entry in self.MESSAGE_REGISTRIES[err_mesg]:
  73. if err_entry == newmessage[3]:
  74. self.LOGGER.info("\tiLO return code %s: %s\n" % (\
  75. message["error"]["@Message.ExtendedInfo"][0]\
  76. ["MessageId"], self.MESSAGE_REGISTRIES\
  77. [err_mesg][err_entry]["Description"]))
  78.  
  79. def redfish_get(self, suburi):
  80. """REDFISH GET"""
  81. return self.redfish_client.get(path=suburi)
  82.  
  83. def redfish_patch(self, suburi, request_body, optionalpassword=None):
  84. """REDFISH PATCH"""
  85. self.LOGGER.info("PATCH " + str(request_body) + " to " + suburi + "\n")
  86. response = self.redfish_client.patch(path=suburi, body=request_body, \
  87. optionalpassword=optionalpassword)
  88. self.LOGGER.info("PATCH response = " + str(response.status) + "\n")
  89.  
  90. return response
  91.  
  92. def redfish_put(self, suburi, request_body, optionalpassword=None):
  93. """REDFISH PUT"""
  94. self.LOGGER.info("PUT " + str(request_body) + " to " + suburi + "\n")
  95. response = self.redfish_client.put(path=suburi, body=request_body, \
  96. optionalpassword=optionalpassword)
  97. self.LOGGER.info("PUT response = " + str(response.status) + "\n")
  98.  
  99. return response
  100.  
  101.  
  102. def redfish_post(self, suburi, request_body):
  103. """REDFISH POST"""
  104. self.LOGGER.info("POST " + str(request_body) + " to " + suburi + "\n")
  105. response = self.redfish_client.post(path=suburi, body=request_body)
  106. self.LOGGER.info("POST response = " + str(response.status) + "\n")
  107.  
  108. return response
  109.  
  110.  
  111. def redfish_delete(self, suburi):
  112. """REDFISH DELETE"""
  113. self.LOGGER.info("DELETE " + suburi + "\n")
  114. response = self.redfish_client.delete(path=suburi)
  115. self.LOGGER.info("DELETE response = " + str(response.status) + "\n")
  116.  
  117. return response
  118.  
  119.  
  120. def get_resource_directory(self):
  121. response = self.redfish_get("/redfish/v1/resourcedirectory/")
  122. resources = {}
  123. if response.status == 200:
  124. resources["resources"] = response.dict["Instances"]
  125. return resources
  126. else:
  127. self.LOGGER.error("\tResource directory missing at " \
  128. "/redfish/v1/resourcedirectory" + "\n")
  129. def get_base_registry(self):
  130. response = self.redfish_get("/redfish/v1/Registries/")
  131. messages = {}
  132. location = None
  133. for entry in response.dict["Members"]:
  134. if not [x for x in ["/Base/", "/iLO/"] if x in entry["@odata.id"]]:
  135. continue
  136. else:
  137. registry = self.redfish_get(entry["@odata.id"])
  138. for location in registry.dict["Location"]:
  139. if "extref" in location["Uri"]:
  140. location = location["Uri"]["extref"]
  141. else:
  142. location = location["Uri"]
  143. reg_resp = self.redfish_get(location)
  144. if reg_resp.status == 200:
  145. messages[reg_resp.dict["RegistryPrefix"]] = \
  146. reg_resp.dict["Messages"]
  147. else:
  148. self.LOGGER.info("\t" + reg_resp.dict["RegistryPrefix"] + \
  149. " not found at " + location + "\n")
  150. return messages
Add Comment
Please, Sign In to add comment