nft-update-addresses: add helper matcher to enum NftValueOperation

main
Felix Stupp 1 year ago
parent 11626ede34
commit 99e7fa2f62
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -496,6 +496,25 @@ class NftValueOperation(Enum):
def if_deleted(b: bool) -> NftValueOperation:
return NftValueOperation.DELETE if b else NftValueOperation.ADD
@property
def set_operation(self) -> str:
assert self.passes_values
return "destroy" if self == NftValueOperation.DELETE else "add"
@property
def passes_values(self) -> bool:
return self in {
NftValueOperation.ADD,
NftValueOperation.REPLACE,
NftValueOperation.DELETE,
}
@property
def flushes_values(self) -> bool:
return self in {
NftValueOperation.REPLACE,
}
@define(
frozen=True,
@ -511,10 +530,10 @@ class NftUpdate:
lines = []
# inet family is the only which supports shared IPv4 & IPv6 entries
obj_id = f"inet {table} {self.obj_name}"
if self.operation == NftValueOperation.REPLACE:
if self.operation.flushes_values:
lines.append(f"flush {self.obj_type} {obj_id}")
if len(self.values) > 0:
op_str = "destroy" if self.operation == NftValueOperation.DELETE else "add"
if self.operation.passes_values and len(self.values) > 0:
op_str = self.operation.set_operation
values_str = ", ".join(self.values)
lines.append(f"{op_str} element {obj_id} {{ {values_str} }}")
return "\n".join(lines)

Loading…
Cancel
Save