Author SHA1 Message Date
David Barroso fef224775e Merge pull request #28 from napalm-automation/develop
Release 0.3.0
2016-12-01 14:14:29 +01:00
David Barroso 562529ac7a Merge pull request #21 from napalm-automation/develop
Release 0.2.1
2016-11-01 20:57:27 +01:00
David Barroso ad1a10f08b Merge pull request #19 from napalm-automation/develop
Release 0.2.0
2016-10-25 11:11:07 +02:00
11 changed files with 83 additions and 91 deletions

No files matched your search

-1
View File
@@ -60,4 +60,3 @@ env
test/unit/test_devices.py
report.json
+14 -10
View File
@@ -1,11 +1,20 @@
language: python
python:
- 2.7
- 3.4
- 3.5
- 2.7
install:
- pip install tox-travis
- pip install coveralls
- pip install -r requirements-dev.txt
- pip install .
script:
- py.test
- pylama .
after_success:
- coveralls
- if [ $TRAVIS_TAG ]; then curl -X POST https://readthedocs.org/build/napalm; fi
deploy:
provider: pypi
user: dbarroso
@@ -14,8 +23,3 @@ deploy:
on:
tags: true
branch: master
script:
- tox
after_success:
- coveralls
- if [ $TRAVIS_TAG ]; then curl -X POST https://readthedocs.org/build/napalm; fi
+47 -58
View File
@@ -11,20 +11,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
from __future__ import unicode_literals
import re
from pyFG.fortios import FortiOS, FortiConfig, logger
from pyFG.exceptions import FailedCommit, CommandExecutionException
from napalm_base.base import NetworkDriver
from napalm_base.exceptions import ReplaceConfigException, MergeConfigException
from napalm_base.utils.string_parsers import colon_separated_string_to_dict,\
convert_uptime_string_seconds
from napalm_base.utils import py23_compat
try:
from napalm.base.base import NetworkDriver
except ImportError:
from napalm_base.base import NetworkDriver
class FortiOSDriver(NetworkDriver):
@@ -54,7 +47,7 @@ class FortiOSDriver(NetworkDriver):
'is_alive': self.device.ssh.get_transport().is_active()
}
def _execute_command_with_vdom(self, command, vdom=None):
def execute_command_with_vdom(self, command, vdom=None):
# If the user doesn't specify a particular vdom we use the default vdom for the object.
vdom = vdom or self.vdom
@@ -77,8 +70,8 @@ class FortiOSDriver(NetworkDriver):
# If there is no vdom we just execute the command
return self.device.execute_command(command)
def _get_command_with_vdom(self, cmd, separator=':', auto=False, vdom=None):
output = self._execute_command_with_vdom(cmd, vdom)
def get_command_with_vdom(self, cmd, separator=':', auto=False, vdom=None):
output = self.execute_command_with_vdom(cmd, vdom)
if auto:
if ':' in output[0]:
@@ -128,7 +121,7 @@ class FortiOSDriver(NetworkDriver):
def commit_config(self):
try:
self._execute_command_with_vdom('execute backup config flash commit_with_napalm')
self.execute_command_with_vdom('execute backup config flash commit_with_napalm')
self.device.commit()
self.discard_config()
except FailedCommit as e:
@@ -142,9 +135,9 @@ class FortiOSDriver(NetworkDriver):
self.device.load_config(in_candidate=True)
def rollback(self):
output = self._execute_command_with_vdom('fnsysctl ls -l data2/config', vdom=None)
output = self.execute_command_with_vdom('fnsysctl ls -l data2/config', vdom=None)
rollback_file = output[-2].split()[-1]
rollback_config = self._execute_command_with_vdom(
rollback_config = self.execute_command_with_vdom(
'fnsysctl cat data2/config/{rollback_file}'.format(rollback_file))
self.device.load_config(empty_candidate=True)
@@ -166,12 +159,12 @@ class FortiOSDriver(NetworkDriver):
get_candidate = retrieve == "all" or retrieve == "candidate"
if retrieve == "all" or get_running:
result = self._execute_command_with_vdom('show')
result = self.execute_command_with_vdom('show')
text_result = '\n'.join(result)
return {
'startup': u"",
'running': py23_compat.text_type(text_result),
'running': unicode(text_result),
'candidate': u"",
}
@@ -183,24 +176,22 @@ class FortiOSDriver(NetworkDriver):
}
def get_facts(self):
system_status = self._get_command_with_vdom('get system status', vdom='global')
performance_status = self._get_command_with_vdom('get system performance status',
vdom='global')
system_status = self.get_command_with_vdom('get system status', vdom='global')
performance_status = self.get_command_with_vdom('get system performance status',
vdom='global')
interfaces = self._execute_command_with_vdom('get system interface | grep ==',
vdom='global')
interfaces = self.execute_command_with_vdom('get system interface | grep ==', vdom='global')
interface_list = [x.split()[2] for x in interfaces if x.strip() is not '']
domain = self._get_command_with_vdom('get system dns | grep domain',
vdom='global')['domain']
domain = self.get_command_with_vdom('get system dns | grep domain', vdom='global')['domain']
return {
'vendor': py23_compat.text_type('Fortigate'),
'os_version': py23_compat.text_type(system_status['Version'].split(',')[0].split()[1]),
'vendor': unicode('Fortigate'),
'os_version': unicode(system_status['Version'].split(',')[0].split()[1]),
'uptime': convert_uptime_string_seconds(performance_status['Uptime']),
'serial_number': py23_compat.text_type(system_status['Serial-Number']),
'model': py23_compat.text_type(system_status['Version'].split(',')[0].split()[0]),
'hostname': py23_compat.text_type(system_status['Hostname']),
'serial_number': unicode(system_status['Serial-Number']),
'model': unicode(system_status['Version'].split(',')[0].split()[0]),
'hostname': unicode(system_status['Hostname']),
'fqdn': u'{}.{}'.format(system_status['Hostname'], domain),
'interface_list': interface_list
}
@@ -227,13 +218,12 @@ class FortiOSDriver(NetworkDriver):
}
def get_interfaces(self):
cmd_data = self._execute_command_with_vdom('diagnose hardware deviceinfo nic',
vdom='global')
cmd_data = self.execute_command_with_vdom('diagnose hardware deviceinfo nic', vdom='global')
interface_list = [x.replace('\t', '') for x in cmd_data if x.startswith('\t')]
interface_statistics = {}
for interface in interface_list:
if_data = self._execute_command_with_vdom(
if_data = self.execute_command_with_vdom(
'diagnose hardware deviceinfo nic {}'.format(interface), vdom='global')
parsed_data = {}
if interface.startswith('mgmt'):
@@ -246,7 +236,7 @@ class FortiOSDriver(NetworkDriver):
elif line.startswith('Link'):
parsed_data['is_up'] = line.split('\t')[-1] is 'up'
elif line.startswith('Current_HWaddr'):
parsed_data['mac_address'] = py23_compat.text_type(line.split('\t')[-1])
parsed_data['mac_address'] = unicode(line.split('\t')[-1])
parsed_data['is_enabled'] = True
parsed_data['description'] = u''
parsed_data['last_flapped'] = -1.0
@@ -259,7 +249,7 @@ class FortiOSDriver(NetworkDriver):
elif line.startswith('PHY Speed'):
parsed_data['speed'] = int(line.split(':')[-1])
elif line.startswith('Current_HWaddr'):
parsed_data['mac_address'] = py23_compat.text_type(line.split(' ')[-1])
parsed_data['mac_address'] = unicode(line.split(' ')[-1])
parsed_data['description'] = u''
parsed_data['last_flapped'] = -1.0
interface_statistics[interface] = parsed_data
@@ -272,7 +262,7 @@ class FortiOSDriver(NetworkDriver):
return l
def get_firewall_policies(self):
cmd = self._execute_command_with_vdom('show firewall policy')
cmd = self.execute_command_with_vdom('show firewall policy')
policy = dict()
policy_id = None
default_policy = dict()
@@ -301,16 +291,16 @@ class FortiOSDriver(NetworkDriver):
policy_item['position'] = position
policy_item['packet_hits'] = -1
policy_item['byte_hits'] = -1
policy_item['id'] = py23_compat.text_type(key)
policy_item['id'] = unicode(key)
policy_item['enabled'] = enabled
policy_item['schedule'] = py23_compat.text_type(policy[key]['schedule'])
policy_item['log'] = py23_compat.text_type(logtraffic)
policy_item['l3_src'] = py23_compat.text_type(policy[key]['srcaddr'])
policy_item['l3_dst'] = py23_compat.text_type(policy[key]['dstaddr'])
policy_item['service'] = py23_compat.text_type(policy[key]['service'])
policy_item['src_zone'] = py23_compat.text_type(policy[key]['srcintf'])
policy_item['dst_zone'] = py23_compat.text_type(policy[key]['dstintf'])
policy_item['action'] = py23_compat.text_type(action)
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
@@ -325,14 +315,14 @@ class FortiOSDriver(NetworkDriver):
command_received = 'get router info bgp neighbors {} received-routes | grep prefixes '
peers = dict()
bgp_sum = self._execute_command_with_vdom(command_sum)
bgp_sum = self.execute_command_with_vdom(command_sum)
re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)}
self.device.load_config('router bgp')
for neighbor, parameters in neighbors.items():
for neighbor, parameters in neighbors.iteritems():
logger.debug('NEW PEER')
neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)]
@@ -350,10 +340,10 @@ class FortiOSDriver(NetworkDriver):
neighbor_dict['address_family']['ipv6'] = dict()
detail_output = [x.lower() for x in
self._execute_command_with_vdom(command_detail.format(neighbor))]
self.execute_command_with_vdom(command_detail.format(neighbor))]
m = re.search('remote router id (.+?)\n', '\n'.join(detail_output))
if m:
neighbor_dict['remote_id'] = py23_compat.text_type(m.group(1))
neighbor_dict['remote_id'] = unicode(m.group(1))
else:
raise Exception('cannot find remote router id for %s' % neighbor)
@@ -362,12 +352,12 @@ class FortiOSDriver(NetworkDriver):
x = detail_output.index(' for address family: {} unicast'.format(family))
block = detail_output[x:]
for term, fortiname in terms.items():
for term, fortiname in terms.iteritems():
text = self._search_line_in_lines('%s prefixes' % fortiname, block)
t = [int(s) for s in text.split() if s.isdigit()][0]
neighbor_dict['address_family'][family][term] = t
received = self._execute_command_with_vdom(
received = self.execute_command_with_vdom(
command_received.format(neighbor))[0].split()
if len(received) > 0:
neighbor_dict['address_family'][family]['received_prefixes'] = received[-1]
@@ -378,13 +368,13 @@ class FortiOSDriver(NetworkDriver):
return {
'global': {
'router_id': py23_compat.text_type(bgp_sum[0].split()[3]),
'router_id': unicode(bgp_sum[0].split()[3]),
'peers': peers
}
}
def get_interfaces_counters(self):
cmd = self._execute_command_with_vdom('fnsysctl ifconfig', vdom=None)
cmd = self.execute_command_with_vdom('fnsysctl ifconfig', vdom=None)
if_name = None
interface_counters = dict()
for line in cmd:
@@ -472,17 +462,16 @@ class FortiOSDriver(NetworkDriver):
v = int(self._search_line_in_lines('upper_non_recoverable',
sensor_block).split('=')[1])
temp_value = int(temp_value)
output[sensor_name] = dict(temperature=float(temp_value), is_alert=is_alert,
is_critical=True if temp_value > v else False)
is_critical=True if v > temp_value else False)
return output
out = dict()
sensors_block = [parse_string(x) for x in
self._execute_command_with_vdom('execute sensor detail', vdom='global')
self.execute_command_with_vdom('execute sensor detail', vdom='global')
if x]
# temp
@@ -496,14 +485,14 @@ class FortiOSDriver(NetworkDriver):
# cpu
out['cpu'] = get_cpu(
[x for x in
self._execute_command_with_vdom('get system performance status | grep CPU',
vdom='global')[1:] if x])
self.execute_command_with_vdom('get system performance status | grep CPU',
vdom='global')[1:] if x])
# memory
memory_command = 'diag hard sys mem | grep Mem:'
t = [x for x in
re.split('\s+', self._execute_command_with_vdom(memory_command,
vdom='global')[0]) if x]
re.split('\s+', self.execute_command_with_vdom(memory_command,
vdom='global')[0]) if x]
out['memory'] = get_memory(t)
# power, not implemented
+1
View File
@@ -0,0 +1 @@
{"included": [{"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_bgp_neighbors[normal]", "teardown": {"duration": 7.700920104980469e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.0003800392150878906, "outcome": "passed", "name": "setup"}, "run_index": 11, "call": {"duration": 0.004490852355957031, "outcome": "passed", "name": "call"}, "duration": 0.005327939987182617, "outcome": "passed"}, "type": "test", "id": 1}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_route_to[no_test_case_found]", "teardown": {"duration": 6.389617919921875e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.0003459453582763672, "outcome": "passed", "name": "setup"}, "run_index": 13, "call": {"duration": 0.00012302398681640625, "outcome": "skipped", "name": "call", "longrepr": "('/Users/dbarroso/.virtualenvs/napalm/lib/python2.7/site-packages/napalm_base/test/getters.py', 78, u'Skipped: Method not implemented')"}, "duration": 0.0008788108825683594, "outcome": "skipped"}, "type": "test", "id": 2}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_arp_table[no_test_case_found]", "teardown": {"duration": 6.914138793945312e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.000370025634765625, "outcome": "passed", "name": "setup"}, "run_index": 1, "call": {"duration": 0.00012612342834472656, "outcome": "skipped", "name": "call", "longrepr": "('/Users/dbarroso/.virtualenvs/napalm/lib/python2.7/site-packages/napalm_base/test/getters.py', 78, u'Skipped: Method not implemented')"}, "duration": 0.0009353160858154297, "outcome": "skipped"}, "type": "test", "id": 3}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_network_instances[no_test_case_found]", "teardown": {"duration": 7.104873657226562e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.0004589557647705078, "outcome": "passed", "name": "setup"}, "run_index": 8, "call": {"duration": 0.000125885009765625, "outcome": "skipped", "name": "call", "longrepr": "('/Users/dbarroso/.virtualenvs/napalm/lib/python2.7/site-packages/napalm_base/test/getters.py', 78, u'Skipped: Method not implemented')"}, "duration": 0.0011148452758789062, "outcome": "skipped"}, "type": "test", "id": 4}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_probes_config[no_test_case_found]", "teardown": {"duration": 7.009506225585938e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.0003409385681152344, "outcome": "passed", "name": "setup"}, "run_index": 3, "call": {"duration": 0.00026488304138183594, "outcome": "skipped", "name": "call", "longrepr": "('/Users/dbarroso/.virtualenvs/napalm/lib/python2.7/site-packages/napalm_base/test/getters.py', 78, u'Skipped: Method not implemented')"}, "duration": 0.001016855239868164, "outcome": "skipped"}, "type": "test", "id": 5}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_ntp_servers[no_test_case_found]", "teardown": {"duration": 0.00011706352233886719, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.007989168167114258, "outcome": "passed", "name": "setup"}, "run_index": 0, "call": {"duration": 0.00020694732666015625, "outcome": "skipped", "name": "call", "longrepr": "('/Users/dbarroso/.virtualenvs/napalm/lib/python2.7/site-packages/napalm_base/test/getters.py', 78, u'Skipped: Method not implemented')"}, "duration": 0.01630234718322754, "outcome": "skipped"}, "type": "test", "id": 6}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_users[no_test_case_found]", "teardown": {"duration": 6.29425048828125e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.00034308433532714844, "outcome": "passed", "name": "setup"}, "run_index": 18, "call": {"duration": 0.00011801719665527344, "outcome": "skipped", "name": "call", "longrepr": "('/Users/dbarroso/.virtualenvs/napalm/lib/python2.7/site-packages/napalm_base/test/getters.py', 78, u'Skipped: Method not implemented')"}, "duration": 0.0008671283721923828, "outcome": "skipped"}, "type": "test", "id": 7}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_interfaces_counters[normal]", "teardown": {"duration": 7.891654968261719e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.0003077983856201172, "outcome": "passed", "name": "setup"}, "run_index": 10, "call": {"duration": 0.007678031921386719, "outcome": "passed", "name": "call"}, "duration": 0.00837254524230957, "outcome": "passed"}, "type": "test", "id": 8}, {"attributes": {"name": "test/unit/test_getters.py::TestGetter::()::test_get_config[normal]", "teardown": {"duration": 6.29425048828125e-05, "outcome": "passed", "name": "teardown"}, "setup": {"duration": 0.00037789344787597656, "outcome": "passed", "name": "setup"}, "run_index": 25, "call": {"duration": 0.0005838871002197266, "outcome": "passed", "name":Line truncated
+1 -1
View File
@@ -1,8 +1,8 @@
coveralls
pytest
pytest-cov
pytest-json
pytest-pythonpath
pylama
flake8-import-order
coveralls
-r requirements.txt
+2 -2
View File
@@ -1,3 +1,3 @@
napalm-base>=0.23.0
pyFG>=0.49_1
napalm-base>=0.18.0
pyFG
future
+4 -4
View File
@@ -1,15 +1,15 @@
[pylama]
linters = mccabe,pep8,pyflakes
ignore = D203,C901
skip = build/*,.tox/*
[pylama:pep8]
max_line_length = 100
[pytest]
addopts = --cov=napalm_fortios --cov-report term-missing -vs --pylama
[tool:pytest]
addopts = --cov=./ -vs
json_report = report.json
jsonapi = true
[coverage:run]
source = napalm_fortios
include =
napalm_fortios/*
+2 -5
View File
@@ -8,14 +8,11 @@ from pip.req import parse_requirements
__author__ = 'David Barroso <dbarrosop@dravetech.com>'
install_reqs = parse_requirements('requirements.txt', session=uuid.uuid1())
try:
reqs = [str(ir.req) for ir in install_reqs]
except:
reqs = [str(ir.requirement) for ir in install_reqs]
reqs = [str(ir.req) for ir in install_reqs]
setup(
name="napalm-fortios",
version="0.4.1",
version="0.3.0",
packages=find_packages(),
author="David Barroso",
author_email="dbarrosop@dravetech.com",
@@ -1 +1 @@
{"1": [{"src_zone": "any", "log": "False", "service": "ALL", "schedule": "always", "l3_src": "all", "enabled": false, "packet_hits": -1, "l3_dst": "all", "dst_zone": "any", "action": "permit", "position": 1, "byte_hits": -1, "id": "1"}] }
{"1": [{"src_zone": "any", "log": "False", "service": "ALL", "schedule": "always", "l3_src": "all", "enabled": false, "packet_hits": -1, "l3_dst": "all", "dst_zone": "any", "action": "permit", "position": 1, "byte_hits": -1, "id": "1"}], "2": [{"src_zone": "port3", "log": "all", "service": "ALL", "schedule": "always", "l3_src": "all", "enabled": false, "packet_hits": -1, "l3_dst": "all", "dst_zone": "port2", "action": "reject", "position": 2, "byte_hits": -1, "id": "2"}]}
@@ -9,4 +9,15 @@ config firewall policy
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
-9
View File
@@ -1,9 +0,0 @@
[tox]
envlist = py27,py34,py35
[testenv]
deps =
-rrequirements-dev.txt
commands=
py.test