Coverage for birdplan/plugins/cmdline/bgp/peer/quarantine/show.py: 45%
65 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
1#
2# SPDX-License-Identifier: GPL-3.0-or-later
3#
4# Copyright (c) 2019-2024, AllWorldIT
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
19"""BirdPlan commandline options for BGP peer quarantine show."""
21import argparse
22import io
23from typing import Any, Dict
25from ...... import BirdPlanBGPPeerQuarantineStatus
26from ......cmdline import BirdPlanCommandLine, BirdPlanCommandlineResult
27from ......console.colors import colored
28from ....cmdline_plugin import BirdPlanCmdlinePluginBase
30__all__ = ["BirdPlanCmdlineBGPPeerQuarantineShow"]
33class BirdPlanCmdlineBGPPeerQuarantineShowResult(BirdPlanCommandlineResult):
34 """BirdPlan BGP peer quarantine show result."""
36 def as_text(self) -> str:
37 """
38 Return data in text format.
40 Returns
41 -------
42 str
43 Return data in text format.
45 """
47 ob = io.StringIO()
49 ob.write("BGP peer quarantine overrides:\n")
50 ob.write("------------------------------\n")
52 # Loop with sorted override list
53 for peer in sorted(self.data["overrides"]):
54 # Print out override
55 status = colored("Enabled", "red") if self.data["overrides"][peer] else colored("Disabled", "green")
56 ob.write(f" {peer}: {status}\n")
57 # If we have no overrides, just print out --none--
58 if not self.data["overrides"]:
59 ob.write("--none--\n")
61 ob.write("\n")
63 # Get a list of all peers we know about
64 peers_all = list(self.data["current"].keys()) + list(self.data["pending"].keys())
65 peers_all = sorted(set(peers_all))
67 ob.write("BGP peer quarantine status:\n")
68 ob.write("---------------------------\n")
70 # Loop with sorted peer list
71 for peer in peers_all:
72 # Grab pending status
73 pending_status = None
74 if peer in self.data["pending"]: # noqa: SIM908
75 pending_status = self.data["pending"][peer]
77 # Grab current status
78 current_status = None
79 if peer in self.data["current"]: # noqa: SIM908
80 current_status = self.data["current"][peer]
82 # Work out our status string
83 status_str = ""
84 if pending_status is None:
85 status_str = colored("REMOVED", "magenta")
86 else:
87 if current_status and not pending_status:
88 status_str = colored("PENDING-QUARANTINE-ENTER", "blue")
89 elif not current_status and pending_status:
90 status_str = colored("PENDING-QUARANTINE-EXIT", "yellow")
91 elif current_status is None:
92 status_str = colored("NEW", "green")
93 elif pending_status:
94 status_str = colored("QUARANTINED", "red")
95 else:
96 status_str = "OK"
98 ob.write(" Peer: " + colored(peer, "cyan") + "\n")
99 ob.write(f" State: {status_str}\n")
100 ob.write("\n")
102 return ob.getvalue()
105class BirdPlanCmdlineBGPPeerQuarantineShow(BirdPlanCmdlinePluginBase):
106 """BirdPlan "bgp peer quarantine show" command."""
108 def __init__(self) -> None:
109 """Initialize object."""
111 super().__init__()
113 # Plugin setup
114 self.plugin_description = "birdplan bgp peer quarantine show"
115 self.plugin_order = 30
117 def register_parsers(self, args: Dict[str, Any]) -> None:
118 """
119 Register commandline parsers.
121 Parameters
122 ----------
123 args : Dict[str, Any]
124 Method argument(s).
126 """
128 plugins = args["plugins"]
130 parent_subparsers = plugins.call_plugin("birdplan.plugins.cmdline.bgp.peer.quarantine", "get_subparsers", {})
132 # CMD: bgp peer quarantine show
133 subparser = parent_subparsers.add_parser("show", help="Show BGP peer quarantine status")
134 subparser.add_argument(
135 "--action",
136 action="store_const",
137 const="bgp_peer_quarantine_show",
138 default="bgp_peer_quarantine_show",
139 help=argparse.SUPPRESS,
140 )
142 # Set our internal subparser property
143 self._subparser = subparser
144 self._subparsers = None
146 def cmd_bgp_peer_quarantine_show(self, args: Any) -> Any:
147 """
148 Commandline handler for "bgp peer quarantine show" action.
150 Parameters
151 ----------
152 args : Dict[str, Any]
153 Method argument(s).
155 """
157 if not self._subparser: # pragma: no cover
158 raise RuntimeError()
160 cmdline: BirdPlanCommandLine = args["cmdline"]
162 # Suppress info output
163 cmdline.birdplan.birdconf.birdconfig_globals.suppress_info = True
165 # Load BirdPlan configuration using the cache
166 cmdline.birdplan_load_config(ignore_irr_changes=True, ignore_peeringdb_changes=True, use_cached=True)
168 # Grab peer list
169 res: BirdPlanBGPPeerQuarantineStatus = cmdline.birdplan.state_bgp_peer_quarantine_status()
171 return BirdPlanCmdlineBGPPeerQuarantineShowResult(res)