Coverage for birdplan/plugins/cmdline/bgp/peer/graceful_shutdown/remove.py: 69%

29 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 remove.""" 

20 

21import argparse 

22from typing import Any, Dict 

23 

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

25from ....cmdline_plugin import BirdPlanCmdlinePluginBase 

26 

27__all__ = ["BirdPlanCmdlineBGPPeerGracefulShutdownRemove"] 

28 

29 

30class BirdPlanCmdlineBGPPeerGracefulShutdownRemove(BirdPlanCmdlinePluginBase): 

31 """BirdPlan "bgp peer graceful-shutdown remove" command.""" 

32 

33 def __init__(self) -> None: 

34 """Initialize object.""" 

35 

36 super().__init__() 

37 

38 # Plugin setup 

39 self.plugin_description = "birdplan bgp peer graceful-shutdown remove" 

40 self.plugin_order = 30 

41 

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

43 """ 

44 Register commandline parsers. 

45 

46 Parameters 

47 ---------- 

48 args : Dict[str, Any] 

49 Method argument(s). 

50 

51 """ 

52 

53 plugins = args["plugins"] 

54 

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

56 

57 # CMD: bgp peer graceful-shutdown remove 

58 subparser = parent_subparsers.add_parser( 

59 "remove", help="Remove a BGP graceful shutdown override flag for a specific peer or pattern" 

60 ) 

61 subparser.add_argument( 

62 "--action", 

63 action="store_const", 

64 const="bgp_peer_graceful_shutdown_remove", 

65 default="bgp_peer_graceful_shutdown_remove", 

66 help=argparse.SUPPRESS, 

67 ) 

68 subparser.add_argument( 

69 "peer", 

70 nargs=1, 

71 metavar="PEER", 

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

73 ) 

74 

75 # Set our internal subparser property 

76 self._subparser = subparser 

77 self._subparsers = None 

78 

79 def cmd_bgp_peer_graceful_shutdown_remove(self, args: Any) -> Any: 

80 """ 

81 Commandline handler for "bgp peer graceful-shutdown remove" action. 

82 

83 Parameters 

84 ---------- 

85 args : Dict[str, Any] 

86 Method argument(s). 

87 

88 """ 

89 

90 if not self._subparser: 

91 raise RuntimeError() 

92 

93 cmdline: BirdPlanCommandLine = args["cmdline"] 

94 

95 peer = cmdline.args.peer[0] 

96 

97 # Suppress info output 

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

99 

100 # Load BirdPlan configuration using the cache 

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

102 

103 # Try remove graceful shutdown override flag 

104 cmdline.birdplan.state_bgp_peer_graceful_shutdown_remove(peer) 

105 

106 # Commit BirdPlan our state 

107 cmdline.birdplan_commit_state() 

108 

109 return BirdPlanCommandlineResult(f"BGP graceful shutdown REMOVED from peer(s) matching '{peer}'")