Skip to content

Commit

Permalink
Add detail param to Collection
Browse files Browse the repository at this point in the history
Using detail=True will fetch complete resources with all attributes.
  • Loading branch information
eonpatapon committed Mar 1, 2016
1 parent 0b38d73 commit 273d7f7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
26 changes: 18 additions & 8 deletions contrail_api_cli/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ class Collection(ResourceBase, UserList):
"""

def __init__(self, type, fetch=False, recursive=1,
fields=None, filters=None, parent_uuid=None,
**kwargs):
fields=None, detail=None, filters=None,
parent_uuid=None, **kwargs):
UserList.__init__(self)
self.type = type
self.fields = fields or []
self.filters = filters or []
self.parent_uuid = list(self._sanitize_parent_uuid(parent_uuid))
self.detail = detail
self.meta = dict(kwargs)
if fetch:
self.fetch(recursive=recursive)
Expand Down Expand Up @@ -149,13 +150,16 @@ def filter(self, field_name, field_value):
"""
self.filters.append((field_name, field_value))

def _format_fetch_params(self, fields, filters, parent_uuid):
def _format_fetch_params(self, fields, detail, filters, parent_uuid):
params = {}
detail = detail or self.detail
fields_str = ",".join(self._fetch_fields(fields))
filters_str = ",".join(['%s==%s' % (f, json.dumps(v))
for f, v in self._fetch_filters(filters)])
parent_uuid_str = ",".join(self._fetch_parent_uuid(parent_uuid))
if fields_str:
if detail is True:
params['detail'] = detail
elif fields_str:
params['fields'] = fields_str
if filters_str:
params['filters'] = filters_str
Expand All @@ -173,38 +177,44 @@ def _fetch_filters(self, filters=None):
def _fetch_fields(self, fields=None):
return self.fields + (fields or [])

def fetch(self, recursive=1, fields=None, filters=None, parent_uuid=None):
def fetch(self, recursive=1, fields=None, detail=None, filters=None, parent_uuid=None):
"""
Fetch collection from API server
:param recursive: level of recursion
:type recursive: int
:param fields: list of field names to fetch
:param fields: fetch only listed fields.
contrail 3.0 required
:type fields: [str]
:param detail: fetch all fields
:type detail: bool
:param filters: list of filters
:type filters: [(name, value), ...]
:param parent_uuid: filter by parent_uuid
:type parent_uuid: v4UUID str or list of v4UUID str
"""

params = self._format_fetch_params(fields, filters, parent_uuid)
params = self._format_fetch_params(fields, detail, filters, parent_uuid)
data = self.session.get_json(self.href, **params)

if not self.type:
self.data = [Collection(col["link"]["name"],
fetch=recursive - 1 > 0,
recursive=recursive - 1,
fields=self._fetch_fields(fields),
detail=detail or self.detail,
filters=self._fetch_filters(filters),
parent_uuid=self._fetch_parent_uuid(parent_uuid),
**col["link"])
for col in data['links']
if col["link"]["rel"] == "collection"]
else:
# when detail=False, res == {resource_attrs}
# when detail=True, res == {'type': {resource_attrs}}
self.data = [Resource(self.type,
fetch=recursive - 1 > 0,
recursive=recursive - 1,
**res)
**res.get(self.type, res))
for res_type, res_list in data.items()
for res in res_list]

Expand Down
34 changes: 34 additions & 0 deletions contrail_api_cli/tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,40 @@ def test_collection_filters(self, mock_session):
c.fetch()
mock_session.get_json.assert_called_with(BASE + '/foos', filters='foo=="bar",bar==42')

@mock.patch('contrail_api_cli.resource.ResourceBase.session')
def test_collection_detail(self, mock_session):
mock_session.configure_mock(base_url=BASE)
c = Collection('foo', fields=['foo', 'bar'], detail=True, fetch=True)
mock_session.get_json.assert_called_with(BASE + '/foos', detail=True)
c.fetch(fields=['baz'])
mock_session.get_json.assert_called_with(BASE + '/foos', detail=True)
c.fetch()
mock_session.get_json.assert_called_with(BASE + '/foos', detail=True)
c = Collection('foo', detail=True, fetch=True)
mock_session.get_json.assert_called_with(c.href, detail=True)
c = Collection('foo', detail=False, fetch=True)
mock_session.get_json.assert_called_with(c.href)
c = Collection('foo', detail='other', fetch=True)
mock_session.get_json.assert_called_with(c.href)

mock_session.get_json.return_value = {
'foos': [
{
'foo': {
'uuid': 'dd2f4111-abda-405f-bce9-c6c24181dd14',
}
},
{
'foo': {
'uuid': '9fe7094d-f54e-4284-a813-9ca4df866019',
}
}
]
}
c = Collection('foo', detail=True, fetch=True)
self.assertEqual(c.data[0], Resource('foo', uuid='dd2f4111-abda-405f-bce9-c6c24181dd14'))
self.assertEqual(c.data[1], Resource('foo', uuid='9fe7094d-f54e-4284-a813-9ca4df866019'))

@mock.patch('contrail_api_cli.resource.ResourceBase.session')
def test_collection_parent_uuid(self, mock_session):
mock_session.configure_mock(base_url=BASE)
Expand Down

0 comments on commit 273d7f7

Please sign in to comment.