Repository: napalm-automation/napalm
Pull request #1910 (2023-04-28 20:27:29)

get_bgp_config for Cisco when no BGP is configured
Tested with no BGP config.

Tested with group="_" both with and without any BGP config.

Tested with group="bogus" with no BGP config.



Patch:
diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py
index 7c01fc65..5e31c49f 100644
--- a/napalm/ios/ios.py
+++ b/napalm/ios/ios.py
@@ -1463,6 +1463,11 @@ class IOSDriver(NetworkDriver):
         bgp_config_list = napalm.base.helpers.netutils_parse_objects(
             "router bgp", cfg["running"]
         )
+
+        # No BGP configuration
+        if not bgp_config_list:
+            return {}
+
         bgp_asn = napalm.base.helpers.regex_find_txt(
             r"router bgp (\d+)", bgp_config_list, default=0
         )
@@ -1569,21 +1574,25 @@ class IOSDriver(NetworkDriver):
                 "route_reflector_client": route_reflector_client,
             }
 
-        bgp_config["_"] = {
-            "apply_groups": [],
-            "description": "",
-            "local_as": bgp_asn,
-            "type": "",
-            "import_policy": "",
-            "export_policy": "",
-            "local_address": "",
-            "multipath": False,
-            "multihop_ttl": 0,
-            "remote_as": 0,
-            "remove_private_as": False,
-            "prefix_limit": {},
-            "neighbors": bgp_group_neighbors.get("_", {}),
-        }
+        # Do not include the no-group ("_") if a group argument is passed in
+        # unless group argument is "_"
+        if not group or group == "_":
+            bgp_config["_"] = {
+                "apply_groups": [],
+                "description": "",
+                "local_as": bgp_asn,
+                "type": "",
+                "import_policy": "",
+                "export_policy": "",
+                "local_address": "",
+                "multipath": False,
+                "multihop_ttl": 0,
+                "remote_as": 0,
+                "remove_private_as": False,
+                "prefix_limit": {},
+                "neighbors": bgp_group_neighbors.get("_", {}),
+            }
+
         # Get the peer-group level config for each group
         for group_name in bgp_group_neighbors.keys():
             # If a group is passed in params, only continue on that group


Test patch:
diff --git a/test/ios/mocked_data/test_get_bgp_config/no_bgp/expected_result.json b/test/ios/mocked_data/test_get_bgp_config/no_bgp/expected_result.json
new file mode 100644
index 00000000..0967ef42
--- /dev/null
+++ b/test/ios/mocked_data/test_get_bgp_config/no_bgp/expected_result.json
@@ -0,0 +1,1 @@
+{}
diff --git a/test/ios/mocked_data/test_get_bgp_config/no_bgp/show_running_config.txt b/test/ios/mocked_data/test_get_bgp_config/no_bgp/show_running_config.txt
new file mode 100644
index 00000000..50e08a54
--- /dev/null
+++ b/test/ios/mocked_data/test_get_bgp_config/no_bgp/show_running_config.txt
@@ -0,0 +1,150 @@
+!
+! Last configuration change at 18:41:02 UTC Thu Nov 24 2016
+!
+version 15.5
+service timestamps debug datetime msec
+service timestamps log datetime msec
+no platform punt-keepalive disable-kernel-core
+platform console auto
+!
+hostname CSR1
+!
+boot-start-marker
+boot-end-marker
+!
+!
+enable password cisco
+!
+aaa new-model
+!
+!
+aaa authentication login default local
+aaa authorization exec default local
+!
+!
+!
+!
+!
+aaa session-id common
+!
+ip vrf MGMT
+!
+!
+!
+!
+!
+!
+!
+!
+!
+
+
+ip domain name example.local
+
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+subscriber templating
+!
+multilink bundle-name authenticated
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+license udi pid CSR1000V sn 9OSEGKJXRHE
+spanning-tree extend system-id
+!
+username cisco privilege 15 password 0 cisco
+!
+redundancy
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+!
+interface Loopback0
+ ip address 1.1.1.1 255.255.255.255
+!
+interface GigabitEthernet1
+ ip vrf forwarding MGMT
+ ip address 192.168.35.121 255.255.255.0
+ negotiation auto
+!
+interface GigabitEthernet2
+ ip address 10.1.1.1 255.255.255.0
+ negotiation auto
+!
+interface GigabitEthernet3
+ no ip address
+ shutdown
+ negotiation auto
+!
+router ospf 1
+ redistribute connected subnets
+ network 10.1.1.0 0.0.0.255 area 0
+!
+!
+!
+!
+virtual-service csr_mgmt
+!
+ip forward-protocol nd
+!
+no ip http server
+no ip http secure-server
+!
+!
+!
+!
+!
+!
+control-plane
+!
+ !
+ !
+ !
+ !
+!
+!
+!
+!
+!
+line con 0
+line vty 0 4
+!
+!
+end