Split alarm query and alarm list in SDK layer.

As discussed in the review of
https://review.openstack.org/#/c/321394/
split alarm query and list in the
SDK layer will make the code more
easier to understand.

Change-Id: Ic1ec81b1e25f6bf3a7913b68310f2fc853fcc5d5
This commit is contained in:
Kevin_Zheng 2016-06-07 21:00:47 +08:00 committed by Julien Danjou
parent c6b5b0b429
commit 93080b86a1
5 changed files with 42 additions and 17 deletions

View File

@ -47,10 +47,10 @@ class AlarmManagerTest(testtools.TestCase):
mock_am.assert_called_with('v2/alarms') mock_am.assert_called_with('v2/alarms')
@mock.patch.object(alarm.AlarmManager, '_post') @mock.patch.object(alarm.AlarmManager, '_post')
def test_list_with_query(self, mock_am): def test_query(self, mock_am):
am = alarm.AlarmManager(self.client) am = alarm.AlarmManager(self.client)
query = '{"=": {"type": "event"}}' query = '{"=": {"type": "event"}}'
am.list(query) am.query(query)
url = 'v2/query/alarms' url = 'v2/query/alarms'
expected_value = ('{"filter": "{\\"=\\": {\\"type\\":' expected_value = ('{"filter": "{\\"=\\": {\\"type\\":'
' \\"event\\"}}"}') ' \\"event\\"}}"}')

View File

@ -11,6 +11,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from debtcollector import removals
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from aodhclient.v2.alarm_cli import ALARM_TYPES from aodhclient.v2.alarm_cli import ALARM_TYPES
@ -29,30 +30,45 @@ class AlarmManager(base.Manager):
urls.append(url) urls.append(url)
return '&'.join(urls) return '&'.join(urls)
def list(self, query=None, filters=None): @removals.removed_kwarg('query',
message='Calling list() with query parameter'
'is deprecated, and will be removed'
'in python-aodhclient 0.7.0, please '
'use query() instead.')
def list(self, filters=None, query=None):
"""List alarms. """List alarms.
:param query: A json format complex query expression, like this:
'{"=":{"type":"threshold"}}', this expression is used to
query all the threshold type alarms.
:type query: json
:param filters: A dict includes filters parameters, for example, :param filters: A dict includes filters parameters, for example,
{'type': 'threshold', 'severity': 'low'} represent {'type': 'threshold', 'severity': 'low'} represent
filters to query alarms with type='threshold' and filters to query alarms with type='threshold' and
severity='low'. severity='low'.
:type filters: dict :type filters: dict
:param query: A json format complex query expression, like this:
'{"=":{"type":"threshold"}}', this expression is used to
query all the threshold type alarms.
:type query: js
""" """
if query: if query:
query = {'filter': query} return query(query)
url = "v2/query/alarms"
return self._post(url,
headers={'Content-Type': "application/json"},
data=jsonutils.dumps(query)).json()
else: else:
url = (self.url + '?' + self._filtersdict_to_url(filters) if url = (self.url + '?' + self._filtersdict_to_url(filters) if
filters else self.url) filters else self.url)
return self._get(url).json() return self._get(url).json()
def query(self, query=None):
"""Query alarms.
:param query: A json format complex query expression, like this:
'{"=":{"type":"threshold"}}', this expression is used to
query all the threshold type alarms.
:type query: json
"""
query = {'filter': query}
url = "v2/query/alarms"
return self._post(url,
headers={'Content-Type': "application/json"},
data=jsonutils.dumps(query)).json()
def get(self, alarm_id): def get(self, alarm_id):
"""Get an alarm """Get an alarm

View File

@ -67,11 +67,10 @@ class CliAlarmList(lister.Lister):
if parsed_args.query: if parsed_args.query:
query = jsonutils.dumps( query = jsonutils.dumps(
utils.search_query_builder(parsed_args.query)) utils.search_query_builder(parsed_args.query))
alarms = utils.get_client(self).alarm.query(query=query)
else: else:
query = None filters = dict(parsed_args.filter) if parsed_args.filter else None
filters = dict(parsed_args.filter) if parsed_args.filter else None alarms = utils.get_client(self).alarm.list(filters=filters)
alarms = utils.get_client(self).alarm.list(query=query,
filters=filters)
return utils.list2cols(ALARM_LIST_COLS, alarms) return utils.list2cols(ALARM_LIST_COLS, alarms)
@ -100,7 +99,7 @@ def _format_alarm(alarm):
def _find_alarm_by_name(client, name): def _find_alarm_by_name(client, name):
# then try to get entity as name # then try to get entity as name
query = jsonutils.dumps({"=": {"name": name}}) query = jsonutils.dumps({"=": {"name": name}})
alarms = client.alarm.list(query) alarms = client.alarm.query(query)
if len(alarms) > 1: if len(alarms) > 1:
msg = (_("Multiple alarms matches found for '%s', " msg = (_("Multiple alarms matches found for '%s', "
"use an ID to be more specific.") % name) "use an ID to be more specific.") % name)

View File

@ -0,0 +1,9 @@
---
upgrade:
- Alarm list and query has been split into two separate functions
in the SDK layer( CLI user still uses alarm list with --query
or --filter as before).
deprecations:
- Calling alarm list function with query parameter has been deprecated
and will be removed in python-aodhclient 0.7.0, please use alarm query
function instead.

View File

@ -9,3 +9,4 @@ oslo.serialization>=1.4.0 # Apache-2.0
oslo.utils>=2.0.0 # Apache-2.0 oslo.utils>=2.0.0 # Apache-2.0
keystoneauth1>=1.0.0 keystoneauth1>=1.0.0
six six
debtcollector