NSO: handle types for leaf-lists in nso_config (ValueBuilder) (#34986)

Fixes: #34985
This commit is contained in:
Claes Nästén 2018-01-24 12:21:17 +01:00 committed by John R Barker
parent b7f815ba89
commit 0582521f96
2 changed files with 64 additions and 9 deletions

View file

@ -446,14 +446,20 @@ class ValueBuilder(object):
schema = self._find_child(parent_path, parent_schema, key) schema = self._find_child(parent_path, parent_schema, key)
if self._is_leaf(schema): if self._is_leaf(schema):
path_type = schema['type'] def get_type(meta, curr_type):
if path_type.get('primitive', False): if curr_type.get('primitive', False):
return path_type['name'] return curr_type['name']
else: if 'namespace' in curr_type:
path_type_key = '{0}:{1}'.format( curr_type_key = '{0}:{1}'.format(
path_type['namespace'], path_type['name']) curr_type['namespace'], curr_type['name'])
type_info = meta['types'][path_type_key] type_info = meta['types'][curr_type_key][-1]
return type_info[-1]['name'] 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 return None
def _ensure_schema_cached(self, path): def _ensure_schema_cached(self, path):

View file

@ -152,7 +152,25 @@ SCHEMA_DATA = {
''', ''',
'/test:test': ''' '/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": { "data": {
"kind": "list", "kind": "list",
"name":"test", "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)) 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): class TestVerifyVersion(unittest.TestCase):
def test_valid_versions(self): def test_valid_versions(self):