modified: netbox_services/engine/__init__.py
modified: netbox_services/engine/allocators.py modified: netbox_services/engine/context.py new file: netbox_services/engine/mappers.py modified: netbox_services/engine/resolver.py new file: netbox_services/engine/templating.py modified: netbox_services/forms.py modified: netbox_services/models.py modified: netbox_services/tests/test_allocators.py new file: netbox_services/tests/test_mappers.py modified: netbox_services/tests/test_resolver.py new file: netbox_services/tests/test_templating.py Simplify layout to template and service instances
This commit is contained in:
1 parent
4ff9eac274
commit
b1c9c7a40b
12 files changed
+1133
-33
No files matched your search
@@ -1,6 +1,68 @@
|
||||
"""Allocators create or acquire objects that don't exist yet (e.g. IP addresses)."""
|
||||
"""Allocators create or acquire objects that don't exist yet.
|
||||
|
||||
Strategies:
|
||||
- ``create`` — instantiate a new model instance from attrs (unsaved)
|
||||
- ``first_free`` — allocate the first available IP from a prefix
|
||||
- ``specific`` — use an existing object by address or context path
|
||||
"""
|
||||
|
||||
from django.db.models import Model
|
||||
|
||||
from .registry import register_allocator, get_selector
|
||||
from .templating import render_attrs
|
||||
|
||||
|
||||
@register_allocator("create")
|
||||
class CreateAllocator:
|
||||
"""Instantiate a new model instance from a dict of attrs.
|
||||
|
||||
The instance is returned **unsaved** — the resolver saves it after
|
||||
applying the parent link (if any).
|
||||
|
||||
Attrs values may be:
|
||||
- Plain literals (int, bool, list) — passed through
|
||||
- Jinja2 strings with ``{{ }}`` — rendered with ``service``,
|
||||
``index``, ``p1`` (parent), ``p2`` (grandparent), etc.
|
||||
- FK fields: the rendered value is treated as a **PK** and
|
||||
resolved to the related model instance via ``.objects.get(pk=...)``
|
||||
|
||||
Spec shape::
|
||||
|
||||
{"strategy": "create", "attrs": {"name": "rtr-{{ service.tenant.slug }}-{{ index + 1 }}", "site": "{{ ..site.pk }}", "status": "active"}}
|
||||
"""
|
||||
|
||||
def __init__(self, spec):
|
||||
self.spec = spec
|
||||
self.attrs = spec.get("attrs", {})
|
||||
|
||||
def allocate(self, context, model=None, index=0, ancestors=None):
|
||||
if model is None:
|
||||
raise ValueError("CreateAllocator requires a model class.")
|
||||
|
||||
rendered = render_attrs(
|
||||
self.attrs, context.service, ancestors or [], index,
|
||||
)
|
||||
|
||||
kwargs = {}
|
||||
for key, value in rendered.items():
|
||||
try:
|
||||
field = model._meta.get_field(key)
|
||||
except Exception:
|
||||
kwargs[key] = value
|
||||
continue
|
||||
|
||||
if (
|
||||
field.is_relation
|
||||
and field.many_to_one
|
||||
and not isinstance(value, Model)
|
||||
and value is not None
|
||||
):
|
||||
related = field.remote_field.model
|
||||
kwargs[key] = related.objects.get(pk=int(value))
|
||||
else:
|
||||
kwargs[key] = value
|
||||
|
||||
return model(**kwargs)
|
||||
|
||||
|
||||
@register_allocator("first_free")
|
||||
@@ -23,7 +85,7 @@ class FirstFreeIPAddressAllocator:
|
||||
qs = selector.resolve(context, Prefix)
|
||||
return qs.first()
|
||||
|
||||
def allocate(self, context):
|
||||
def allocate(self, context, model=None, index=0, ancestors=None):
|
||||
from ipam.models import IPAddress
|
||||
from ipam.choices import IPAddressStatusChoices
|
||||
|
||||
@@ -62,7 +124,7 @@ class SpecificAllocator:
|
||||
self.path = spec.get("path")
|
||||
self.address = spec.get("address")
|
||||
|
||||
def allocate(self, context):
|
||||
def allocate(self, context, model=None, index=0, ancestors=None):
|
||||
if self.address:
|
||||
from ipam.models import IPAddress
|
||||
from ipam.choices import IPAddressStatusChoices
|
||||
|
||||
Reference in new issue
Block a user