Commit c4d180bc by Jenkins Committed by Gerrit Code Review

Merge "Support filter while listing composed nodes"

parents dbc1487a 2867117b
......@@ -25,8 +25,10 @@ from valence.validation import validator
class Nodes(Resource):
def get(self):
filter_args = request.args
return utils.make_response(
http_client.OK, nodes.Node.list_composed_nodes())
http_client.OK,
nodes.Node.list_composed_nodes(filter_args.to_dict()))
@validator.check_input('compose_node_schema')
def post(self):
......
......@@ -182,13 +182,13 @@ class Node(object):
return message
@classmethod
def list_composed_nodes(cls):
def list_composed_nodes(cls, filters={}):
"""List all composed node
return: brief info of all composed node
"""
return [cls._show_node_brief_info(node_info.as_dict())
for node_info in db_api.Connection.list_composed_nodes()]
return [cls._show_node_brief_info(node.as_dict())
for node in db_api.Connection.list_composed_nodes(filters)]
def node_action(self, node_uuid, request_body):
"""Post action to a composed node
......
......@@ -153,9 +153,9 @@ class Connection(object):
return cls.dbdriver.update_composed_node(composed_node_uuid, values)
@classmethod
def list_composed_nodes(cls):
def list_composed_nodes(cls, filters={}):
"""Get a list of all composed nodes.
:returns: A list of all composed node.
"""
return cls.dbdriver.list_composed_nodes()
return cls.dbdriver.list_composed_nodes(filters)
......@@ -180,9 +180,13 @@ class EtcdDriver(object):
return composed_node
def list_composed_nodes(self):
# TODO(lin.a.yang): support filter for listing composed_node
def list_composed_nodes(self, filters={}):
"""List composed nodes from DB
:param filters: filters to be applied on results.
Eg: Filter results based on podm_id {'podm_id': 'xxxx'}
:returns: List of composed nodes after filters applied if any
"""
try:
resp = getattr(self.client.read(models.ComposedNode.path),
'children', None)
......@@ -197,5 +201,9 @@ class EtcdDriver(object):
if node.value is not None:
composed_nodes.append(translate_to_models(
node, models.ComposedNode.path))
if filters:
# Filter nodes having value specified w.r.t key
for key, value in filters.items():
composed_nodes = [node for node in composed_nodes
if node[key] == value]
return composed_nodes
......@@ -11,6 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
import json
import unittest
......@@ -225,6 +226,27 @@ class TestDBAPI(unittest.TestCase):
mock_etcd_read.assert_called_once_with(
'/nodes/' + node['uuid'])
@mock.patch('etcd.Client.read')
def test_list_composed_nodes(self, mock_etcd_read):
node = utils.get_test_composed_node_db_info()
node2 = copy.deepcopy(node)
node2['uuid'] = '5a8e2a25-2901-438d-8157-de7ffd68d05'
node2['podm_id'] = 'da8e2a25-2901-438d-8157-de7ffd685'
mock_etcd_read.return_value = utils.get_etcd_read_list(
'/nodes', json.dumps(node), json.dumps(node2))
# check without filters
result = db_api.Connection.list_composed_nodes()
result = [val.as_dict() for val in result]
self.assertEqual(result, [node, node2])
# check with filters
filters = {'podm_id': 'da8e2a25-2901-438d-8157-de7ffd685'}
result = db_api.Connection.list_composed_nodes(filters)
result = [val.as_dict() for val in result]
self.assertEqual(result, [node2])
@mock.patch('etcd.Client.delete')
@mock.patch('etcd.Client.read')
def test_delete_composed_node(self, mock_etcd_read, mock_etcd_delete):
......
......@@ -14,16 +14,35 @@
import etcd
def get_etcd_read_result(key, value):
"""Return EtcdResult object for read regular key"""
def _get_etcd_data(key):
data = {
u'action': u'get',
u'node': {
u'modifiedIndex': 190,
u'key': key,
u'value': value
}
}
return data
def get_etcd_read_result(key, value):
"""Return EtcdResult object for read regular key"""
data = _get_etcd_data(key)
data['node']['value'] = value
return etcd.EtcdResult(**data)
def get_etcd_read_list(path, *args):
"""Return EtcdResult object for read list"""
values = []
for val in args:
node = {u'key': None, u'value': val}
values.append(node)
data = _get_etcd_data(path)
data['node']['dir'] = 'true'
data['node']['nodes'] = values
return etcd.EtcdResult(**data)
......@@ -85,6 +104,7 @@ def get_test_flavor(**kwargs):
def get_test_composed_node_db_info(**kwargs):
return {
'uuid': kwargs.get('uuid', 'ea8e2a25-2901-438d-8157-de7ffd68d051'),
'podm_id': kwargs.get('podm_id', 'fa8e2a25-2901-438d-8157-de7ffd68d'),
'name': kwargs.get('name', 'fake_name'),
'index': kwargs.get('index', '1'),
'resource_uri': kwargs.get(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment