Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for SSoT #43

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Nautobot Adapter for {{ cookiecutter.system_of_record }} SSoT plugin."""

from diffsync import DiffSync
from {{ cookiecutter.plugin_name }}.diffsync.models.nautobot import NautobotDevice


class NautobotAdapter(DiffSync):
"""DiffSync adapter for Nautobot."""

top_level = []
device = NautobotDevice

top_level = ["device"]

def __init__(self, *args, job=None, sync=None, **kwargs):
"""Initialize Nautobot.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""{{ cookiecutter.verbose_name }} Adapter for {{ cookiecutter.system_of_record }} SSoT plugin."""

from diffsync import DiffSync
from {{ cookiecutter.plugin_name }}.diffsync.models.{{ cookiecutter.system_of_record_slug }} import {{ cookiecutter.system_of_record_camel }}Device


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

top_level = []
device = {{ cookiecutter.system_of_record_camel }}Device

top_level = ["device"]

def __init__(self, *args, job=None, sync=None, client=None, **kwargs):
"""Initialize {{ cookiecutter.system_of_record }}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class Device(DiffSyncModel):
role: Optional[str]
model: Optional[str]
site: Optional[str]
ip_address = Optional[str]
ip_address: Optional[str]

uuid = Optional[UUID]
uuid: Optional[UUID]
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@
from diffsync import DiffSyncModel

from nautobot.dcim.models import Device as NewDevice
from nautobot.dcim.models import Site, DeviceRole
from nautobot.dcim.models import Site, DeviceRole, DeviceType
from nautobot.extras.models import Status
from {{ cookiecutter.plugin_name }}.diffsync.models.base import Device


class NautobotDevice(DiffSyncModel):
class NautobotDevice(Device):
"""Nautobot implementation of {{ cookiecutter.system_of_record }} Device model."""

@classmethod
def create(cls, diffsync, ids, attrs):
"""Create Device in Nautobot from NautobotDevice object."""
new_device = NewDevice(
name=ids["name"],
status=Status.objects.get_or_create(name=attrs["status"]),
role=DeviceRole.objects.get_or_create(name=attrs["role"]),
site=Site.objects.get_or_create(name=attrs["site"]),
device_type=DeviceType.objects.get_or_create(model=attrs["model"])[0],
status=Status.objects.get_or_create(name=attrs["status"])[0],
device_role=DeviceRole.objects.get_or_create(name=attrs["role"])[0],
site=Site.objects.get_or_create(name=attrs["site"])[0],
)
new_device.validated_save()
return super().create(diffsync=diffsync, ids=ids, attrs=attrs)

def update(self, attrs):
"""Update Device in Nautobot from NautobotDevice object."""
device = Device.objects.get(id=attrs["uuid"])
device = NewDevice.objects.get(id=self.uuid)
if "status" in attrs:
device.status = Status.objects.get_or_create(name=attrs["status"])
device.status = Status.objects.get_or_create(name=attrs["status"])[0]
if "role" in attrs:
device.role = DeviceRole.objects.get_or_create(name=attrs["role"])
device.device_role = DeviceRole.objects.get_or_create(name=attrs["role"])[0]
if "site" in attrs:
device.site = Site.objects.get_or_create(name=attrs["site"])
device.site = Site.objects.get_or_create(name=attrs["site"])[0]
device.validated_save()
return super().update(attrs)

def delete(self):
"""Delete Device in Nautobot from NautobotDevice object."""
dev = Device.objects.get(id=self.uuid)
dev = NewDevice.objects.get(id=self.uuid)
super().delete()
dev.delete()
return self
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class {{ cookiecutter.system_of_record_camel }}Device(Device):

@classmethod
def create(cls, diffsync, ids, attrs):
"""Create Device in {{ cookiecutter.system_of_record }} from {{ cookiecutter.camel_name }}Device object."""
"""Create Device in {{ cookiecutter.system_of_record }} from {{ cookiecutter.system_of_record_camel }}Device object."""
return super().create(diffsync=diffsync, ids=ids, attrs=attrs)

def update(self, attrs):
"""Update Device in {{ cookiecutter.system_of_record }} from {{ cookiecutter.camel_name }}Device object."""
"""Update Device in {{ cookiecutter.system_of_record }} from {{ cookiecutter.system_of_record_camel }}Device object."""
return super().update(attrs)

def delete(self):
"""Delete Device in {{ cookiecutter.system_of_record }} from {{ cookiecutter.camel_name }}Device object."""
"""Delete Device in {{ cookiecutter.system_of_record }} from {{ cookiecutter.system_of_record_camel }}Device object."""
return self
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def load_json(path):
return json.loads(file.read())


SITE_FIXTURE = []
DEVICE_FIXTURE = load_json("./{{ cookiecutter.plugin_name }}/tests/fixtures/fixtures/get_devices.json")


class Test{{ cookiecutter.system_of_record_camel }}AdapterTestCase(TransactionTestCase):
Expand All @@ -28,7 +28,7 @@ class Test{{ cookiecutter.system_of_record_camel }}AdapterTestCase(TransactionTe
def setUp(self):
"""Initialize test case."""
self.{{ cookiecutter.system_of_record_slug }}_client = MagicMock()
self.{{ cookiecutter.system_of_record_slug }}_client.get_sites.return_value = SITE_FIXTURE
self.{{ cookiecutter.system_of_record_slug }}_client.get_devices.return_value = DEVICE_FIXTURE

self.job = {{ cookiecutter.system_of_record_camel }}DataSource()
self.job.job_result = JobResult.objects.create(
Expand All @@ -40,6 +40,6 @@ def test_data_loading(self):
"""Test {{ cookiecutter.verbose_name }} load() function."""
# self.{{ cookiecutter.system_of_record_slug }}.load()
# self.assertEqual(
# {site["name"] for site in SITE_FIXTURE},
# {site.get_unique_id() for site in self.{{ cookiecutter.system_of_record_slug }}.get_all("site")},
# {dev["name"] for dev in DEVICE_FIXTURE},
# {dev.get_unique_id() for dev in self.{{ cookiecutter.system_of_record_slug }}.get_all("device")},
# )
17 changes: 15 additions & 2 deletions nautobot-app/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

if "{{ cookiecutter.model_class_name }}" == "None":
files_to_remove = [
"api/__init__.py",
"api/nested_serializers.py",
"api/serializers.py",
"api/urls.py",
"api/views.py",
"filters.py",
"forms.py",
"migrations/__init__.py",
"models.py",
"navigation.py",
"tables.py",
Expand All @@ -45,9 +47,20 @@
]
for file in files_to_remove:
(_ADDONS_PATH / file).unlink()
folders_to_remove = [
"api",
"migrations",
"templates/{{ cookiecutter.plugin_name }}",
"templates",
"views",
]
for folder in folders_to_remove:
(_ADDONS_PATH / folder).rmdir()

# Persist the baked cookie parameters in-repo for future usage as a replay file or for the drift management.
cookie = {{ cookiecutter }}
(_PROJECT_PATH / ".cookiecutter.json").write_text(json.dumps({"cookiecutter": cookie}, indent=4) + "\n", encoding="utf-8")
cookie = {{cookiecutter}}
(_PROJECT_PATH / ".cookiecutter.json").write_text(
json.dumps({"cookiecutter": cookie}, indent=4) + "\n", encoding="utf-8"
)

print(_CONGRATS)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ services:
- "-c" # this is to evaluate the $NAUTOBOT_LOG_LEVEL from the env
- "nautobot-server celery worker -l $$NAUTOBOT_LOG_LEVEL --events" ## $$ because of docker-compose
depends_on:
- "nautobot"
nautobot:
condition: "service_healthy"
healthcheck:
interval: "30s"
timeout: "10s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ services:
- "./nautobot_config.py:/opt/nautobot/nautobot_config.py"
- "../:/source"
healthcheck:
interval: "30s"
timeout: "10s"
start_period: "60s"
retries: 3
test: ["CMD", "true"] # Due to layering, disable: true won't work. Instead, change the test
docs:
entrypoint: "mkdocs serve -v -a 0.0.0.0:8080"
Expand Down