Skip to content

Commit

Permalink
modified plugins loading to fallback to defaults in case of either no…
Browse files Browse the repository at this point in the history
… plugins in config or wrong config

Signed-off-by: Christian Pinto <[email protected]>
  • Loading branch information
christian-pinto committed Jun 26, 2024
1 parent b31c792 commit 1f8e8d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
53 changes: 38 additions & 15 deletions sunfish/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
import sunfish.models.plugins as plugin_modules
logger = logging.getLogger(__name__)

plugins_default = {
"storage_backend": {
"module_name": "storage.file_system_backend.backend_FS",
"class_name": "BackendFS"
},
"events_handler": {
"module_name": "events_handlers.redfish.redfish_event_handler",
"class_name": "RedfishEventHandler"
},
"objects_handler": {
"module_name": "objects_handlers.sunfish_server.redfish_object_handler",
"class_name": "RedfishObjectHandler"
}
}

class Core:

Expand Down Expand Up @@ -68,36 +82,45 @@ def __init__(self, conf):
# interface.

# Default storage plugin loaded if nothing is specified in the configuration
# or if the configuration is not correct
if "storage_backend" not in conf:
storage_plugin = {
"module_name": "storage.file_system_backend.backend_FS",
"class_name": "BackendFS"
}
storage_plugin = plugins_default["storage_backend"]
else:
storage_plugin = conf["storage_backend"]
storage_cl = plugin_modules.load_plugin(storage_plugin)
try:
storage_cl = plugin_modules.load_plugin(storage_plugin)
except ModuleNotFoundError:
logger.warning(f"Falling back to the default storage_backend plugin")
# If this one fails as well, then we have a problem and we must fail
storage_cl = plugin_modules.load_plugin(plugins_default["storage_backend"])
self.storage_backend = storage_cl(self.conf)

# Default event_handler plugin loaded if nothing is specified in the configuration
# or if the configuration is not correct
if "events_handler" not in conf:
event_plugin = {
"module_name": "events_handlers.redfish.redfish_event_handler",
"class_name": "RedfishEventHandler"
}
event_plugin = plugins_default["events_handler"]
else:
event_plugin = conf["events_handler"]
event_cl = plugin_modules.load_plugin(event_plugin)
try:
event_cl = plugin_modules.load_plugin(event_plugin)
except ModuleNotFoundError:
logger.warning(f"Falling back to the default events_handler plugin")
# If this one fails as well, then we have a problem and we must fail
event_cl = plugin_modules.load_plugin(plugins_default["events_handler"])
self.event_handler = event_cl(self)

# Default objects_handler plugin loaded if nothing is specified in the configuration
# or if the configuration is not correct
if "objects_handler" not in conf:
objects_plugin = {
"module_name": "objects_handlers.sunfish_server.redfish_object_handler",
"class_name": "RedfishObjectHandler"
}
objects_plugin = plugins_default["objects_handler"]
else:
objects_plugin = conf["objects_handler"]
objects_handler_cl = plugin_modules.load_plugin(objects_plugin)
try:
objects_handler_cl = plugin_modules.load_plugin(objects_plugin)
except ModuleNotFoundError:
logger.warning(f"Falling back to the default objects_handler plugin")
# If this one fails as well, then we have a problem and we must fail
objects_handler_cl = plugin_modules.load_plugin(plugins_default["objects_handler"])
self.objects_handler = objects_handler_cl(self)

if conf['handlers']['subscription_handler'] == 'redfish':
Expand Down
5 changes: 3 additions & 2 deletions tests/test_sunfishcore_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ def test_init_core_wrong_plugin(self):
conf = json.load(json_data)
except FileNotFoundError as e:
raise ResourceNotFound('conf.json')
with pytest.raises(ModuleNotFoundError):
try:
core = Core(conf)

except ModuleNotFoundError as e:
assert False, f" test_init_core_wrong_plugin raised an exception {e}"

# TEST REST
# Delete
Expand Down

0 comments on commit 1f8e8d4

Please sign in to comment.