|
| 1 | +"""Inspect interface for PAN-OS (Palo Alto) appliances.""" |
| 2 | + |
| 3 | +from typing import ClassVar |
| 4 | + |
| 5 | +import requests |
| 6 | +from ironic.common import exception |
| 7 | +from ironic.common import states |
| 8 | +from ironic.drivers import base |
| 9 | +from oslo_log import log |
| 10 | + |
| 11 | +from ironic_understack.utils import panos_api |
| 12 | + |
| 13 | +LOG = log.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class PanosInspect(base.InspectInterface): |
| 17 | + """Inspect a PAN-OS appliance over its XML API. |
| 18 | +
|
| 19 | + No ramdisk is involved; everything goes over the management address in |
| 20 | + ``driver_info['management_ip']``, which enroll-fw sets. |
| 21 | + """ |
| 22 | + |
| 23 | + # A firewall has no memory_mb/local_gb/cpu_arch to report. |
| 24 | + ESSENTIAL_PROPERTIES: ClassVar[set] = set() |
| 25 | + |
| 26 | + def get_properties(self): |
| 27 | + return { |
| 28 | + "management_ip": "Address of the appliance's management interface, " |
| 29 | + "used for the XML API. Required.", |
| 30 | + } |
| 31 | + |
| 32 | + def validate(self, task): |
| 33 | + if not task.node.driver_info.get("management_ip"): |
| 34 | + raise exception.MissingParameterValue( |
| 35 | + f"Node {task.node.uuid} is missing driver_info management_ip" |
| 36 | + ) |
| 37 | + |
| 38 | + def inspect_hardware(self, task): |
| 39 | + node = task.node |
| 40 | + address = node.driver_info["management_ip"] |
| 41 | + client = panos_api.PanosClient(address) |
| 42 | + try: |
| 43 | + password = client.login() |
| 44 | + LOG.info( |
| 45 | + "[node:%s] Logged in to %s with the %s password", |
| 46 | + node.uuid, |
| 47 | + address, |
| 48 | + password, |
| 49 | + ) |
| 50 | + system = client.system_info() |
| 51 | + except (panos_api.PanosApiError, requests.RequestException) as e: |
| 52 | + raise exception.HardwareInspectionFailure( |
| 53 | + error=f"PAN-OS {address}: {e}" |
| 54 | + ) from e |
| 55 | + |
| 56 | + _apply_system_info(node, system) |
| 57 | + |
| 58 | + # TODO: discover data plane connectivity and create ports from it: |
| 59 | + # - show interface all, for the data plane interfaces and their MACs |
| 60 | + # - LLDP neighbors. Interfaces without config don't transmit LLDP, so |
| 61 | + # this needs to enable LLDP and a transmit-receive profile on them, |
| 62 | + # commit, wait for neighbors, collect them, then restore the |
| 63 | + # original config and commit again. |
| 64 | + # - an Ironic port per neighbor named "<node name>:<interface>", with |
| 65 | + # local_link_connection from the neighbor and the remote interface |
| 66 | + # as extra["bios_name"] |
| 67 | + # TODO: show high-availability all, recording the HA peer serial in |
| 68 | + # extra["mate_serial"] (which enroll-fw also sets) when HA is enabled. |
| 69 | + |
| 70 | + node.save() |
| 71 | + return states.MANAGEABLE |
| 72 | + |
| 73 | + |
| 74 | +def _apply_system_info(node, system: dict[str, str]) -> None: |
| 75 | + """Record what ``show system info`` reported on the node. |
| 76 | +
|
| 77 | + model and serial are also set by enroll-fw from Nautobot, so those are only |
| 78 | + filled in when missing; a disagreement is logged rather than overwritten. |
| 79 | + """ |
| 80 | + properties = dict(node.properties) |
| 81 | + extra = dict(node.extra) |
| 82 | + for target, key, field in ( |
| 83 | + (properties, "model", "model"), |
| 84 | + (extra, "serial", "serial"), |
| 85 | + ): |
| 86 | + found = system.get(field) |
| 87 | + if not found: |
| 88 | + continue |
| 89 | + if not target.get(key): |
| 90 | + target[key] = found |
| 91 | + elif target[key] != found: |
| 92 | + LOG.warning( |
| 93 | + "[node:%s] Appliance reports %s %r but the node has %r", |
| 94 | + node.uuid, |
| 95 | + key, |
| 96 | + found, |
| 97 | + target[key], |
| 98 | + ) |
| 99 | + if system.get("sw-version"): |
| 100 | + properties["firmware_version"] = system["sw-version"] |
| 101 | + node.properties = properties |
| 102 | + node.extra = extra |
0 commit comments