generate: container: Remove Nones and log_error() on missing optional keys
Signed-off-by: Enno Tensing <tenno@suij.in>
This commit is contained in:
parent
4d86b49ed4
commit
6d9f63abb5
1 changed files with 26 additions and 18 deletions
|
@ -247,13 +247,13 @@ class Volume:
|
||||||
path: str
|
path: str
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||||
"""Create from JSON."""
|
"""Create from JSON."""
|
||||||
if val is None:
|
if val is None:
|
||||||
return None
|
return []
|
||||||
if not isinstance(val, dict):
|
if not isinstance(val, dict):
|
||||||
logger.log_warning("Volume key is present, but malformed.")
|
logger.log_warning("Volume key is present, but malformed.")
|
||||||
return None
|
return []
|
||||||
return [cls(key, value) for key, value in val.items()]
|
return [cls(key, value) for key, value in val.items()]
|
||||||
|
|
||||||
def is_host_volume(self) -> bool:
|
def is_host_volume(self) -> bool:
|
||||||
|
@ -302,13 +302,13 @@ class Secret:
|
||||||
options: str
|
options: str
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||||
"""Create from JSON."""
|
"""Create from JSON."""
|
||||||
if val is None:
|
if val is None:
|
||||||
return None
|
return []
|
||||||
if not isinstance(val, dict):
|
if not isinstance(val, dict):
|
||||||
logger.log_warning("Secret key is present, but malformed!")
|
logger.log_warning("Secret key is present, but malformed!")
|
||||||
return None
|
return []
|
||||||
secrets = []
|
secrets = []
|
||||||
for key in val:
|
for key in val:
|
||||||
if not isinstance(val[key], dict):
|
if not isinstance(val[key], dict):
|
||||||
|
@ -451,8 +451,11 @@ class Network:
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||||
"""Create from JSON."""
|
"""Create from JSON."""
|
||||||
if val is None or not isinstance(val, dict):
|
if val is None:
|
||||||
logger.log_error("Network configuration is missing or malformed!")
|
return cls("", [])
|
||||||
|
|
||||||
|
if not isinstance(val, dict):
|
||||||
|
logger.log_warning("Network configuration is malformed!")
|
||||||
return cls("", [])
|
return cls("", [])
|
||||||
mode = maybe(val, "mode")
|
mode = maybe(val, "mode")
|
||||||
options = maybe(val, "options")
|
options = maybe(val, "options")
|
||||||
|
@ -505,13 +508,15 @@ class Capability:
|
||||||
mode: str
|
mode: str
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||||
"""Create from JSON."""
|
"""Create from JSON."""
|
||||||
if val is None or not isinstance(val, dict):
|
if val is None:
|
||||||
|
return []
|
||||||
|
if not isinstance(val, dict):
|
||||||
logger.log_warning(
|
logger.log_warning(
|
||||||
"Capabilities key is either missing or malformed!"
|
"Capabilities key is malformed!"
|
||||||
)
|
)
|
||||||
return None
|
return []
|
||||||
add = [cls(value, "add") for value in val["add"]]
|
add = [cls(value, "add") for value in val["add"]]
|
||||||
drop = [cls(value, "drop") for value in val["drop"]]
|
drop = [cls(value, "drop") for value in val["drop"]]
|
||||||
return add + drop
|
return add + drop
|
||||||
|
@ -531,8 +536,11 @@ class Dns:
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||||
"""Create from JSON."""
|
"""Create from JSON."""
|
||||||
if val is None or not isinstance(val, dict):
|
if val is None:
|
||||||
logger.log_error("DNS Key is either missing or malformed!")
|
return cls([], "")
|
||||||
|
|
||||||
|
if not isinstance(val, dict):
|
||||||
|
logger.log_warning("DNS Key is malformed!")
|
||||||
return cls([], "")
|
return cls([], "")
|
||||||
search = maybe_or(val, "search", "")
|
search = maybe_or(val, "search", "")
|
||||||
servers = maybe(val, "servers")
|
servers = maybe(val, "servers")
|
||||||
|
@ -570,10 +578,10 @@ class Container:
|
||||||
network: Network
|
network: Network
|
||||||
dns: Dns
|
dns: Dns
|
||||||
ports: Ports
|
ports: Ports
|
||||||
env: Environment | None
|
env: Environment
|
||||||
secrets: list | None
|
secrets: list
|
||||||
volumes: list | None
|
volumes: list
|
||||||
capabilities: list | None
|
capabilities: list
|
||||||
accounting: Accounting
|
accounting: Accounting
|
||||||
|
|
||||||
def __init__(self, json: dict, logger: Log | None = None) -> None:
|
def __init__(self, json: dict, logger: Log | None = None) -> None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue