From ac461281684432c04c37b312d5d61aefd1532143 Mon Sep 17 00:00:00 2001 From: awlx Date: Tue, 29 Nov 2016 09:56:23 +0100 Subject: [PATCH 1/5] Initial commit --- napalm_fortios/fortios.py | 45 +++++++++++++++++++ .../normal/expected_result.json | 1 + .../normal/show_firewall_policy.txt | 23 ++++++++++ 3 files changed, 69 insertions(+) create mode 100644 test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json create mode 100644 test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt diff --git a/napalm_fortios/fortios.py b/napalm_fortios/fortios.py index 365e6f5..5663533 100644 --- a/napalm_fortios/fortios.py +++ b/napalm_fortios/fortios.py @@ -261,6 +261,51 @@ class FortiOSDriver(NetworkDriver): if search in l: return l + def get_firewall_policies(self): + cmd = self.execute_command_with_vdom('show firewall policy') + policy = dict() + policy_id = None + default_policy = dict() + position = 1 + + for line in cmd: + policy_data = line.strip() + if policy_data.find("edit") == 0: + policy_id = policy_data.split()[1] + policy[policy_id] = dict() + if policy_id is not None: + if len(policy_data.split()) > 2: + policy_setting = policy_data.split()[1] + policy[policy_id][policy_setting] = policy_data.split()[2].replace("\"", "") + + for key in policy: + + enabled = 'status' in policy[key] + + logtraffic = policy[key]['logtraffic'] if 'logtraffic' in policy[key] else False + + action = 'permit' if 'action' in policy[key] else 'reject' + + policy_item = dict() + default_policy[key] = list() + policy_item['position'] = position + policy_item['packet_hits'] = -1 + policy_item['byte_hits'] = -1 + policy_item['id'] = unicode(key) + policy_item['enabled'] = enabled + policy_item['schedule'] = unicode(policy[key]['schedule']) + policy_item['log'] = unicode(logtraffic) + policy_item['l3_src'] = unicode(policy[key]['srcaddr']) + policy_item['l3_dst'] = unicode(policy[key]['dstaddr']) + policy_item['service'] = unicode(policy[key]['service']) + policy_item['src_zone'] = unicode(policy[key]['srcintf']) + policy_item['dst_zone'] = unicode(policy[key]['dstintf']) + policy_item['action'] = unicode(action) + default_policy[key].append(policy_item) + + position = position + 1 + return default_policy + def get_bgp_neighbors(self): families = ['ipv4', 'ipv6'] diff --git a/test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json b/test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json new file mode 100644 index 0000000..07f8e47 --- /dev/null +++ b/test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json @@ -0,0 +1 @@ +{'1': [{'src_zone': u'any', 'log': u'False', 'service': u'ALL', 'schedule': u'always', 'l3_src': u'all', 'enabled': False, 'packet_hits': -1, 'l3_dst': u'all', 'dst_zone': u'any', 'action': u'permit', 'position': 1, 'byte_hits': -1, 'id': u'1'}], '2': [{'src_zone': u'port3', 'log': u'all', 'service': u'ALL', 'schedule': u'always', 'l3_src': u'all', 'enabled': False, 'packet_hits': -1, 'l3_dst': u'all', 'dst_zone': u'port2', 'action': u'reject', 'position': 2, 'byte_hits': -1, 'id': u'2'}]} diff --git a/test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt b/test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt new file mode 100644 index 0000000..996a5d7 --- /dev/null +++ b/test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt @@ -0,0 +1,23 @@ +config firewall policy + edit 1 + set uuid 9d747de8-dad4-51e5-7a7e-d078aa77140a + set srcintf "any" + set dstintf "any" + set srcaddr "all" + set dstaddr "all" + set action accept + set schedule "always" + set service "ALL" + next + edit 2 + set name "bla" + set uuid de597b64-aca8-51e6-5d00-b7874e6b72b8 + set srcintf "port3" + set dstintf "port2" + set srcaddr "all" + set dstaddr "all" + set schedule "always" + set service "ALL" + set logtraffic all + next +end From 91e179c6593eb8ab0c55bd10f05cf3a9542a6459 Mon Sep 17 00:00:00 2001 From: awlx Date: Tue, 29 Nov 2016 10:07:50 +0100 Subject: [PATCH 2/5] Patched is_alive test failing --- test/unit/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit/conftest.py b/test/unit/conftest.py index 8a8ee31..b9f7825 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -40,6 +40,8 @@ class PatchedDriver(OriginalDriver): def close(self): pass + def is_alive(self): + pass class FakeDevice(BaseTestDouble): """Device test double.""" From e2eeda8b9f167b57c7381f5413eb0fcc247930dd Mon Sep 17 00:00:00 2001 From: awlx Date: Tue, 29 Nov 2016 10:20:09 +0100 Subject: [PATCH 3/5] is_alive return {'is_alive': True} --- test/unit/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/conftest.py b/test/unit/conftest.py index b9f7825..f7d99fb 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -41,7 +41,7 @@ class PatchedDriver(OriginalDriver): pass def is_alive(self): - pass + return({'is_alive': True}) class FakeDevice(BaseTestDouble): """Device test double.""" From fc7579f1b4a0368e41ac7c4b990480bc9a401f27 Mon Sep 17 00:00:00 2001 From: awlx Date: Tue, 29 Nov 2016 10:24:29 +0100 Subject: [PATCH 4/5] Added mockdata --- test/unit/conftest.py | 1 + test/unit/mocked_data/test_is_alive/normal/expected_result.json | 1 + 2 files changed, 2 insertions(+) create mode 100644 test/unit/mocked_data/test_is_alive/normal/expected_result.json diff --git a/test/unit/conftest.py b/test/unit/conftest.py index f7d99fb..5ff70d2 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -43,6 +43,7 @@ class PatchedDriver(OriginalDriver): def is_alive(self): return({'is_alive': True}) + class FakeDevice(BaseTestDouble): """Device test double.""" diff --git a/test/unit/mocked_data/test_is_alive/normal/expected_result.json b/test/unit/mocked_data/test_is_alive/normal/expected_result.json new file mode 100644 index 0000000..43b92ac --- /dev/null +++ b/test/unit/mocked_data/test_is_alive/normal/expected_result.json @@ -0,0 +1 @@ +{"is_alive": true} From ee28aa880736703d5b5f0401825388c592c67f0d Mon Sep 17 00:00:00 2001 From: awlx Date: Tue, 29 Nov 2016 10:26:51 +0100 Subject: [PATCH 5/5] Whitespace fix on line 285 in fortios.py --- napalm_fortios/fortios.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napalm_fortios/fortios.py b/napalm_fortios/fortios.py index 5663533..46d5efe 100644 --- a/napalm_fortios/fortios.py +++ b/napalm_fortios/fortios.py @@ -282,7 +282,7 @@ class FortiOSDriver(NetworkDriver): enabled = 'status' in policy[key] - logtraffic = policy[key]['logtraffic'] if 'logtraffic' in policy[key] else False + logtraffic = policy[key]['logtraffic'] if 'logtraffic' in policy[key] else False action = 'permit' if 'action' in policy[key] else 'reject'