From 0582521f9624aae5522ea726b62ee2ce54b7bdf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claes=20N=C3=A4st=C3=A9n?= Date: Wed, 24 Jan 2018 12:21:17 +0100 Subject: [PATCH] NSO: handle types for leaf-lists in nso_config (ValueBuilder) (#34986) Fixes: #34985 --- lib/ansible/module_utils/network/nso/nso.py | 22 +++++--- .../module_utils/network/nso/test_nso.py | 51 ++++++++++++++++++- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/lib/ansible/module_utils/network/nso/nso.py b/lib/ansible/module_utils/network/nso/nso.py index f4267b8cab2..bdf62cc734c 100644 --- a/lib/ansible/module_utils/network/nso/nso.py +++ b/lib/ansible/module_utils/network/nso/nso.py @@ -446,14 +446,20 @@ class ValueBuilder(object): schema = self._find_child(parent_path, parent_schema, key) if self._is_leaf(schema): - path_type = schema['type'] - if path_type.get('primitive', False): - return path_type['name'] - else: - path_type_key = '{0}:{1}'.format( - path_type['namespace'], path_type['name']) - type_info = meta['types'][path_type_key] - return type_info[-1]['name'] + def get_type(meta, curr_type): + if curr_type.get('primitive', False): + return curr_type['name'] + if 'namespace' in curr_type: + curr_type_key = '{0}:{1}'.format( + curr_type['namespace'], curr_type['name']) + type_info = meta['types'][curr_type_key][-1] + return get_type(meta, type_info) + if 'leaf_type' in curr_type: + return get_type(meta, curr_type['leaf_type'][-1]) + return curr_type['name'] + + return get_type(meta, schema['type']) + return None def _ensure_schema_cached(self, path): diff --git a/test/units/module_utils/network/nso/test_nso.py b/test/units/module_utils/network/nso/test_nso.py index 38fed66b23b..b05d7a8b1f8 100644 --- a/test/units/module_utils/network/nso/test_nso.py +++ b/test/units/module_utils/network/nso/test_nso.py @@ -152,7 +152,25 @@ SCHEMA_DATA = { ''', '/test:test': ''' { - "meta": {}, + "meta": { + "types": { + "http://example.com/test:t15": [ + { + "leaf_type":[ + { + "name":"string" + } + ], + "list_type":[ + { + "name":"http://example.com/test:t15", + "leaf-list":true + } + ] + } + ] + } + }, "data": { "kind": "list", "name":"test", @@ -206,6 +224,15 @@ SCHEMA_DATA = { ] } ] + }, + { + "kind":"leaf-list", + "name":"device-list", + "qname":"test:device-list", + "type": { + "namespace":"http://example.com/test", + "name":"t15" + } } ] } @@ -365,6 +392,28 @@ class TestValueBuilder(unittest.TestCase): self.assertEqual(0, len(calls)) + @patch('ansible.module_utils.network.nso.nso.open_url') + def test_leaf_list_type(self, open_url_mock): + calls = [ + MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'), + get_schema_response('/test:test') + ] + open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs) + + parent = "/test:test" + schema_data = json.loads( + SCHEMA_DATA['/test:test']) + schema = schema_data['data'] + + vb = nso.ValueBuilder(nso.JsonRpc('http://localhost:8080/jsonrpc')) + vb.build(parent, None, {'device-list': ['one', 'two']}, schema) + self.assertEquals(1, len(vb.values)) + value = vb.values[0] + self.assertEquals('{0}/device-list'.format(parent), value.path) + self.assertEquals(['one', 'two'], value.value) + + self.assertEqual(0, len(calls)) + class TestVerifyVersion(unittest.TestCase): def test_valid_versions(self):