Api for getting a resource id by the type & tag
Change-Id: If05390d3b58b84290e1f306f03c5ba3654bd1fad
This commit is contained in:
parent
4a4493e98a
commit
eff49e1899
|
@ -979,3 +979,37 @@ class TestNsxSearch(nsxlib_testcase.NsxClientTestCase):
|
|||
mock.call('search?query=%s' % query),
|
||||
mock.call('search?query=%s&cursor=2' % query)])
|
||||
self.assertEqual(3, len(results))
|
||||
|
||||
def test_get_id_by_resource_and_tag(self):
|
||||
id = 'test'
|
||||
scope = 'user'
|
||||
tag = 'k8s'
|
||||
res_type = 'LogicalPort'
|
||||
results = {'result_count': 1, 'results': [{'id': id}]}
|
||||
with mock.patch.object(self.nsxlib.client, 'url_get',
|
||||
return_value=results):
|
||||
actual_id = self.nsxlib.get_id_by_resource_and_tag(
|
||||
res_type, scope, tag)
|
||||
self.assertEqual(id, actual_id)
|
||||
|
||||
def test_get_id_by_resource_and_tag_not_found(self):
|
||||
scope = 'user'
|
||||
tag = 'k8s'
|
||||
res_type = 'LogicalPort'
|
||||
results = {'result_count': 0, 'results': []}
|
||||
with mock.patch.object(self.nsxlib.client, 'url_get',
|
||||
return_value=results):
|
||||
self.assertRaises(exceptions.ResourceNotFound,
|
||||
self.nsxlib.get_id_by_resource_and_tag,
|
||||
res_type, scope, tag, alert_not_found=True)
|
||||
|
||||
def test_get_id_by_resource_and_tag_multiple(self):
|
||||
scope = 'user'
|
||||
tag = 'k8s'
|
||||
res_type = 'LogicalPort'
|
||||
results = {'result_count': 2, 'results': [{'id': '1'}, {'id': '2'}]}
|
||||
with mock.patch.object(self.nsxlib.client, 'url_get',
|
||||
return_value=results):
|
||||
self.assertRaises(exceptions.ManagerError,
|
||||
self.nsxlib.get_id_by_resource_and_tag,
|
||||
res_type, scope, tag, alert_multiple=True)
|
||||
|
|
|
@ -147,6 +147,43 @@ class NsxLibBase(object):
|
|||
if cursor >= result_count:
|
||||
return results
|
||||
|
||||
def get_id_by_resource_and_tag(self, resource_type, scope, tag,
|
||||
alert_not_found=False,
|
||||
alert_multiple=False):
|
||||
"""Search a resource type by 1 scope&tag.
|
||||
|
||||
Return the id of the result only if it is single.
|
||||
"""
|
||||
query_tags = [{'scope': utils.escape_tag_data(scope),
|
||||
'tag': utils.escape_tag_data(tag)}]
|
||||
query_result = self.search_by_tags(
|
||||
tags=query_tags, resource_type=resource_type)
|
||||
if not query_result['result_count']:
|
||||
if alert_not_found:
|
||||
msg = _("No %(type)s found for tag '%(scope)s:%(tag)s'") % {
|
||||
'type': resource_type,
|
||||
'scope': scope,
|
||||
'tag': tag}
|
||||
LOG.warning(msg)
|
||||
raise exceptions.ResourceNotFound(
|
||||
manager=self.nsxlib_config.nsx_api_managers,
|
||||
operation=msg)
|
||||
elif query_result['result_count'] == 1:
|
||||
return query_result['results'][0]['id']
|
||||
else:
|
||||
# multiple results
|
||||
if alert_multiple:
|
||||
msg = _("Multiple %(type)s found for tag '%(scope)s:"
|
||||
"%(tag)s'") % {
|
||||
'type': resource_type,
|
||||
'scope': scope,
|
||||
'tag': tag}
|
||||
LOG.warning(msg)
|
||||
raise exceptions.ManagerError(
|
||||
manager=self.nsxlib_config.nsx_api_managers,
|
||||
operation=msg,
|
||||
details='')
|
||||
|
||||
def _build_query(self, tags):
|
||||
try:
|
||||
return " AND ".join(['tags.scope:%(scope)s AND '
|
||||
|
|
|
@ -134,6 +134,13 @@ def build_extra_args(body, extra_args, **kwargs):
|
|||
return body
|
||||
|
||||
|
||||
def escape_tag_data(data):
|
||||
# ElasticSearch query_string requires slashes and dashes to
|
||||
# be escaped. We assume no other reserved characters will be
|
||||
# used in tag scopes or values
|
||||
return data.replace('/', '\\/').replace('-', '\\-')
|
||||
|
||||
|
||||
class NsxLibApiBase(object):
|
||||
"""Base class for nsxlib api """
|
||||
def __init__(self, client, nsxlib_config=None, nsxlib=None):
|
||||
|
|
Loading…
Reference in New Issue