Coverage for birdplan/plugins/cmdline/bgp/peer/graceful_shutdown/set.py: 94%

35 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 graceful-shutdown 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__ = ["BirdPlanCmdlineBGPPeerGracefulShutdownSet"] 

29 

30 

31class BirdPlanCmdlineBGPPeerGracefulShutdownSet(BirdPlanCmdlinePluginBase): 

32 """BirdPlan "bgp peer graceful-shutdown 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 graceful-shutdown 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.graceful_shutdown", "get_subparsers", {}) 

57 

58 # CMD: bgp peer graceful-shutdown set 

59 subparser = parent_subparsers.add_parser( 

60 "set", help="Override the BGP graceful shutdown flag for a specific peer or pattern" 

61 ) 

62 subparser.add_argument( 

63 "--action", 

64 action="store_const", 

65 const="bgp_peer_graceful_shutdown_set", 

66 default="bgp_peer_graceful_shutdown_set", 

67 help=argparse.SUPPRESS, 

68 ) 

69 subparser.add_argument( 

70 "peer", 

71 nargs=1, 

72 metavar="PEER", 

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

74 ) 

75 subparser.add_argument( 

76 "value", 

77 nargs=1, 

78 metavar="VALUE", 

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

80 ) 

81 

82 # Set our internal subparser property 

83 self._subparser = subparser 

84 self._subparsers = None 

85 

86 def cmd_bgp_peer_graceful_shutdown_set(self, args: Any) -> Any: 

87 """ 

88 Commandline handler for "bgp peer graceful-shutdown set" action. 

89 

90 Parameters 

91 ---------- 

92 args : Dict[str, Any] 

93 Method argument(s). 

94 

95 """ 

96 

97 if not self._subparser: 

98 raise RuntimeError() 

99 

100 cmdline: BirdPlanCommandLine = args["cmdline"] 

101 

102 # Grab peer name 

103 peer = cmdline.args.peer[0] 

104 

105 # Check value is valid 

106 if cmdline.args.value[0] not in ("true", "false"): 

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

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

109 

110 # Suppress info output 

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

112 

113 # Load BirdPlan configuration using the cache 

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

115 

116 # Try set graceful shutdown flag 

117 cmdline.birdplan.state_bgp_peer_graceful_shutdown_set(peer, value) 

118 

119 # Commit BirdPlan our state 

120 cmdline.birdplan_commit_state() 

121 

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

123 

124 return BirdPlanCommandlineResult(f"BGP graceful shutdown {status} for peer(s) matching '{peer}'")