Skip to content

Commit

Permalink
revamps SSoT cookie for contrib pattern
Browse files Browse the repository at this point in the history
- collapses both adapters into one file, asthe Nautobot adapter is
  significantly simpler with contrib collapse all models into one
  class per Nautobot content type -> users are of course free to change
  this but I found that for most SSoTs this is unnecessary complexity
- the differentation between base, SoR and Nautobot seems not necessary
  because SSoTs that sync both way simply don't exist
- add a "direction_of_sync" cookiecutter variable to define whether
  contrib classes are used or not depending on direction of sync
  • Loading branch information
Kircheneer committed Dec 16, 2024
1 parent e9b4d0c commit 87f48e8
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 147 deletions.
6 changes: 5 additions & 1 deletion nautobot-app-ssot/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"system_of_record": "System of Record",
"system_of_record_camel": "{{ cookiecutter.system_of_record.title().replace(' ', '').replace('_', '').replace('-', '') }}",
"system_of_record_slug": "{{ cookiecutter.system_of_record.lower().replace(' ', '_').replace('-', '_') }}",
"model_class_name": "None",
"direction_of_sync": [
"To Nautobot",
"From Nautobot"
],
"app_name": "nautobot_ssot_{{ cookiecutter.system_of_record.lower().replace(' ', '_').replace('-', '_') }}",
"verbose_name": "{{ cookiecutter.app_name.title().replace('_', ' ') }}",
"app_slug": "{{ cookiecutter.app_name.lower().replace(' ', '-').replace('_', '-') }}",
Expand All @@ -16,7 +21,6 @@
"max_nautobot_version": "2.9999",
"camel_name": "{{ cookiecutter.app_slug.title().replace(' ', '').replace('-', '') }}",
"project_short_description": "{{ cookiecutter.verbose_name }}",
"model_class_name": "None",
"open_source_license": [
"Apache-2.0",
"Not open source"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Diffsync components for {{ cookiecutter.app_name }}."""
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""{{ cookiecutter.verbose_name }} Adapter for {{ cookiecutter.system_of_record }} SSoT app."""
"""Diffsync adapters for {{ cookiecutter.app_name }}."""

from diffsync import Adapter

from {{ cookiecutter.app_name }}.diffsync.models.{{ cookiecutter.system_of_record_slug }} import {{ cookiecutter.system_of_record_camel }}Device
from {{ cookiecutter.app_name }}.diffsync.models import DiffsyncDevice


class {{ cookiecutter.system_of_record_camel }}Adapter(Adapter):
class {{ cookiecutter.system_of_record_camel }}RemoteAdapter(Adapter):
"""DiffSync adapter for {{ cookiecutter.system_of_record }}."""

device = {{ cookiecutter.system_of_record_camel }}Device
device = DiffsyncDevice

top_level = ["device"]

Expand All @@ -27,4 +27,18 @@ def __init__(self, *args, job=None, sync=None, client=None, **kwargs):

def load(self):
"""Load data from {{ cookiecutter.system_of_record }} into DiffSync models."""
raise NotImplementedError
raise NotImplementedError()


class {{ cookiecutter.system_of_record_camel }}NautobotAdapter(NautobotAdapter):
"""DiffSync adapter for Nautobot."""

device = NautobotDevice

top_level = ["device"]

{% if cookiecutter.direction_of_sync == "From Nautobot" %}
def load(self):
"""Load data from Nautobot into DiffSync models."""
raise NotImplementedError()
{% endif %}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Diffsync models for {{ cookiecutter.app_name }}."""
from typing import Optional, Annotated

{% if cookiecutter.direction_of_sync == "From Nautobot" %}
from diffsync import DiffSyncModel
{% endif %}
from nautobot.dcim.models import Device
{% if cookiecutter.direction_of_sync == "To Nautobot" %}
from nautobot_ssot.contrib import CustomFieldAnnotation, NautobotModel
{% endif %}

class DiffsyncDevice({{ "NautobotModel" if cookiecutter.direction_of_sync == "To Nautobot" else "DiffSyncModel" }}):
"""DiffSync model for {{ cookiecutter.system_of_record }} devices."""

{% if cookiecutter.direction_of_sync == "To Nautobot" %}_model = Device{% endif %}
_modelname = "device"
_identifiers = ("name",)
_attributes = (
"status__name",
"role__name",
"device_type__name",
"location__name",
"example_custom_field"
)

name: str
status__name: Optional[str] = None
role__name: Optional[str] = None
device_type__name: Optional[str] = None
location__name: Optional[str] = None
ip_address: Optional[str] = None
example_custom_field: Annotated[str, CustomFieldAnnotation(key="my_example_custom_field")]
{% if cookiecutter.direction_of_sync == "From Nautobot" %}
@classmethod
def create(cls, adapter, ids, attrs):
"""Create device in {{ cookiecutter.system_of_record }}."""
raise NotImplementedError("Device creation is not implemented.")

def update(self, attrs):
"""Update device in {{ cookiecutter.system_of_record }}."""
raise NotImplementedError("Device updates are not implemented.")

def delete(self):
"""Delete device in {{ cookiecutter.system_of_record }}."""
raise NotImplementedError("Device deletion is not implemented.")
{% endif %}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from nautobot.apps.jobs import BooleanVar, register_jobs
from nautobot_ssot.jobs.base import DataSource, DataTarget

from {{ cookiecutter.app_name }}.diffsync.adapters import {{ cookiecutter.system_of_record_slug }}, nautobot

from {{ cookiecutter.app_name }}.diffsync.adapters import {{ cookiecutter.system_of_record_camel }}RemoteAdapter, {{ cookiecutter.system_of_record_camel }}NautobotAdapter

name = "{{ cookiecutter.system_of_record }} SSoT" # pylint: disable=invalid-name

Expand Down

0 comments on commit 87f48e8

Please sign in to comment.