Skip to content

Commit ebd6956

Browse files
authored
Merge pull request #17 from pawnar29/patch-1
Fix vrf data being ignored when importing to Netbox Changed the way scan processor is comparing the ip address to include vrf in case of duplicate ip
2 parents 368ea1f + dcdcca0 commit ebd6956

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

netbox_import.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def parse_tags(tags_string: str) -> List[Dict[str, str]]:
9595
def process_row(row: Dict[str, str], pbar: tqdm, netbox_instance: pynetbox.api) -> None:
9696
"""
9797
Process a single row from the CSV file and update/create IP addresses in Netbox.
98-
98+
9999
Args:
100100
row (Dict[str, str]): Dictionary representing a single row from the CSV file
101101
pbar (tqdm): Progress bar instance
@@ -114,12 +114,28 @@ def process_row(row: Dict[str, str], pbar: tqdm, netbox_instance: pynetbox.api)
114114
tags_list = parse_tags(row['tags'])
115115
logger.debug(f"Parsed tags for {address}: {tags_list}")
116116

117-
# Prepare tenant and VRF data
117+
# Prepare tenant data
118118
tenant_data = {'name': row['tenant']} if row['tenant'] != 'N/A' else None
119-
vrf_data = {'name': row['VRF']} if row['VRF'] != 'N/A' else None
120-
121-
# Get existing address
122-
existing_address = netbox_instance.ipam.ip_addresses.get(address=address)
119+
120+
# Look up VRF by name
121+
vrf_data = None
122+
if row['VRF'] != 'N/A':
123+
try:
124+
vrf = netbox_instance.ipam.vrfs.get(name=row['VRF'])
125+
if vrf:
126+
# Convert VRF object to a dictionary with just the ID
127+
vrf_data = {'id': vrf.id}
128+
logger.debug(f"Found VRF {row['VRF']} with ID {vrf.id}")
129+
else:
130+
logger.warning(f"VRF {row['VRF']} not found in Netbox")
131+
except Exception as e:
132+
logger.error(f"Error looking up VRF {row['VRF']}: {str(e)}")
133+
134+
# Get existing address - use the VRF ID
135+
existing_address = netbox_instance.ipam.ip_addresses.get(
136+
address=address,
137+
vrf_id=vrf_data['id'] if vrf_data else None
138+
)
123139

124140
if existing_address:
125141
_update_existing_address(

scan_processor.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ def get_latest_files(dir_path: str, num_files: int = 2) -> List[str]:
135135

136136
def read_csv(file_path: str) -> Dict[str, Dict[str, str]]:
137137
"""
138-
Read a CSV file and return a dictionary with addresses as keys.
138+
Read a CSV file and return a dictionary with address_vrf combinations as keys.
139139
140140
Args:
141141
file_path: The path to the CSV file
142142
143143
Returns:
144-
Dictionary with addresses as keys and corresponding row data as values
144+
Dictionary with address_vrf combinations as keys and corresponding row data as values
145145
146146
Raises:
147147
FileNotFoundError: If the file doesn't exist
@@ -155,9 +155,12 @@ def read_csv(file_path: str) -> Dict[str, Dict[str, str]]:
155155
with open(file_path, 'r', encoding='utf-8') as file:
156156
reader = csv.DictReader(file)
157157
for row in reader:
158+
# Create composite key using address and VRF
158159
address = row['address']
159-
data[address] = row
160-
logger.debug(f"Processed row for address: {address}")
160+
vrf = row['VRF']
161+
composite_key = f"{address}_{vrf}"
162+
data[composite_key] = row
163+
logger.debug(f"Processed row for address: {address} in VRF: {vrf}")
161164

162165
logger.info(f"Successfully read {len(data)} records from {file_path}")
163166
return data

0 commit comments

Comments
 (0)