77
88from understack_workflows import firewall
99from understack_workflows import helpers
10+ from understack_workflows import ironic_node
1011from understack_workflows import netdev_reconciler
1112from understack_workflows .ironic .client import IronicClient
1213
@@ -98,6 +99,8 @@ def enroll_fw(
9899 driver = (driver or "" ).strip ()
99100 if not driver :
100101 raise ValueError ("enroll-fw requires a non-empty --driver" )
102+ if not (physical_network or "" ).strip ():
103+ raise ValueError ("enroll-fw requires --physical-network" )
101104 resource_class = _require_specific_resource_class (resource_class )
102105 management_switch , management_switch_port = _require_management_location (
103106 management_switch , management_switch_port
@@ -135,6 +138,19 @@ def enroll_fw(
135138 )
136139 return node .uuid
137140
141+ if not ports :
142+ return _enroll_by_inspection (
143+ client = client ,
144+ name = name ,
145+ physical_network = physical_network ,
146+ resource_class = resource_class ,
147+ driver = driver ,
148+ external_cmdb_id = external_cmdb_id ,
149+ driver_info = driver_info ,
150+ extra = extra ,
151+ properties = properties ,
152+ )
153+
138154 # New or re-enrollable node: the engine writes the metadata inside the
139155 # enrollment lifecycle (at create, or patched before the node becomes
140156 # available), so the node is never allocatable in an under-described state.
@@ -160,6 +176,85 @@ def _get_node_or_none(client: IronicClient, name: str):
160176 return None
161177
162178
179+ def _enroll_by_inspection (
180+ * ,
181+ client : IronicClient ,
182+ name : str ,
183+ physical_network : str ,
184+ resource_class : str ,
185+ driver : str ,
186+ external_cmdb_id : int | str | None ,
187+ driver_info : dict ,
188+ extra : dict ,
189+ properties : dict ,
190+ ) -> str :
191+ """Enroll a firewall without ports and let inspection find and label them.
192+
193+ Provided only once it has an outside and an inside port, which get the
194+ given physical_network.
195+ """
196+ if external_cmdb_id not in (None , "" ):
197+ extra = {** extra , "external_cmdb_id" : external_cmdb_id }
198+ node , created = netdev_reconciler .find_or_create_netdev_node (
199+ client = client ,
200+ name = name ,
201+ resource_class = resource_class ,
202+ driver = driver ,
203+ driver_info = driver_info ,
204+ extra = extra ,
205+ properties = properties ,
206+ )
207+ if node .provision_state != "manageable" :
208+ ironic_node .transition (node , target_state = "manage" , expected_state = "manageable" )
209+ if not created :
210+ netdev_reconciler .update_node_metadata (
211+ client = client ,
212+ node = node ,
213+ resource_class = resource_class ,
214+ driver_info = driver_info ,
215+ extra = extra ,
216+ properties = properties ,
217+ )
218+
219+ logger .info ("[node:%s] Inspecting to discover and label the ports" , node .uuid )
220+ ironic_node .transition (node , target_state = "inspect" , expected_state = "manageable" )
221+ role_ports = [
222+ port
223+ for port in client .list_ports (node .uuid )
224+ if getattr (port , "category" , None ) in netdev_reconciler .PORT_ROLES
225+ ]
226+ if not {port .category for port in role_ports }.issuperset (
227+ netdev_reconciler .PORT_ROLES
228+ ):
229+ raise RuntimeError (
230+ f"[node:{ node .uuid } ] Inspection did not give the node an outside and "
231+ "an inside port; leaving it manageable"
232+ )
233+
234+ for port in role_ports :
235+ if port .physical_network != physical_network :
236+ logger .info (
237+ "[node:%s] Setting physical_network=%s on port %s (was %s)" ,
238+ node .uuid ,
239+ physical_network ,
240+ port .name ,
241+ port .physical_network ,
242+ )
243+ client .update_port (
244+ port .uuid ,
245+ [
246+ {
247+ "op" : "add" ,
248+ "path" : "/physical_network" ,
249+ "value" : physical_network ,
250+ }
251+ ],
252+ )
253+
254+ ironic_node .transition (node , target_state = "provide" , expected_state = "available" )
255+ return node .uuid
256+
257+
163258def _update_active_firewall (
164259 * ,
165260 client : IronicClient ,
@@ -196,14 +291,15 @@ def _update_active_firewall(
196291 f"{ resource_class !r} on an in-service firewall"
197292 )
198293
199- netdev_ports = netdev_reconciler .build_netdev_ports (ports )
200- _reject_structural_drift (
201- client = client ,
202- node = node ,
203- name = name ,
204- physical_network = physical_network ,
205- netdev_ports = netdev_ports ,
206- )
294+ # Without ports there is nothing to compare: inspection owns them.
295+ if ports :
296+ _reject_structural_drift (
297+ client = client ,
298+ node = node ,
299+ name = name ,
300+ physical_network = physical_network ,
301+ netdev_ports = netdev_reconciler .build_netdev_ports (ports ),
302+ )
207303
208304 active_extra = dict (extra )
209305 if external_cmdb_id not in (None , "" ):
@@ -271,21 +367,18 @@ def argument_parser():
271367 parser .add_argument (
272368 "--physical-network" ,
273369 required = True ,
274- help = "Port physical_network" ,
370+ help = "Port physical_network, the VLAN group in Nautobot (e.g. f20-3-network) " ,
275371 )
276372 parser .add_argument (
277373 "--ports" ,
278- required = False ,
279374 default = "[]" ,
280- help = "Optional JSON array of ports to override LLDP discovery (same format "
281- "as enroll-netdev). By default, ports are discovered automatically via LLDP "
282- "during inspection. Use this option to manually specify ports that may not "
283- "be discovered properly or to override auto-discovered values. 'switch' must "
375+ help = "JSON array of ports (same format as enroll-netdev). 'switch' must "
284376 "be the switch FQDN (e.g. n11-22-1.dfw3.rackspace.net) so the Nautobot "
285377 "sync can resolve the cable. Optional per-port 'bios_name' (the device "
286378 "interface name); defaults to the label. At least two ports: the "
287379 "lowest-numbered interface becomes the router's outside NIC (gateway) "
288- "and the next one its inside NIC (subnets)." ,
380+ "and the next one its inside NIC (subnets). Leave it out to have "
381+ "inspection discover the ports over LLDP and label them." ,
289382 )
290383 parser .add_argument (
291384 "--resource-class" ,
0 commit comments