blueprints: keep more modular state
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
0cfffa28ad
commit
0e9762072a
|
@ -27,16 +27,23 @@ def get_attrs(obj: SerializerModel) -> dict[str, Any]:
|
||||||
continue
|
continue
|
||||||
if _field.read_only:
|
if _field.read_only:
|
||||||
data.pop(field_name, None)
|
data.pop(field_name, None)
|
||||||
if _field.default == data.get(field_name, None):
|
if _field.get_initial() == data.get(field_name, None):
|
||||||
data.pop(field_name, None)
|
data.pop(field_name, None)
|
||||||
if field_name.endswith("_set"):
|
if field_name.endswith("_set"):
|
||||||
data.pop(field_name, None)
|
data.pop(field_name, None)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlueprintEntryState:
|
||||||
|
"""State of a single instance"""
|
||||||
|
|
||||||
|
instance: Optional[Model] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class BlueprintEntry:
|
class BlueprintEntry:
|
||||||
"""Single entry of a bundle"""
|
"""Single entry of a blueprint"""
|
||||||
|
|
||||||
identifiers: dict[str, Any]
|
identifiers: dict[str, Any]
|
||||||
model: str
|
model: str
|
||||||
|
@ -45,11 +52,11 @@ class BlueprintEntry:
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
id: Optional[str] = None
|
id: Optional[str] = None
|
||||||
|
|
||||||
_instance: Optional[Model] = None
|
_state: BlueprintEntryState = field(default_factory=BlueprintEntryState)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_model(model: SerializerModel, *extra_identifier_names: str) -> "BlueprintEntry":
|
def from_model(model: SerializerModel, *extra_identifier_names: str) -> "BlueprintEntry":
|
||||||
"""Convert a SerializerModel instance to a Bundle Entry"""
|
"""Convert a SerializerModel instance to a blueprint Entry"""
|
||||||
identifiers = {
|
identifiers = {
|
||||||
"pk": model.pk,
|
"pk": model.pk,
|
||||||
}
|
}
|
||||||
|
@ -123,15 +130,15 @@ class KeyOf(YAMLTag):
|
||||||
|
|
||||||
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
|
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
|
||||||
for _entry in blueprint.entries:
|
for _entry in blueprint.entries:
|
||||||
if _entry.id == self.id_from and _entry._instance:
|
if _entry.id == self.id_from and _entry._state.instance:
|
||||||
# Special handling for PolicyBindingModels, as they'll have a different PK
|
# Special handling for PolicyBindingModels, as they'll have a different PK
|
||||||
# which is used when creating policy bindings
|
# which is used when creating policy bindings
|
||||||
if (
|
if (
|
||||||
isinstance(_entry._instance, PolicyBindingModel)
|
isinstance(_entry._state.instance, PolicyBindingModel)
|
||||||
and entry.model.lower() == "authentik_policies.policybinding"
|
and entry.model.lower() == "authentik_policies.policybinding"
|
||||||
):
|
):
|
||||||
return _entry._instance.pbm_uuid
|
return _entry._state.instance.pbm_uuid
|
||||||
return _entry._instance.pk
|
return _entry._state.instance.pk
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"KeyOf: failed to find entry with `id` of `{self.id_from}` and a model instance"
|
f"KeyOf: failed to find entry with `id` of `{self.id_from}` and a model instance"
|
||||||
)
|
)
|
||||||
|
|
|
@ -21,6 +21,7 @@ from yaml import load
|
||||||
from authentik.blueprints.v1.common import (
|
from authentik.blueprints.v1.common import (
|
||||||
Blueprint,
|
Blueprint,
|
||||||
BlueprintEntry,
|
BlueprintEntry,
|
||||||
|
BlueprintEntryState,
|
||||||
BlueprintLoader,
|
BlueprintLoader,
|
||||||
EntryInvalidError,
|
EntryInvalidError,
|
||||||
)
|
)
|
||||||
|
@ -220,7 +221,7 @@ class Importer:
|
||||||
model = serializer.save()
|
model = serializer.save()
|
||||||
if "pk" in entry.identifiers:
|
if "pk" in entry.identifiers:
|
||||||
self.__pk_map[entry.identifiers["pk"]] = model.pk
|
self.__pk_map[entry.identifiers["pk"]] = model.pk
|
||||||
entry._instance = model
|
entry._state = BlueprintEntryState(model)
|
||||||
self.logger.debug("updated model", model=model, pk=model.pk)
|
self.logger.debug("updated model", model=model, pk=model.pk)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
Reference in New Issue