|
10 | 10 | get_device_oob_ip, |
11 | 11 | get_nb_device_query_list_ironic, |
12 | 12 | ) |
| 13 | +from osism.tasks.netbox import _matches_netbox_filter |
13 | 14 | from osism.tasks.conductor.utils import ( |
14 | 15 | deep_compare, |
15 | 16 | deep_decrypt, |
@@ -400,7 +401,7 @@ def sync_ironic(request_id, get_ironic_parameters, node_name=None, force_update= |
400 | 401 |
|
401 | 402 |
|
402 | 403 | def sync_netbox_from_ironic(request_id, node_name=None, netbox_filter=None): |
403 | | - """Sync Ironic node states to NetBox (including secondaries) |
| 404 | + """Sync Ironic node states to NetBox |
404 | 405 |
|
405 | 406 | This function synchronizes the state of Ironic baremetal nodes to NetBox. |
406 | 407 | It updates three custom fields in NetBox: |
@@ -449,44 +450,55 @@ def sync_netbox_from_ironic(request_id, node_name=None, netbox_filter=None): |
449 | 450 | return |
450 | 451 |
|
451 | 452 | # Check secondary NetBox instances connectivity |
| 453 | + # Apply filter BEFORE connectivity checks to avoid unnecessary network calls |
452 | 454 | reachable_secondaries = [] |
453 | 455 | if osism_utils.secondary_nb_list: |
454 | | - osism_utils.push_task_output( |
455 | | - request_id, "Checking secondary NetBox instances connectivity...\n" |
456 | | - ) |
457 | | - for nb in osism_utils.secondary_nb_list: |
458 | | - try: |
459 | | - nb.status() |
460 | | - reachable_secondaries.append(nb) |
461 | | - |
462 | | - # Build info message |
463 | | - name = getattr(nb, "netbox_name", None) |
464 | | - site = getattr(nb, "netbox_site", None) |
465 | | - info_parts = [] |
466 | | - if name: |
467 | | - info_parts.append(f"Name: {name}") |
468 | | - if site: |
469 | | - info_parts.append(f"Site: {site}") |
470 | | - info = f" ({', '.join(info_parts)})" if info_parts else "" |
| 456 | + # First filter the secondary instances based on netbox_filter |
| 457 | + filtered_secondaries = [ |
| 458 | + nb |
| 459 | + for nb in osism_utils.secondary_nb_list |
| 460 | + if _matches_netbox_filter(nb, netbox_filter, is_primary=False) |
| 461 | + ] |
471 | 462 |
|
472 | | - osism_utils.push_task_output( |
473 | | - request_id, f"Secondary NetBox is reachable: {nb.base_url}{info}\n" |
474 | | - ) |
475 | | - except Exception as e: |
476 | | - # Build warning message |
477 | | - name = getattr(nb, "netbox_name", None) |
478 | | - site = getattr(nb, "netbox_site", None) |
479 | | - info_parts = [] |
480 | | - if name: |
481 | | - info_parts.append(f"Name: {name}") |
482 | | - if site: |
483 | | - info_parts.append(f"Site: {site}") |
484 | | - info = f" ({', '.join(info_parts)})" if info_parts else "" |
| 463 | + # Only check connectivity for filtered instances |
| 464 | + if filtered_secondaries: |
| 465 | + osism_utils.push_task_output( |
| 466 | + request_id, "Checking secondary NetBox instances connectivity...\n" |
| 467 | + ) |
| 468 | + for nb in filtered_secondaries: |
| 469 | + try: |
| 470 | + nb.status() |
| 471 | + reachable_secondaries.append(nb) |
| 472 | + |
| 473 | + # Build info message |
| 474 | + name = getattr(nb, "netbox_name", None) |
| 475 | + site = getattr(nb, "netbox_site", None) |
| 476 | + info_parts = [] |
| 477 | + if name: |
| 478 | + info_parts.append(f"Name: {name}") |
| 479 | + if site: |
| 480 | + info_parts.append(f"Site: {site}") |
| 481 | + info = f" ({', '.join(info_parts)})" if info_parts else "" |
485 | 482 |
|
486 | | - osism_utils.push_task_output( |
487 | | - request_id, |
488 | | - f"WARNING: Secondary NetBox not reachable: {nb.base_url}{info}: {e}\n", |
489 | | - ) |
| 483 | + osism_utils.push_task_output( |
| 484 | + request_id, |
| 485 | + f"Secondary NetBox is reachable: {nb.base_url}{info}\n", |
| 486 | + ) |
| 487 | + except Exception as e: |
| 488 | + # Build warning message |
| 489 | + name = getattr(nb, "netbox_name", None) |
| 490 | + site = getattr(nb, "netbox_site", None) |
| 491 | + info_parts = [] |
| 492 | + if name: |
| 493 | + info_parts.append(f"Name: {name}") |
| 494 | + if site: |
| 495 | + info_parts.append(f"Site: {site}") |
| 496 | + info = f" ({', '.join(info_parts)})" if info_parts else "" |
| 497 | + |
| 498 | + osism_utils.push_task_output( |
| 499 | + request_id, |
| 500 | + f"WARNING: Secondary NetBox not reachable: {nb.base_url}{info}: {e}\n", |
| 501 | + ) |
490 | 502 |
|
491 | 503 | # Check Ironic API connectivity |
492 | 504 | try: |
@@ -518,11 +530,15 @@ def sync_netbox_from_ironic(request_id, node_name=None, netbox_filter=None): |
518 | 530 | osism_utils.finish_task_output(request_id, rc=1) |
519 | 531 | return |
520 | 532 |
|
521 | | - # Sync each node to NetBox (including secondaries) |
| 533 | + # Determine if we have secondaries for messaging |
| 534 | + has_secondaries = len(reachable_secondaries) > 0 |
| 535 | + secondary_msg = " (including secondaries)" if has_secondaries else "" |
| 536 | + |
| 537 | + # Sync each node to NetBox |
522 | 538 | for node in nodes: |
523 | 539 | osism_utils.push_task_output( |
524 | 540 | request_id, |
525 | | - f"Syncing state of {node['name']} to NetBox (including secondaries)\n", |
| 541 | + f"Syncing state of {node['name']} to NetBox{secondary_msg}\n", |
526 | 542 | ) |
527 | 543 |
|
528 | 544 | # Update all three states (each function handles primary + secondary NetBox instances) |
|
0 commit comments