Coverage for birdplan/plugins/cmdline/ospf/interface/ecmp_weight/set.py: 97%

33 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 "ospf interface ecmp-weight set".""" 

20 

21import argparse 

22from typing import Any, Dict 

23 

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

25from ....cmdline_plugin import BirdPlanCmdlinePluginBase 

26 

27__all__ = ["BirdPlanCmdlineOSPFInterfaceECMPWeightSet"] 

28 

29 

30class BirdPlanCmdlineOSPFInterfaceECMPWeightSet(BirdPlanCmdlinePluginBase): 

31 """BirdPlan "ospf interface ecmp-weight set" command.""" 

32 

33 def __init__(self) -> None: 

34 """Initialize object.""" 

35 

36 super().__init__() 

37 

38 # Plugin setup 

39 self.plugin_description = "birdplan ospf interface ecmp-weight set" 

40 self.plugin_order = 40 

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.ospf.interface.ecmp_weight", "get_subparsers", {}) 

56 

57 # CMD: ospf interface ecmp-weight set 

58 subparser = parent_subparsers.add_parser("set", help="Set OSPF interface ECMP weight override") 

59 subparser.add_argument( 

60 "--action", 

61 action="store_const", 

62 const="ospf_interface_ecmp_weight_set", 

63 default="ospf_interface_ecmp_weight_set", 

64 help=argparse.SUPPRESS, 

65 ) 

66 subparser.add_argument( 

67 "area", 

68 nargs=1, 

69 metavar="AREA", 

70 help="Area in which the interface is in", 

71 ) 

72 subparser.add_argument( 

73 "interface", 

74 nargs=1, 

75 metavar="IFACE", 

76 help="Interface to set OSPF ECMP weight override for", 

77 ) 

78 subparser.add_argument( 

79 "ecmp_weight", 

80 nargs=1, 

81 type=int, 

82 metavar="ECMP_WEIGHT", 

83 help="OSPF ECMP weight", 

84 ) 

85 

86 # Set our internal subparser property 

87 self._subparser = subparser 

88 self._subparsers = None 

89 

90 def cmd_ospf_interface_ecmp_weight_set(self, args: Any) -> Any: 

91 """ 

92 Commandline handler for "ospf interface ecmp-weight set" action. 

93 

94 Parameters 

95 ---------- 

96 args : Dict[str, Any] 

97 Method argument(s). 

98 

99 """ 

100 

101 if not self._subparser: 

102 raise RuntimeError() 

103 

104 cmdline: BirdPlanCommandLine = args["cmdline"] 

105 

106 # Grab our args 

107 area = cmdline.args.area[0] 

108 interface = cmdline.args.interface[0] 

109 ecmp_weight = cmdline.args.ecmp_weight[0] 

110 

111 # Suppress info output 

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

113 

114 # Load BirdPlan configuration using the cache 

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

116 

117 # Set OSPF interface ECMP weight 

118 cmdline.birdplan.state_ospf_set_interface_ecmp_weight(area, interface, ecmp_weight) 

119 

120 # Commit BirdPlan our state 

121 cmdline.birdplan_commit_state() 

122 

123 return BirdPlanCommandlineResult(f"OSPF area '{area}' interface '{interface}' ECMP weight override set to {ecmp_weight}")