Skip to content

Commit

Permalink
Merge pull request #250 from aristanetworks/release-1.3.1
Browse files Browse the repository at this point in the history
Release 1.3.1
  • Loading branch information
mharista authored Apr 13, 2023
2 parents 39c258f + c739025 commit 49fbb4a
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
1.3.1
2 changes: 1 addition & 1 deletion cvprac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
''' RESTful API Client class for Cloudvision(R) Portal
'''

__version__ = '1.3.0'
__version__ = '1.3.1'
__author__ = 'Arista Networks, Inc.'
56 changes: 54 additions & 2 deletions cvprac/cvp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# pylint: disable=redefined-builtin
from io import open
from datetime import datetime
from re import split

from cvprac.cvp_client_errors import CvpApiError

Expand Down Expand Up @@ -1177,6 +1178,7 @@ def add_configlet(self, name, config):
% qplus(name), timeout=self.request_timeout)
return data['key']


def delete_configlet(self, name, key):
''' Delete the configlet.
Expand Down Expand Up @@ -1308,6 +1310,51 @@ def add_note_to_configlet(self, key, note):
return self.clnt.post('/configlet/addNoteToConfiglet.do',
data=data, timeout=self.request_timeout)

def sanitize_warnings(self, data):
''' Sanitize the warnings returned after validation.
In some cases where the configlets has both errors
and warnings, CVP may split any warnings that have
`,` across multiple strings.
This method concats the strings back into one string
per warning, and correct the warningCount.
Args:
data (dict): A dict that contians the result
of the validation operation
Returns:
response (dict): A dict that contains the result of the
validation operation
'''
if "warnings" not in data:
# nothing to do here, we can return as is
return data
# Since there may be warnings incorrectly split on
# ', ' within the warning text by CVP, we join all the
# warnings together using ', ' into one large string
temp_warnings = ", ".join(data['warnings']).strip()

# To split the large string again we match on the
# 'at line XXX' that should indicate the end of the warning.
# We capture as well the remaining \\n or whitespace and include
# the extra ', ' added in the previous step in the matching criteria.
# The extra ', ' is not included in the strings of the new list
temp_warnings = split(
r'(.*?at line \d+.*?),\s+',
temp_warnings
)

# The behaviour of re.split will add empty strings
# if the regex matches on the begging or ending of the line.
# Refer to https://docs.python.org/3/library/re.html#re.split

# Use filter to remove any empty strings
# that re.split inserted
data['warnings'] = list(filter(None, temp_warnings))
# Update the count of warnings to the correct value
data['warningCount'] = len(data['warnings'])
return data

def validate_config_for_device(self, device_mac, config):
''' Validate a config against a device
Expand All @@ -1322,8 +1369,13 @@ def validate_config_for_device(self, device_mac, config):
self.log.debug('validate_config_for_device: device_mac: %s config: %s'
% (device_mac, config))
body = {'netElementId': device_mac, 'config': config}
return self.clnt.post('/configlet/validateConfig.do', data=body,
timeout=self.request_timeout)
return self.sanitize_warnings(
self.clnt.post(
'/configlet/validateConfig.do',
data=body,
timeout=self.request_timeout
)
)

def validate_config(self, device_mac, config):
''' Validate a config against a device and parse response to
Expand Down
10 changes: 10 additions & 0 deletions docs/release-notes-1.3.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
######
v1.3.1
######

2023-4-12

Fixed
^^^^^

* Add workaround for issue in CVP API validate config. (`248 <https://github.com/aristanetworks/cvprac/pull/248>`_) [`chetryan <https://github.com/chetryan>`_]
48 changes: 45 additions & 3 deletions test/system/test_cvp_client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import time
import unittest
import uuid
from pkg_resources import parse_version
from pprint import pprint
import urllib3
from test_cvp_base import TestCvpClientBase
Expand Down Expand Up @@ -690,13 +691,48 @@ def test_api_validate_config(self):
result = self.api.validate_config(self.device['key'], config)
self.assertEqual(result, True)

def test_api_validate_config_warn(self):
''' Verify config with only warnings returns True
'''
config = 'interface ethernet1\n description test\nspanning-tree portfast'
result = self.api.validate_config(self.device['key'], config)
self.assertEqual(result, True)

def test_api_validate_config_error(self):
''' Verify an invalid config returns False
'''
config = 'interface ethernet1\n typocommand test'
result = self.api.validate_config(self.device['key'], config)
self.assertEqual(result, False)

def test_api_validate_config_device(self):
''' Verify config with only warnings returns True
'''
# The current APIVersions do not allow the cut off at CVP 2021.1.X
# that this error message changes. So this is version comparision and
# parsing is repetitive.
if not self.clnt.apiversion:
self.api.get_cvp_info()
version_components = self.clnt.version.split(".")
if len(version_components) < 3:
version_components.append("0")
self.log.info('Version found with less than 3 components.'
' Appending 0. Updated Version String - %s',
".".join(version_components))
full_version = ".".join(version_components)

config = 'interface ethernet1\n description test\nspanning-tree portfast\n!\nruter bgp something'
result = self.api.validate_config_for_device(self.device['key'], config)
expected_warning = "! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 3"
self.assertTrue(expected_warning in result["warnings"][0])
expected_error = "> ruter bgp something% Invalid input "
# Error message format changes as of 2021.1.0 or 2021.1.1
if parse_version(full_version) >= parse_version("2021.1.0"):
expected_error += "(at token 0: 'ruter') "
expected_error += "at line 5"
self.assertEqual(result['errors'][0]["error"], expected_error)
self.assertEqual(result['warningCount'], 1)

def test_api_get_task_by_id_bad(self):
''' Verify get_task_by_id with bad task id
'''
Expand Down Expand Up @@ -789,9 +825,15 @@ def test_api_get_configlet_builder(self):
'SYS_TelemetryBuilderV4')
except CvpApiError as e:
if 'Entity does not exist' in e.msg:
# Configlet Builder for 2022.2.0 +
cfglt = self.api.get_configlet_by_name(
'SYS_TelemetryBuilderV5')
# Configlet Builder for 2022.2.0 - 2022.3.X
try:
cfglt = self.api.get_configlet_by_name(
'SYS_TelemetryBuilderV5')
except CvpApiError as e:
if 'Entity does not exist' in e.msg:
# Configlet Builder for 2022.3.X+
cfglt = self.api.get_configlet_by_name(
'SYS_TelemetryBuilderV6')
else:
raise
else:
Expand Down
129 changes: 129 additions & 0 deletions test/unit/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# pylint: disable=wrong-import-position
#
# Copyright (c) 2017, Arista Networks, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of Arista Networks nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

""" Unit tests for the CvpAPI class
"""
import unittest
from itertools import cycle
from cvprac.cvp_client import CvpClient
from cvprac.cvp_api import CvpApi


class TestAPI(unittest.TestCase):
"""Unit test cases for CvpAPI"""

# pylint: disable=protected-access
# pylint: disable=invalid-name
# pylint: disable=too-many-statements

def setUp(self):
"""Setup for CvpAPI unittests"""
self.clnt = CvpClient()
nodes = ["1.1.1.1"]
self.clnt.nodes = nodes
self.clnt.node_cnt = len(nodes)
self.clnt.node_pool = cycle(nodes)
self.api = CvpApi(self.clnt)

def test_sanitize_warnings(self):
"""Test sanitization if warnings are split"""
input = {
"warnings": [
"! Change will take effect only after switch reboot at line 11\\n\\n",
"! \\nWARNING!\\nChanging TCAM profile will cause forwarding agent(s) to exit and restart.\\nAll traffic through the forwarding chip managed by the restarting\\nforwarding agent will be dropped.\\n at line 392",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs",
"concentrators",
"switches",
"bridges",
"etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 2\\n\\n",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs",
"concentrators",
"switches",
"bridges",
"etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 4\\n",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 6\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n",
"! Interface does not exist. The configuration will not take effect until the module is inserted. at line 2799\\n\\n\\n\\n\\n\\n",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 1247\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n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> ruter bgp 1512% Invalid input (at token 0: 'ruter') at line 6",
}
],
"errorCount": 1,
}
expected = {
"warnings": [
"! Change will take effect only after switch reboot at line 11\\n\\n",
"! \\nWARNING!\\nChanging TCAM profile will cause forwarding agent(s) to exit and restart.\\nAll traffic through the forwarding chip managed by the restarting\\nforwarding agent will be dropped.\\n at line 392",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 2\\n\\n",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 4\\n",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 6\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n",
"! Interface does not exist. The configuration will not take effect until the module is inserted. at line 2799\\n\\n\\n\\n\\n\\n",
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 1247\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\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\\n\\n\\n\\n\\n\\n\\n\\n\\n"
],
"warningCount": 7,
"errors": [
{
"lineNo": " 6",
"error": "> ruter bgp 1512% Invalid input (at token 0: 'ruter') at line 6",
}
],
"errorCount": 1,
}
assert self.api.sanitize_warnings(input) == expected

def test_sanitize_warnings_skip(self):
"""Test sanitization if no warnings need changing"""
input = {
"result": [
{
"output": "enter input line by line; when done enter one or more control-d\n\n> spanning-tree portfast\n! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 2\nCopy completed successfully.\n",
"messages": ["Copy completed successfully."],
},
{
"output": "! Command: show session-configuration named capiVerify-2002-f8a137cac96e11ed89be020000000000\n! device: tp-avd-leaf2 (vEOS-lab, EOS-4.29.1F)\n!\n! boot system flash:/vEOS-lab-4.29.1F.swi\n!\nno aaa root\n!\ntransceiver qsfp default-mode 4x10G\n!\nservice routing protocols model ribd\n!\nspanning-tree mode mstp\n!\ninterface Ethernet1\n spanning-tree portfast\n!\ninterface Ethernet2\n!\ninterface Ethernet3\n!\ninterface Ethernet4\n!\ninterface Ethernet5\n!\ninterface Management1\n!\nno ip routing\n!\nend\n"
},
],
"warnings": [
"! portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc. to this interface when portfast is enabled can cause temporary bridging loops. Use with CAUTION. at line 2"
],
"id": "Arista-3-4826123409839743",
"warningCount": 1,
"jsonrpc": "2.0",
}
# The result should not change
assert self.api.sanitize_warnings(input) == input

0 comments on commit 49fbb4a

Please sign in to comment.