validate_iptrunk.py 3.43 KiB
"""Router validation workflow. Used in a nightly schedule."""
import json
from orchestrator.targets import Target
from orchestrator.utils.errors import ProcessFailureError
from orchestrator.utils.json import json_dumps
from orchestrator.workflow import StepList, done, init, step, workflow
from orchestrator.workflows.steps import resync, store_process_subscription, unsync
from orchestrator.workflows.utils import wrap_modify_initial_input_form
from gso.products.product_types.iptrunk import Iptrunk
from gso.services import infoblox
from gso.services.provisioning_proxy import execute_playbook, pp_interaction
@step("Validate IP trunk configuration")
def validate_router_config(subscription: Iptrunk, callback_route: str) -> None:
"""Run an Ansible playbook that validates the configuration that is present on an active IP trunk."""
extra_vars = {"wfo_trunk_json": json.loads(json_dumps(subscription)), "verb": "validate"}
execute_playbook(
playbook_name="base_config.yaml",
callback_route=callback_route,
inventory=f"{subscription.iptrunk.iptrunk_sides[0].iptrunk_side_node.router_fqdn}\n"
f"{subscription.iptrunk.iptrunk_sides[1].iptrunk_side_node.router_fqdn}\n",
extra_vars=extra_vars,
)
@step("Verify IPAM resources for LAG interfaces")
def verify_ipam_loopback(subscription: Iptrunk) -> None:
"""Validate the :term:`IPAM` resources for the :term:`LAG` interfaces.
Raises an :class:`orchestrator.utils.errors.ProcessFailureError` if :term:`IPAM` is configured incorrectly.
"""
ipam_errors = []
ipam_v4_network = infoblox.find_network_by_cidr(subscription.iptrunk.iptrunk_ipv4_network)
ipam_v6_network = infoblox.find_network_by_cidr(subscription.iptrunk.iptrunk_ipv6_network)
if not ipam_v4_network or not ipam_v6_network:
ipam_errors += [
(
"Missing IP trunk IPAM records, found the following instead.\n"
f"IPv4 expected {subscription.iptrunk.iptrunk_ipv4_network}, actual: {ipam_v4_network}\n"
f"IPv6 expected {subscription.iptrunk.iptrunk_ipv6_network}, actual: {ipam_v6_network}"
)
]
# Validate both sides of the trunk.
for trunk_side in subscription.iptrunk.iptrunk_sides:
lag_fqdn = f"{trunk_side.iptrunk_side_ae_iface}.{trunk_side.iptrunk_side_node.router_fqdn}"
# Validate both IPv4 and IPv6 records.
for record in [infoblox.find_host_by_fqdn(lag_fqdn), infoblox.find_v6_host_by_fqdn(lag_fqdn)]:
if not record:
ipam_errors += [f"Missing IPAM record for LAG interface {lag_fqdn}."]
elif str(subscription.subscription_id) not in record.comment:
ipam_errors += [f"Found a misconfigured IPAM entry for {lag_fqdn}. Received: {record}"]
if ipam_errors:
raise ProcessFailureError(str(ipam_errors))
@workflow(
"Validate IP trunk configuration",
target=Target.SYSTEM,
initial_input_form=wrap_modify_initial_input_form(None),
)
def validate_iptrunk() -> StepList:
"""Validate an existing, active IP Trunk subscription.
* Run an Ansible playbook to verify the configuration is intact.
* Verify that the :term:`LAG` interfaces are correctly configured in :term:`IPAM`.
"""
return (
init
>> store_process_subscription(Target.SYSTEM)
>> unsync
>> pp_interaction(validate_router_config)
>> verify_ipam_loopback
>> resync
>> done
)