Skip to content

Commit

Permalink
Merge pull request #41 from napalm-automation-community/develop
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
ppieprzycki authored Dec 18, 2020
2 parents dd96e7d + a5d3b94 commit 7201ed0
Show file tree
Hide file tree
Showing 12 changed files with 805 additions and 6 deletions.
54 changes: 49 additions & 5 deletions napalm_vyos/vyos.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def load_replace_candidate(self, filename=None, config=None):
raise ReplaceConfigException('filename or config param must be provided.')

if filename is None:
temp_file = tempfile.NamedTemporaryFile()
temp_file = tempfile.NamedTemporaryFile(mode='w+')
temp_file.write(config)
temp_file.flush()
cfg_filename = temp_file.name
Expand Down Expand Up @@ -165,7 +165,7 @@ def load_merge_candidate(self, filename=None, config=None):
raise MergeConfigException('filename or config param must be provided.')

if filename is None:
temp_file = tempfile.NamedTemporaryFile()
temp_file = tempfile.NamedTemporaryFile(mode='w+')
temp_file.write(config)
temp_file.flush()
cfg_filename = temp_file.name
Expand Down Expand Up @@ -698,10 +698,15 @@ def get_facts(self):
ver_str = [line for line in output if "Version" in line][0]
version = self.parse_version(ver_str)

sn_str = [line for line in output if "S/N" in line][0]
snumber = self.parse_snumber(sn_str)
above_1_1 = False if version.startswith('1.0') or version.startswith('1.1') else True
if above_1_1:
sn_str = [line for line in output if "Hardware S/N" in line][0]
hwmodel_str = [line for line in output if "Hardware model" in line][0]
else:
sn_str = [line for line in output if "S/N" in line][0]
hwmodel_str = [line for line in output if "HW model" in line][0]

hwmodel_str = [line for line in output if "HW model" in line][0]
snumber = self.parse_snumber(sn_str)
hwmodel = self.parse_hwmodel(hwmodel_str)

output = self.device.send_command("show configuration")
Expand Down Expand Up @@ -914,3 +919,42 @@ def ping(self,
}

return ping_result

def get_config(self, retrieve="all", full=False, sanitized=False):
"""
Return the configuration of a device.
:param retrieve: String to determine which configuration type you want to retrieve, default is all of them.
The rest will be set to "".
:param full: Boolean to retrieve all the configuration. (Not supported)
:param sanitized: Boolean to remove secret data. (Only supported for 'running')
:return: The object returned is a dictionary with a key for each configuration store:
- running(string) - Representation of the native running configuration
- candidate(string) - Representation of the candidate configuration.
- startup(string) - Representation of the native startup configuration.
"""
if retrieve not in ["running", "candidate", "startup", "all"]:
raise Exception("ERROR: Not a valid option to retrieve.\nPlease select from 'running', 'candidate', "
"'startup', or 'all'")
else:
config_dict = {
"running": "",
"startup": "",
"candidate": ""
}
if retrieve in ["running", "all"]:
config_dict['running'] = self._get_running_config(sanitized)
if retrieve in ["startup", "all"]:
config_dict['startup'] = self.device.send_command(f"cat {self._BOOT_FILENAME}")
if retrieve in ["candidate", "all"]:
config_dict['candidate'] = self._new_config or ""

return config_dict

def _get_running_config(self, sanitized):
if sanitized:
return self.device.send_command("show configuration")
self.device.config_mode()
config = self.device.send_command("show")
config = config[:config.rfind('\n')]
self.device.exit_config_mode()
return config
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="napalm-vyos",
version="0.2.0",
version="0.2.1",
packages=find_packages(),
author="Piotr Pieprzycki",
author_email="[email protected]",
Expand Down
9 changes: 9 additions & 0 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ def open(self):
class FakeVyOSDevice(BaseTestDouble):
"""VyOS device test double."""

def __init__(self):
self.mode_config = False

def send_command(self, command, **kwargs):
filename = '{}.text'.format(self.sanitize_text(command))
full_path = self.find_file(filename)
return self.read_txt_file(full_path)

def config_mode(self):
self.mode_config = True

def exit_config_mode(self):
self.mode_config = False
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
interfaces {
ethernet eth0 {
address dhcp
}
ethernet eth1 {
address 10.0.1.222/24
}
loopback lo {
address 10.2.2.2/32
address 8.8.8.8/32
}
}
policy {
prefix-list EXPORT {
rule 1 {
action permit
prefix 172.16.2.0/24
}
rule 65535 {
action permit
prefix 10.2.2.2/32
}
}
route-map EXPORT-POLICY {
rule 1 {
action permit
match {
ip {
address {
prefix-list EXPORT
}
}
}
}
}
}
protocols {
bgp 65002 {
neighbor 10.0.1.100 {
remote-as 65001
route-map {
export EXPORT-POLICY
}
}
redistribute {
connected {
route-map EXPORT-POLICY
}
}
}
}
service {
snmp {
community commro {
authorization ro
}
contact [email protected]
location PL,Krakow
}
ssh {
disable-host-validation
port 22
}
}
system {
config-management {
commit-revisions 20
}
host-name vyos2
login {
banner {
pre-login "My banner for all devices"
}
user vagrant {
authentication {
encrypted-password $6$fcHhBu3T$WLmiu6/txlEfWK5uh4mKE8v7qocuftsoAN1oHqPIIoogXAX8zS.SKhB105EExYU6yBy4cKHUD/Q6Mm7CUbVTr.
plaintext-password ""
public-keys vagrant {
key AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==
type ssh-rsa
}
}
level admin
}
user vyos {
authentication {
encrypted-password $1$yHIMnG/J$aWDkd3oDYSYps8twB5vpw1
plaintext-password ""
}
level admin
}
}
ntp {
server 10.0.1.100 {
}
}
package {
auto-sync 1
repository community {
components main
distribution helium
password ""
url http://packages.vyos.net/vyos
username ""
}
}
syslog {
global {
facility all {
level notice
}
facility protocols {
level debug
}
}
}
time-zone UTC
}


/* Warning: Do not remove the following line. */
/* === vyatta-config-version: "cluster@1:config-management@1:conntrack-sync@1:conntrack@1:cron@1:dhcp-relay@1:dhcp-server@4:firewall@5:ipsec@4:nat@4:qos@1:quagga@2:system@6:vrrp@1:wanloadbalance@3:webgui@1:webproxy@1:zone-policy@1" === */
/* Release version: VyOS 1.1.7 */
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"running": "interfaces {\n ethernet eth0 {\n address dhcp\n duplex auto\n smp_affinity auto\n speed auto\n }\n ethernet eth1 {\n address 10.0.1.222/24\n duplex auto\n smp_affinity auto\n speed auto\n }\n loopback lo {\n address 10.2.2.2/32\n address 8.8.8.8/32\n }\n}\npolicy {\n prefix-list EXPORT {\n rule 1 {\n action permit\n prefix 172.16.2.0/24\n }\n rule 65535 {\n action permit\n prefix 10.2.2.2/32\n }\n }\n route-map EXPORT-POLICY {\n rule 1 {\n action permit\n match {\n ip {\n address {\n prefix-list EXPORT\n }\n }\n }\n }\n }\n}\nprotocols {\n bgp 65002 {\n neighbor 10.0.1.100 {\n remote-as 65001\n route-map {\n export EXPORT-POLICY\n }\n }\n redistribute {\n connected {\n route-map EXPORT-POLICY\n }\n }\n }\n}\nservice {\n snmp {\n community commro {\n authorization ro\n }\n contact [email protected]\n location PL,Krakow\n }\n ssh {\n disable-host-validation\n port 22\n }\n}\nsystem {\n config-management {\n commit-revisions 20\n }\n host-name vyos2\n login {\n banner {\n pre-login \"My banner for all devices\"\n }\n user vagrant {\n authentication {\n encrypted-password $6$fcHhBu3T$WLmiu6/txlEfWK5uh4mKE8v7qocuftsoAN1oHqPIIoogXAX8zS.SKhB105EExYU6yBy4cKHUD/Q6Mm7CUbVTr.\n plaintext-password \"\"\n public-keys vagrant {\n key AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==\n type ssh-rsa\n }\n }\n level admin\n }\n user vyos {\n authentication {\n encrypted-password $1$yHIMnG/J$aWDkd3oDYSYps8twB5vpw1\n plaintext-password \"\"\n }\n level admin\n }\n }\n ntp {\n server 10.0.1.100 {\n }\n }\n package {\n auto-sync 1\n repository community {\n components main\n distribution helium\n password \"\"\n url http://packages.vyos.net/vyos\n username \"\"\n }\n }\n syslog {\n global {\n facility all {\n level notice\n }\n facility protocols {\n level debug\n }\n }\n }\n time-zone UTC\n}", "startup": "interfaces {\n ethernet eth0 {\n address dhcp\n }\n ethernet eth1 {\n address 10.0.1.222/24\n }\n loopback lo {\n address 10.2.2.2/32\n address 8.8.8.8/32\n }\n}\npolicy {\n prefix-list EXPORT {\n rule 1 {\n action permit\n prefix 172.16.2.0/24\n }\n rule 65535 {\n action permit\n prefix 10.2.2.2/32\n }\n }\n route-map EXPORT-POLICY {\n rule 1 {\n action permit\n match {\n ip {\n address {\n prefix-list EXPORT\n }\n }\n }\n }\n }\n}\nprotocols {\n bgp 65002 {\n neighbor 10.0.1.100 {\n remote-as 65001\n route-map {\n export EXPORT-POLICY\n }\n }\n redistribute {\n connected {\n route-map EXPORT-POLICY\n }\n }\n }\n}\nservice {\n snmp {\n community commro {\n authorization ro\n }\n contact [email protected]\n location PL,Krakow\n }\n ssh {\n disable-host-validation\n port 22\n }\n}\nsystem {\n config-management {\n commit-revisions 20\n }\n host-name vyos2\n login {\n banner {\n pre-login \"My banner for all devices\"\n }\n user vagrant {\n authentication {\n encrypted-password $6$fcHhBu3T$WLmiu6/txlEfWK5uh4mKE8v7qocuftsoAN1oHqPIIoogXAX8zS.SKhB105EExYU6yBy4cKHUD/Q6Mm7CUbVTr.\n plaintext-password \"\"\n public-keys vagrant {\n key AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==\n type ssh-rsa\n }\n }\n level admin\n }\n user vyos {\n authentication {\n encrypted-password $1$yHIMnG/J$aWDkd3oDYSYps8twB5vpw1\n plaintext-password \"\"\n }\n level admin\n }\n }\n ntp {\n server 10.0.1.100 {\n }\n }\n package {\n auto-sync 1\n repository community {\n components main\n distribution helium\n password \"\"\n url http://packages.vyos.net/vyos\n username \"\"\n }\n }\n syslog {\n global {\n facility all {\n level notice\n }\n facility protocols {\n level debug\n }\n }\n }\n time-zone UTC\n}\n\n\n/* Warning: Do not remove the following line. */\n/* === vyatta-config-version: \"cluster@1:config-management@1:conntrack-sync@1:conntrack@1:cron@1:dhcp-relay@1:dhcp-server@4:firewall@5:ipsec@4:nat@4:qos@1:quagga@2:system@6:vrrp@1:wanloadbalance@3:webgui@1:webproxy@1:zone-policy@1\" === */\n/* Release version: VyOS 1.1.7 */\n", "candidate": ""}
125 changes: 125 additions & 0 deletions test/unit/mocked_data/test_get_config/normal/show.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
interfaces {
ethernet eth0 {
address dhcp
duplex auto
smp_affinity auto
speed auto
}
ethernet eth1 {
address 10.0.1.222/24
duplex auto
smp_affinity auto
speed auto
}
loopback lo {
address 10.2.2.2/32
address 8.8.8.8/32
}
}
policy {
prefix-list EXPORT {
rule 1 {
action permit
prefix 172.16.2.0/24
}
rule 65535 {
action permit
prefix 10.2.2.2/32
}
}
route-map EXPORT-POLICY {
rule 1 {
action permit
match {
ip {
address {
prefix-list EXPORT
}
}
}
}
}
}
protocols {
bgp 65002 {
neighbor 10.0.1.100 {
remote-as 65001
route-map {
export EXPORT-POLICY
}
}
redistribute {
connected {
route-map EXPORT-POLICY
}
}
}
}
service {
snmp {
community commro {
authorization ro
}
contact [email protected]
location PL,Krakow
}
ssh {
disable-host-validation
port 22
}
}
system {
config-management {
commit-revisions 20
}
host-name vyos2
login {
banner {
pre-login "My banner for all devices"
}
user vagrant {
authentication {
encrypted-password $6$fcHhBu3T$WLmiu6/txlEfWK5uh4mKE8v7qocuftsoAN1oHqPIIoogXAX8zS.SKhB105EExYU6yBy4cKHUD/Q6Mm7CUbVTr.
plaintext-password ""
public-keys vagrant {
key AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==
type ssh-rsa
}
}
level admin
}
user vyos {
authentication {
encrypted-password $1$yHIMnG/J$aWDkd3oDYSYps8twB5vpw1
plaintext-password ""
}
level admin
}
}
ntp {
server 10.0.1.100 {
}
}
package {
auto-sync 1
repository community {
components main
distribution helium
password ""
url http://packages.vyos.net/vyos
username ""
}
}
syslog {
global {
facility all {
level notice
}
facility protocols {
level debug
}
}
}
time-zone UTC
}
[edit]
Loading

0 comments on commit 7201ed0

Please sign in to comment.