|
18 | 18 | """Palo Alto Networks Firewall object""" |
19 | 19 |
|
20 | 20 | # import modules |
| 21 | +import itertools |
21 | 22 | import re |
22 | 23 | import logging |
23 | 24 | import xml.etree.ElementTree as ET |
@@ -360,6 +361,63 @@ def commit_policy_and_objects(self, sync=False, exception=False): |
360 | 361 | return self._commit(sync=sync, exclude="policy-and-objects", |
361 | 362 | exception=exception) |
362 | 363 |
|
| 364 | + def organize_into_vsys(self, create_vsys_objects=True, refresh_vsys=True): |
| 365 | + """Organizes all imported objects under the appropriate Vsys object. |
| 366 | +
|
| 367 | + Args: |
| 368 | + create_vsys_objects (bool): Create the vsys objects (True) or use the ones already connected to this firewall (False). |
| 369 | + refresh_vsys (bool): Refresh all vsys objects' parameters before doing the reorganization or not. This is assumed True if create_vsys_objects is True. |
| 370 | +
|
| 371 | + """ |
| 372 | + from pandevice import network |
| 373 | + |
| 374 | + # Mapping of device.Vsys params to pandevice classes. |
| 375 | + mapping = { |
| 376 | + 'interface': network.Interface, |
| 377 | + 'vlans': network.Vlan, |
| 378 | + 'virtual_wires': network.VirtualWire, |
| 379 | + 'virtual_routers': network.VirtualRouter, |
| 380 | + } |
| 381 | + |
| 382 | + # Optional: create the vsys objects. |
| 383 | + if create_vsys_objects: |
| 384 | + device.Vsys.refreshall(self, name_only=True) |
| 385 | + |
| 386 | + # Vsys to put objects into. |
| 387 | + available_vsys = [x for x in self.children |
| 388 | + if isinstance(x, device.Vsys)] |
| 389 | + |
| 390 | + # Optional: refresh the vsys params. |
| 391 | + if create_vsys_objects or refresh_vsys: |
| 392 | + for x in available_vsys: |
| 393 | + x.refresh(refresh_children=False) |
| 394 | + |
| 395 | + # List of objects we need to iterate over. |
| 396 | + parents = self.children[:] |
| 397 | + |
| 398 | + # Reorganize into vsys. |
| 399 | + for x in itertools.chain(parents): |
| 400 | + # Skip device.Vsys children. |
| 401 | + if isinstance(x, device.Vsys): |
| 402 | + continue |
| 403 | + |
| 404 | + # Add children for later processing. |
| 405 | + parents.extend(x.children) |
| 406 | + |
| 407 | + # Check this class against the importable classes. |
| 408 | + for param, importable_class in mapping.items(): |
| 409 | + if isinstance(x, importable_class): |
| 410 | + # Importable class found, check if it should be moved. |
| 411 | + for vsys in available_vsys: |
| 412 | + if (getattr(vsys, param) is not None and |
| 413 | + x.uid in getattr(vsys, param)): |
| 414 | + # If its vsys isn't right, move it. |
| 415 | + if x.vsys != vsys.uid: |
| 416 | + x.parent.remove(x) |
| 417 | + vsys.add(x) |
| 418 | + break |
| 419 | + break |
| 420 | + |
363 | 421 |
|
364 | 422 | class FirewallState(object): |
365 | 423 |
|
|
0 commit comments