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
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return None
|
||||
return []
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Volume key is present, but malformed.")
|
||||
return None
|
||||
return []
|
||||
return [cls(key, value) for key, value in val.items()]
|
||||
|
||||
def is_host_volume(self) -> bool:
|
||||
|
@ -302,13 +302,13 @@ class Secret:
|
|||
options: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return None
|
||||
return []
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Secret key is present, but malformed!")
|
||||
return None
|
||||
return []
|
||||
secrets = []
|
||||
for key in val:
|
||||
if not isinstance(val[key], dict):
|
||||
|
@ -451,8 +451,11 @@ class Network:
|
|||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
if val is None or not isinstance(val, dict):
|
||||
logger.log_error("Network configuration is missing or malformed!")
|
||||
if val is None:
|
||||
return cls("", [])
|
||||
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Network configuration is malformed!")
|
||||
return cls("", [])
|
||||
mode = maybe(val, "mode")
|
||||
options = maybe(val, "options")
|
||||
|
@ -505,13 +508,15 @@ class Capability:
|
|||
mode: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
"""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(
|
||||
"Capabilities key is either missing or malformed!"
|
||||
"Capabilities key is malformed!"
|
||||
)
|
||||
return None
|
||||
return []
|
||||
add = [cls(value, "add") for value in val["add"]]
|
||||
drop = [cls(value, "drop") for value in val["drop"]]
|
||||
return add + drop
|
||||
|
@ -531,8 +536,11 @@ class Dns:
|
|||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
if val is None or not isinstance(val, dict):
|
||||
logger.log_error("DNS Key is either missing or malformed!")
|
||||
if val is None:
|
||||
return cls([], "")
|
||||
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("DNS Key is malformed!")
|
||||
return cls([], "")
|
||||
search = maybe_or(val, "search", "")
|
||||
servers = maybe(val, "servers")
|
||||
|
@ -570,10 +578,10 @@ class Container:
|
|||
network: Network
|
||||
dns: Dns
|
||||
ports: Ports
|
||||
env: Environment | None
|
||||
secrets: list | None
|
||||
volumes: list | None
|
||||
capabilities: list | None
|
||||
env: Environment
|
||||
secrets: list
|
||||
volumes: list
|
||||
capabilities: list
|
||||
accounting: Accounting
|
||||
|
||||
def __init__(self, json: dict, logger: Log | None = None) -> None:
|
||||
|
|
Loading…
Add table
Reference in a new issue