Get the StorPoolConfItems data from the config automatically.
Let us not repeat ourselves by having to write the same config
option names in the same order over and over again.
Change-Id: I1de574410a6ea5ad4abad0c3ca3ebee687ed59fc
diff --git a/src/charm.py b/src/charm.py
index 2e935f8..04ebb7a 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -32,6 +32,13 @@
sp_api_http_port: str
sp_auth_token: str
+ @classmethod
+ def from_config(cls, data) -> "StorPoolConfItems":
+ args = {
+ field.name: str(data[field.name.replace("_", "-")]) for field in dataclasses.fields(cls)
+ }
+ return cls(**args)
+
def to_ini_key_value_pairs(self) -> str:
return "".join(
f"{name.upper()}={value}\n" for name, value in dataclasses.asdict(self).items()
@@ -89,11 +96,7 @@
return
- self.create_storpool_conf(
- StorPoolConfItems(
- config["sp-api-http-host"], str(config["sp-api-http-port"]), config["sp-auth-token"]
- )
- )
+ self.create_storpool_conf(StorPoolConfItems.from_config(config))
super().on_config(event)
@@ -128,11 +131,7 @@
]
)
- self.create_storpool_conf(
- StorPoolConfItems(
- config["sp-api-http-host"], str(config["sp-api-http-port"]), config["sp-auth-token"]
- )
- )
+ self.create_storpool_conf(StorPoolConfItems.from_config(config))
self._stored.is_started = True