Skip to content

Commit f058cdd

Browse files
authored
Merge branch 'main' into copilot/fix-80
2 parents 1c6efb4 + b9c76a3 commit f058cdd

File tree

11 files changed

+3637
-3443
lines changed

11 files changed

+3637
-3443
lines changed

.github/workflows/sync-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- name: Checkout source repository
17-
uses: actions/checkout@v4
17+
uses: actions/checkout@v5
1818
with:
1919
path: source-repo
2020

.github/workflows/trigger-push-stable.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
- name: "Install Poetry"
4646
uses: "snok/install-poetry@v1"
4747
with:
48-
version: 1.8.5
48+
version: 2.1.3
4949
virtualenvs-create: true
5050
virtualenvs-in-project: true
5151
installer-parallel: true
@@ -74,7 +74,7 @@ jobs:
7474
poetry version $VERSION # directly use the VERSION
7575
7676
- name: Update lock file
77-
run: poetry lock --no-update
77+
run: poetry lock
7878

7979
- name: commit pyproject.toml
8080
uses: github-actions-x/commit@v2.9

.github/workflows/workflow-linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
- "3.11"
6464
- "3.12"
6565
poetry-version:
66-
- "1.8.5"
66+
- "2.1.3"
6767
timeout-minutes: 5
6868
steps:
6969
- name: "Check out repository code"

.github/workflows/workflow-poetry-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
python-version:
3838
- "3.12"
3939
poetry-version:
40-
- "1.8.5"
40+
- "2.1.3"
4141
steps:
4242
- name: "Check out repository code"
4343
uses: "actions/checkout@v4"

.github/workflows/workflow-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
python-version:
4848
- "3.12"
4949
poetry-version:
50-
- "1.8.5"
50+
- "2.1.3"
5151
steps:
5252
- name: "Set up Python ${{ matrix.python-version }}"
5353
uses: "actions/setup-python@v5"

docs/package-lock.json

Lines changed: 2484 additions & 2645 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infrahub_sync/adapters/ipfabricsync.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SyncAdapter,
2222
SyncConfig,
2323
)
24+
from infrahub_sync.adapters.utils import build_mapping
2425

2526
if TYPE_CHECKING:
2627
from collections.abc import Mapping
@@ -45,41 +46,14 @@ def __init__(self, target: str, adapter: SyncAdapter, config: SyncConfig, *args,
4546
def _create_ipfabric_client(self, adapter: SyncAdapter) -> IPFClient:
4647
settings = adapter.settings or {}
4748

48-
base_url = settings.get("base_url") or None
49-
auth = settings.get("auth") or None
50-
timeout = settings.get("timeout", 10)
51-
verify_ssl = settings.get("verify_ssl", True)
49+
base_url = settings.get("base_url", None)
50+
auth = settings.get("auth", None)
5251

5352
if not base_url or not auth:
5453
msg = "Both url and auth must be specified!"
5554
raise ValueError(msg)
5655

57-
return IPFClient(base_url=base_url, auth=auth, timeout=timeout, verify=verify_ssl)
58-
59-
def build_mapping(self, reference, obj) -> str:
60-
# Get object class and model name from the store
61-
object_class, modelname = self.store._get_object_class_and_model(model=reference)
62-
63-
# Find the schema element matching the model name
64-
schema_element = next(
65-
(element for element in self.config.schema_mapping if element.name == modelname),
66-
None,
67-
)
68-
69-
# Collect all relevant field mappings for identifiers
70-
new_identifiers = []
71-
72-
# Convert schema_element.fields to a dictionary for fast lookup
73-
field_dict = {field.name: field.mapping for field in schema_element.fields}
74-
75-
# Loop through object_class._identifiers to find corresponding field mappings
76-
for identifier in object_class._identifiers:
77-
if identifier in field_dict:
78-
new_identifiers.append(field_dict[identifier])
79-
80-
# Construct the unique identifier, using a fallback if a key isn't found
81-
unique_id = "__".join(str(obj.get(key, "")) for key in new_identifiers)
82-
return unique_id
56+
return IPFClient(**settings)
8357

8458
def model_loader(self, model_name: str, model: IpfabricsyncModel) -> None:
8559
"""
@@ -143,7 +117,7 @@ def ipfabric_dict_to_diffsync(self, obj: dict, mapping: SchemaMappingModel, mode
143117
raise IndexError(msg)
144118
if not field_is_list and (node := obj[field.mapping]):
145119
matching_nodes = []
146-
node_id = self.build_mapping(reference=field.reference, obj=obj)
120+
node_id = build_mapping(adapter=self, reference=field.reference, obj=obj, field=field)
147121
matching_nodes = [item for item in nodes if str(item) == node_id]
148122
if len(matching_nodes) == 0:
149123
data[field.name] = None

infrahub_sync/adapters/slurpitsync.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
SyncAdapter,
1919
SyncConfig,
2020
)
21-
22-
from .utils import get_value
21+
from infrahub_sync.adapters.utils import build_mapping, get_value
2322

2423
if TYPE_CHECKING:
2524
from collections.abc import Mapping
@@ -220,32 +219,6 @@ def model_loader(self, model_name: str, model: SlurpitsyncModel) -> None:
220219
if self.skipped:
221220
print(f"{self.type}: skipped syncing {len(self.skipped)} models")
222221

223-
# Reuse mapping from another adapter
224-
def build_mapping(self, reference, obj):
225-
# Get object class and model name from the store
226-
object_class, modelname = self.store._get_object_class_and_model(model=reference)
227-
228-
# Find the schema element matching the model name
229-
schema_element = next(
230-
(element for element in self.config.schema_mapping if element.name == modelname),
231-
None,
232-
)
233-
234-
# Collect all relevant field mappings for identifiers
235-
new_identifiers = []
236-
237-
# Convert schema_element.fields to a dictionary for fast lookup
238-
field_dict = {field.name: field.mapping for field in schema_element.fields}
239-
240-
# Loop through object_class._identifiers to find corresponding field mappings
241-
for identifier in object_class._identifiers:
242-
if identifier in field_dict:
243-
new_identifiers.append(field_dict[identifier])
244-
245-
# Construct the unique identifier, using a fallback if a key isn't found
246-
unique_id = "__".join(str(obj.get(key, "")) for key in new_identifiers)
247-
return unique_id
248-
249222
def slurpit_obj_to_diffsync(
250223
self, obj: dict[str, Any], mapping: SchemaMappingModel, model: SlurpitsyncModel
251224
) -> dict:
@@ -276,7 +249,7 @@ def slurpit_obj_to_diffsync(
276249
if not field_is_list:
277250
if node := obj.get(field.mapping):
278251
matching_nodes = []
279-
node_id = self.build_mapping(reference=field.reference, obj=obj)
252+
node_id = build_mapping(adapter=self, reference=field.reference, obj=obj)
280253
matching_nodes = [item for item in nodes if str(item) == node_id]
281254
if len(matching_nodes) == 0:
282255
self.skipped.append(node)

infrahub_sync/adapters/utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
from __future__ import annotations
22

3-
from typing import Any
3+
from typing import TYPE_CHECKING, Any
4+
5+
if TYPE_CHECKING:
6+
from diffsync import Adapter
7+
8+
9+
def build_mapping(adapter: Adapter, reference: str, obj, field) -> str:
10+
"""This is used when references are encountered to attempt to resolve them for mapping."""
11+
# Get object class and model name from the store
12+
object_class, modelname = adapter.store._get_object_class_and_model(model=reference)
13+
14+
# Find the schema element matching the model name
15+
schema_element = next(
16+
(element for element in adapter.config.schema_mapping if element.name == modelname),
17+
None,
18+
)
19+
if not schema_element:
20+
msg = (
21+
f"Schema mapping for model '{reference}' not found when attempting to resolve "
22+
f"reference for {field.name}. The reference must be an existing schema mapping."
23+
)
24+
raise ValueError(msg)
25+
26+
# Collect all relevant field mappings for identifiers
27+
new_identifiers = []
28+
29+
# Convert schema_element.fields to a dictionary for fast lookup
30+
field_dict = {field.name: field.mapping for field in schema_element.fields}
31+
32+
# Loop through object_class._identifiers to find corresponding field mappings
33+
for identifier in object_class._identifiers:
34+
if identifier in field_dict:
35+
new_identifiers.append(field_dict[identifier])
36+
37+
# Construct the unique identifier, using a fallback if a key isn't found
38+
unique_id = "__".join(str(obj.get(key, "")) for key in new_identifiers)
39+
return unique_id
440

541

642
def get_value(obj: Any, name: str) -> Any | None:

0 commit comments

Comments
 (0)