From 39f634039d00155e9b60aac16be5a5de204271d6 Mon Sep 17 00:00:00 2001 From: Andy Balogh Date: Fri, 7 Aug 2020 09:26:20 -0700 Subject: [PATCH] "version 1.0.56" --- RELEASENOTES.md | 5 +++ .../assistants/sessions/sessionassistant.py | 2 +- ixnetwork_restpy/base.py | 36 +++++++++++++++---- .../pytest_tests/tests/test_errors.py | 18 +++++----- ixnetwork_restpy/select.py | 1 - version.txt | 2 +- 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index e42952552..3d816635b 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -1,5 +1,10 @@ # Release Notes +### Aug 2020 +* 1.0.56 + * fixed intermittent KeyError: 'href' bug + * enhanced KeyError message when error is valid + ### Jul 2020 * 1.0.55 * PortMapAssistant.Connect method bug fix diff --git a/ixnetwork_restpy/assistants/sessions/sessionassistant.py b/ixnetwork_restpy/assistants/sessions/sessionassistant.py index ad0c0edb1..9d98ff1db 100644 --- a/ixnetwork_restpy/assistants/sessions/sessionassistant.py +++ b/ixnetwork_restpy/assistants/sessions/sessionassistant.py @@ -76,7 +76,7 @@ def __init__(self, IpAddress='127.0.0.1', RestPort=11009, UserName='admin', Pass if SessionId is not None: session = testplatform.Sessions.find(Id=SessionId) if len(session) == 0: - raise NotFoundError('Session %s does not exist on %s:%s', (SessionId, testplatform.Hostname, testplatform.RestPort)) + raise NotFoundError('Session %s does not exist on %s:%s' % (SessionId, testplatform.Hostname, testplatform.RestPort)) session.Name = SessionName elif SessionName is not None: session = testplatform.Sessions.find(Name=SessionName) diff --git a/ixnetwork_restpy/base.py b/ixnetwork_restpy/base.py index 180188621..3daffea71 100644 --- a/ixnetwork_restpy/base.py +++ b/ixnetwork_restpy/base.py @@ -135,7 +135,12 @@ def _get_attribute(self, name): try: return self._properties[name] except Exception as e: - raise NotFoundError('The attribute %s is not in the internal list of object dicts. (%s)' % (name, e)) + msg = """The attribute %s is not in the internal list of object dicts. + If there is a find method execute that prior to executing a property accessor. + Check the number of encapsulated resources using the len method. + %s + """ % (name, e) + raise NotFoundError(msg) def _set_attribute(self, name, value): """Update a property on the server and save it locally if there is no exception @@ -155,7 +160,7 @@ def __str__(self): instances = '' for i in range(len(self)): properties = self._object_properties[i] - instances += '\n%s[%s]: %s' % (self.__class__.__name__, i, properties['href']) + instances += '\n%s[%s]: %s' % (self.__class__.__name__, i, self.href) for key in sorted(properties.keys()): if key == 'href': continue @@ -273,8 +278,7 @@ def _set_properties(self, properties, clear=False): def _update(self, locals_dict): payload = self._build_payload(locals_dict) if payload is not None: - self._connection._update(self._properties['href'], payload) - return self.refresh() + self._connection._update(self.href, payload) return self def _delete(self): @@ -287,7 +291,7 @@ def _delete(self): raise e def _execute(self, operation, child=None, payload=None, response_object=None): - url = self._properties['href'] + url = self.href if child is not None: url = '%s/%s' % (url, child) if operation is not None: @@ -322,7 +326,7 @@ def refresh(self): for properties in self._object_properties: selects.append( { - 'from': properties['href'], + 'from': self.href, 'properties': ['*'], 'children': [], 'inlines': [] @@ -346,6 +350,21 @@ def _read(self, href): self._set_properties(response, clear=True) return self + def __is_key_value_in_response(self, key_value, response): + """Return whether or not a key is in a response + """ + if isinstance(response, list): + for item in response: + if self.__is_key_value_in_response(key_value, item) is True: + return True + elif isinstance(response, dict): + if key_value in response.keys(): + return True + for value in response.values(): + if self.__is_key_value_in_response(key_value, value) is True: + return True + return False + def _select(self, locals_dict=dict()): selects = [] for parent in self._parent: @@ -377,7 +396,10 @@ def _select(self, locals_dict=dict()): if 'ixnetwork' in self._parent.href: end = self._parent.href.index('ixnetwork') + len('ixnetwork') url = '%s/operations/select' % self._parent.href[0:end] - responses = self._connection._execute(url, payload) + while True: + responses = self._connection._execute(url, payload) + if self.__is_key_value_in_response('objRef', responses) is False: + break self._set_properties(None, clear=True) # process children of from for response in responses: diff --git a/ixnetwork_restpy/pytest_tests/tests/test_errors.py b/ixnetwork_restpy/pytest_tests/tests/test_errors.py index 030e05ffa..7062e3423 100644 --- a/ixnetwork_restpy/pytest_tests/tests/test_errors.py +++ b/ixnetwork_restpy/pytest_tests/tests/test_errors.py @@ -1,16 +1,16 @@ from ixnetwork_restpy.errors import * -def test_operation_returns_400_error (ixnetwork): +def test_operation_improper_name (ixnetwork): try: - ixnetwork.ConnectCardById('2323232') - raise Exception('Error should have been raised') - except Exception as e: - print(e) + ixnetwork.NewConfig1() + assert Exception('Error should have been raised') + except AttributeError as e: + pass def test_operation_returns_404_error (ixnetwork): try: - ixnetwork.ConnectCardById('2323232', 'asdf') - raise Exception('Error should have been raised') - except Exception as e: - print(e) + ixnetwork.LoadConfig('arg1', 'arg2', 'arg3') + assert Exception('Error should have been raised') + except NotFoundError as e: + pass diff --git a/ixnetwork_restpy/select.py b/ixnetwork_restpy/select.py index 3cda15e72..27c080d02 100644 --- a/ixnetwork_restpy/select.py +++ b/ixnetwork_restpy/select.py @@ -19,7 +19,6 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -from ixnetwork_restpy.base import Base class Select(object): diff --git a/version.txt b/version.txt index 61098d267..3c79fcb59 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.55 \ No newline at end of file +1.0.56 \ No newline at end of file