Skip to content

Commit

Permalink
Improve dataflow when rendering config file. (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanchiwai-ray authored Mar 1, 2024
1 parent 48f70bc commit 634e108
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
26 changes: 13 additions & 13 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,20 @@ def _on_cos_agent_relation_departed(self, event: EventBase) -> None:
logger.info("Stop and disable exporter service")
self._on_update_status(event)

def _get_redfish_creds(self) -> Dict[str, str]:
"""Provide redfish config if redfish is available, else empty dict."""
def _get_redfish_creds(self) -> Dict[str, Any]:
"""Provide redfish config if redfish is available."""
bmc_tools = bmc_hw_verifier()
if HWTool.REDFISH in bmc_tools:
bmc_address = get_bmc_address()
redfish_creds = {
# Force to use https as default protocol
"host": f"https://{bmc_address}",
"username": self.model.config.get("redfish-username", ""),
"password": self.model.config.get("redfish-password", ""),
}
else:
redfish_creds = {}
return redfish_creds
if HWTool.REDFISH not in bmc_tools:
logger.warning(
"Redfish is not available, disregarding redfish credentials config options..."
)
return {"enable": False}
return {
"enable": True,
"host": f"https://{get_bmc_address()}",
"username": self.model.config.get("redfish-username", ""),
"password": self.model.config.get("redfish-password", ""),
}

def validate_exporter_configs(self) -> Tuple[bool, str]:
"""Validate the static and runtime config options for the exporter."""
Expand Down
3 changes: 1 addition & 2 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
EXPORTER_NAME,
EXPORTER_SERVICE_PATH,
EXPORTER_SERVICE_TEMPLATE,
HWTool,
)
from hw_tools import get_hw_tool_white_list

Expand Down Expand Up @@ -91,7 +90,7 @@ def render_config(self, port: str, level: str, redfish_creds: dict) -> bool:
PORT=port,
LEVEL=level,
COLLECTORS=collectors,
REDFISH_ENABLE=(HWTool.REDFISH in hw_tools),
REDFISH_ENABLE=redfish_creds.get("enable", False),
REDFISH_HOST=redfish_creds.get("host", ""),
REDFISH_USERNAME=redfish_creds.get("username", ""),
REDFISH_PASSWORD=redfish_creds.get("password", ""),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_05_install_redfish_unavailable(self, mock_hw_tool_helper, mock_exporter
self.assertTrue(self.harness.charm._stored.resource_installed)

self.harness.charm.exporter.install.assert_called_with(
int(EXPORTER_DEFAULT_PORT), "INFO", {}
int(EXPORTER_DEFAULT_PORT), "INFO", {"enable": False}
)

@mock.patch("charm.Exporter", return_value=mock.MagicMock())
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def test_render_config(self, mock_get_hw_tool_white_list):
self.template.render_config(
port="80",
level="info",
redfish_creds={},
redfish_creds={"enable": False},
)
mock_install.assert_called_with(
EXPORTER_CONFIG_PATH,
Expand All @@ -349,6 +349,7 @@ def test_render_config_redfish(self, mock_get_hw_tool_white_list):
port="80",
level="info",
redfish_creds={
"enable": True,
"host": "127.0.0.1",
"username": "default_user",
"password": "default_pwd",
Expand Down

0 comments on commit 634e108

Please sign in to comment.