2121 SyncAdapter ,
2222 SyncConfig ,
2323)
24+ from infrahub_sync .adapters .utils import build_mapping
2425
2526if 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
0 commit comments