Coverage for birdplan/plugins/cmdline/bgp/peer/quarantine/set.py: 100%

31 statements  

« 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/>. 

18 

19"""BirdPlan commandline options for BGP peer quarantine set.""" 

20 

21import argparse 

22from typing import Any, Dict 

23 

24from ......cmdline import BirdPlanCommandLine, BirdPlanCommandlineResult 

25from ......exceptions import BirdPlanErrorUsage 

26from ....cmdline_plugin import BirdPlanCmdlinePluginBase 

27 

28__all__ = ["BirdPlanCmdlineBGPPeerQuarantineSet"] 

29 

30 

31class BirdPlanCmdlineBGPPeerQuarantineSet(BirdPlanCmdlinePluginBase): 

32 """BirdPlan "bgp peer quarantine set" command.""" 

33 

34 def __init__(self) -> None: 

35 """Initialize object.""" 

36 

37 super().__init__() 

38 

39 # Plugin setup 

40 self.plugin_description = "birdplan bgp peer quarantine set" 

41 self.plugin_order = 30 

42 

43 def register_parsers(self, args: Dict[str, Any]) -> None: 

44 """ 

45 Register commandline parsers. 

46 

47 Parameters 

48 ---------- 

49 args : Dict[str, Any] 

50 Method argument(s). 

51 

52 """ 

53 

54 plugins = args["plugins"] 

55 

56 parent_subparsers = plugins.call_plugin("birdplan.plugins.cmdline.bgp.peer.quarantine", "get_subparsers", {}) 

57 

58 # CMD: bgp peer quarantine set 

59 subparser = parent_subparsers.add_parser("set", help="Override the BGP quarantine flag for a specific peer or pattern") 

60 subparser.add_argument( 

61 "--action", 

62 action="store_const", 

63 const="bgp_peer_quarantine_set", 

64 default="bgp_peer_quarantine_set", 

65 help=argparse.SUPPRESS, 

66 ) 

67 subparser.add_argument( 

68 "peer", 

69 nargs=1, 

70 metavar="PEER", 

71 help="Peer name (* = pattern match character)", 

72 ) 

73 subparser.add_argument( 

74 "value", 

75 nargs=1, 

76 metavar="VALUE", 

77 help="Flag value ('true' or 'false')", 

78 ) 

79 

80 # Set our internal subparser property 

81 self._subparser = subparser 

82 self._subparsers = None 

83 

84 def cmd_bgp_peer_quarantine_set(self, args: Any) -> Any: 

85 """ 

86 Commandline handler for "bgp peer quarantine set" action. 

87 

88 Parameters 

89 ---------- 

90 args : Dict[str, Any] 

91 Method argument(s). 

92 

93 """ 

94 

95 if not self._subparser: # pragma: no cover 

96 raise RuntimeError() 

97 

98 cmdline: BirdPlanCommandLine = args["cmdline"] 

99 

100 # Grab peer name 

101 peer = cmdline.args.peer[0] 

102 

103 # Check value is valid 

104 if cmdline.args.value[0] not in ("true", "false"): # pragma: no cover 

105 raise BirdPlanErrorUsage("BGP peer quarantine override flag value must be 'true' or 'false'", self._subparser) 

106 value = cmdline.args.value[0] == "true" 

107 

108 # Suppress info output 

109 cmdline.birdplan.birdconf.birdconfig_globals.suppress_info = True 

110 

111 # Load BirdPlan configuration using the cache 

112 cmdline.birdplan_load_config(ignore_irr_changes=True, ignore_peeringdb_changes=True, use_cached=True) 

113 

114 # Try set quarantine flag 

115 cmdline.birdplan.state_bgp_peer_quarantine_set(peer, value) 

116 

117 # Commit BirdPlan our state 

118 cmdline.birdplan_commit_state() 

119 

120 status = "ENABLED" if value else "DISABLED" 

121 

122 return BirdPlanCommandlineResult(f"BGP quarantine {status} for peer(s) matching '{peer}'")