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

backport contrib changes to LTM #357

Merged
merged 1 commit into from
Feb 15, 2024
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
18 changes: 17 additions & 1 deletion docs/dev/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ It contains 3 steps:
- Nautobot Job

```python
# example_ssot_plugin/jobs.py
# example_ssot_app/jobs.py
from typing import Optional

from diffsync import DiffSync
Expand Down Expand Up @@ -132,6 +132,22 @@ class YourSSoTNautobotAdapter(NautobotAdapter):

The `load` function is already implemented on this adapter and will automatically and recursively traverse any children relationships for you, provided the models are [defined correctly](../user/modeling.md).

Developers are able to override the default loading of basic parameters to control how that parameter is loaded from Nautobot.

This only works with basic parameters belonging to the model and does not override more complex parameters (foreign keys, custom fields, custom relationships, etc.).

To override a parameter, simply add a method with the name `load_param_{param_key}` to your adapter class inheriting from `NautobotAdapter`:

```python
from nautobot_ssot.contrib import NautobotAdapter

class YourSSoTNautobotAdapter(NautobotAdapter):
...
def load_param_time_zone(self, parameter_name, database_object):
"""Custom loader for `time_zone` parameter."""
return str(getattr(database_object, parameter_name))
```

### Step 2.2 - Creating the Remote Adapter

Regardless of which direction you are synchronizing data in, you need to write the `load` method for the remote adapter yourself. You can find many examples of how this can be done in the `nautobot_ssot.integrations` module, which contains pre-existing integrations with remote systems.
Expand Down
28 changes: 26 additions & 2 deletions docs/user/modeling.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ The following table describes in brief the different types of model fields and h

| Type of field | Field name | Notes | Applies to |
|----------------------------------------------------|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Normal fields](#normal-fields) | Has to match ORM exactly | Make sure that the name matches the name in the ORM model. | Fields that are neither custom fields nor relations |
| [Normal fields](#normal-fields) | Has to match ORM exactly | Make sure that the name matches the name in the ORM model. | Fields that are neither custom fields nor relations |
| [Custom fields](#custom-fields) | Field name doesn't matter | Use `nautobot_ssot.contrib.CustomFieldAnnotation` | [Nautobot custom fields](https://docs.nautobot.com/projects/core/en/stable/user-guides/custom-fields/?h=custom+fields) |
| [*-to-one relationships](#-to-one-relationships) | Django lookup syntax | See [here](https://docs.djangoproject.com/en/3.2/topics/db/queries/#lookups-that-span-relationships) - your model fields need to use this syntax | `django.db.models.OneToOneField`, `django.db.models.ForeignKey`, `django.contrib.contenttypes.fields.GenericForeignKey` |
| [*-to-many relationships](#-to-many-relationships) | Has to match ORM exactly | In case of a generic foreign key see [here](#special-case-generic-foreign-key) | `django.db.models.ManyToManyField`, `django.contrib.contenttypes.fields.GenericRelation`, `django.db.models.ForeignKey` [backwards](https://docs.djangoproject.com/en/3.2/topics/db/queries/#backwards-related-objects) |
| Custom Relationships | n/a | Not yet supported | https://docs.nautobot.com/projects/core/en/stable/models/extras/relationship/ |
| Custom Relationships | Field name doesn't matter | Use `nautobot_ssot.contrib.CustomRelationshipAnnotation` | https://docs.nautobot.com/projects/core/en/stable/models/extras/relationship/ |


## Normal Fields
Expand Down Expand Up @@ -156,3 +156,27 @@ Through us defining the model, Nautobot will now be able to dynamically load IP

!!! note
Although `Interface.ip_addresses` is a generic relation, there is only one content type (i.e. `ipam.ipaddress`) that may be related through this relation, therefore we don't have to specific this in any way.


## Filtering Objects Loaded From Nautobot


If you'd like to filter the objects loaded from the Nautobot, you can do so creating a `get_queryset` function in your model class and return your own queryset. Here is an example where the adapter would only load Tenant objects whose name starts with an "s".

```python
from nautobot.tenancy.models import Tenant
from nautobot_ssot.contrib import NautobotModel

class TenantModel(NautobotModel):
_model = Tenant
_modelname = "tenant"
_identifiers = ("name",)
_attributes = ("description",)

name: str
description: str

@classmethod
def get_queryset(cls):
return Tenant.objects.filter(name__startswith="s")
```
Loading
Loading