From 58227342270f233e4de1a523932e251c41bae23e Mon Sep 17 00:00:00 2001 From: gordon chung Date: Thu, 7 Jan 2016 17:56:11 -0500 Subject: [PATCH] add alarm-history interface - aodh alarm-history show - aodh alarm-history search Change-Id: I5d4c8ad27dd1b09f49e54e0cd090b2f6aaa077fa --- aodhclient/shell.py | 3 + .../tests/functional/test_alarm_history.py | 78 +++++++++++++++++++ aodhclient/v2/alarm_history.py | 40 ++++++++++ aodhclient/v2/alarm_history_cli.py | 51 ++++++++++++ aodhclient/v2/client.py | 2 + 5 files changed, 174 insertions(+) create mode 100644 aodhclient/tests/functional/test_alarm_history.py create mode 100644 aodhclient/v2/alarm_history.py create mode 100644 aodhclient/v2/alarm_history_cli.py diff --git a/aodhclient/shell.py b/aodhclient/shell.py index ea4e7dd..170a9a3 100644 --- a/aodhclient/shell.py +++ b/aodhclient/shell.py @@ -26,6 +26,7 @@ from keystoneauth1 import loading from aodhclient import client from aodhclient import noauth from aodhclient.v2 import alarm_cli +from aodhclient.v2 import alarm_history_cli from aodhclient.v2 import capabilities_cli from aodhclient.version import __version__ @@ -38,6 +39,8 @@ class AodhCommandManager(commandmanager.CommandManager): "alarm show": alarm_cli.CliAlarmShow, "alarm search": alarm_cli.CliAlarmSearch, "alarm update": alarm_cli.CliAlarmUpdate, + "alarm-history show": alarm_history_cli.CliAlarmHistoryShow, + "alarm-history search": alarm_history_cli.CliAlarmHistorySearch, "capabilities list": capabilities_cli.CliCapabilitiesList, } diff --git a/aodhclient/tests/functional/test_alarm_history.py b/aodhclient/tests/functional/test_alarm_history.py new file mode 100644 index 0000000..d3a980c --- /dev/null +++ b/aodhclient/tests/functional/test_alarm_history.py @@ -0,0 +1,78 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import uuid + +from oslo_serialization import jsonutils + +from aodhclient.tests.functional import base + + +class AlarmHistoryTest(base.ClientTestBase): + + def test_help(self): + self.aodh("help", params="alarm-history show") + self.aodh("help", params="alarm-history search") + + def test_alarm_history_scenario(self): + + PROJECT_ID = str(uuid.uuid4()) + + result = self.aodh(u'alarm', + params=(u"create --type threshold --name history1 " + "-m meter_name --threshold 5 " + "--project-id %s" % PROJECT_ID)) + alarm = self.details_multiple(result)[0] + ALARM_ID = alarm['alarm_id'] + result = self.aodh(u'alarm', + params=(u"create --type threshold --name history2 " + "-m meter_name --threshold 10 " + "--project-id %s" % PROJECT_ID)) + alarm = self.details_multiple(result)[0] + ALARM_ID2 = alarm['alarm_id'] + + # SHOW + result = self.aodh( + 'alarm-history', params=("show %s" % ALARM_ID)) + history = self.parser.listing(result)[0] + self.assertEqual('creation', history['type']) + self.assertEqual('history1', + jsonutils.loads(history['detail'])['name']) + + result = self.aodh( + 'alarm-history', params=("show %s" % ALARM_ID2)) + history = self.parser.listing(result)[0] + self.assertEqual('creation', history['type']) + self.assertEqual('history2', + jsonutils.loads(history['detail'])['name']) + + # SEARCH ALL + result = self.aodh('alarm-history', params=("search")) + self.assertIn(ALARM_ID, + [r['alarm_id'] for r in self.parser.listing(result)]) + self.assertIn(ALARM_ID2, + [r['alarm_id'] for r in self.parser.listing(result)]) + + # SEARCH + result = self.aodh('alarm-history', + params=("search --query " + "'{\"=\": {\"alarm_id\": \"%s\"}}'" + % ALARM_ID)) + history = self.parser.listing(result)[0] + self.assertEqual(ALARM_ID, history["alarm_id"]) + self.assertEqual('creation', history['type']) + self.assertEqual('history1', + jsonutils.loads(history['detail'])['name']) + + # CLEANUP + self.aodh('alarm', params="delete %s" % ALARM_ID) + self.aodh('alarm', params="delete %s" % ALARM_ID2) diff --git a/aodhclient/v2/alarm_history.py b/aodhclient/v2/alarm_history.py new file mode 100644 index 0000000..3cdebe6 --- /dev/null +++ b/aodhclient/v2/alarm_history.py @@ -0,0 +1,40 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from oslo_serialization import jsonutils + +from aodhclient.v2 import base + + +class AlarmHistoryManager(base.Manager): + + url = "v2/alarms/%s/history" + + def get(self, alarm_id): + """Get history of an alarm + + :param alarm_id: ID of the alarm + :type alarm_id: str + """ + return self._get(self.url % alarm_id).json() + + def search(self, query=None): + """List of history matching corresponding query + + :param query: The query dictionary + :type query: dict + """ + query = {'filter': query} if query else {} + url = "v2/query/alarms/history" + return self._post(url, headers={'Content-Type': "application/json"}, + data=jsonutils.dumps(query)).json() diff --git a/aodhclient/v2/alarm_history_cli.py b/aodhclient/v2/alarm_history_cli.py new file mode 100644 index 0000000..df9b80c --- /dev/null +++ b/aodhclient/v2/alarm_history_cli.py @@ -0,0 +1,51 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from cliff import lister + +from aodhclient import utils + + +class CliAlarmHistorySearch(lister.Lister): + """Show history for all alarms based on query""" + + COLS = ('alarm_id', 'timestamp', 'type', 'detail') + + def get_parser(self, prog_name): + parser = super(CliAlarmHistorySearch, self).get_parser(prog_name) + parser.add_argument("--query", help="Query"), + return parser + + def take_action(self, parsed_args): + history = self.app.client.alarm_history.search(query=parsed_args.query) + return utils.list2cols(self.COLS, history) + + +def _format_alarm(alarm): + alarm.update(alarm.pop('threshold_rule')) + return alarm + + +class CliAlarmHistoryShow(lister.Lister): + """Show history for an alarm""" + + COLS = ('timestamp', 'type', 'detail') + + def get_parser(self, prog_name): + parser = super(CliAlarmHistoryShow, self).get_parser(prog_name) + parser.add_argument("alarm_id", help="ID of an alarm") + return parser + + def take_action(self, parsed_args): + history = self.app.client.alarm_history.get( + alarm_id=parsed_args.alarm_id) + return utils.list2cols(self.COLS, history) diff --git a/aodhclient/v2/client.py b/aodhclient/v2/client.py index f447a44..8876903 100644 --- a/aodhclient/v2/client.py +++ b/aodhclient/v2/client.py @@ -15,6 +15,7 @@ from aodhclient import client from aodhclient.v2 import alarm +from aodhclient.v2 import alarm_history from aodhclient.v2 import capabilities @@ -30,4 +31,5 @@ class Client(object): self.api = client.SessionClient(session, service_type=service_type, **kwargs) self.alarm = alarm.AlarmManager(self) + self.alarm_history = alarm_history.AlarmHistoryManager(self) self.capabilities = capabilities.CapabilitiesManager(self)