From c0ea892e87d449699a8d9d62c1ea6120a7f9b65f Mon Sep 17 00:00:00 2001 From: Rohit Jaiswal Date: Thu, 9 Jul 2015 20:04:45 +0000 Subject: [PATCH] Fixes querying alarm history with severity field Querying alarm-history with the severity field fails with a 500 error as the storage model has no severity field. This fix adds the severity field to AlarmHistory table and model and allows search on severity field. Change-Id: I4a374d324c7e1bde566e26daf7092fd325f33625 Closes-Bug: 1473046 --- aodh/api/controllers/v2/alarms.py | 8 +++-- ...07adac380_add_severity_to_alarm_history.py | 36 +++++++++++++++++++ aodh/storage/sqlalchemy/models.py | 1 + aodh/tests/api/v2/test_alarm_scenarios.py | 9 +++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 aodh/storage/sqlalchemy/alembic/versions/bb07adac380_add_severity_to_alarm_history.py diff --git a/aodh/api/controllers/v2/alarms.py b/aodh/api/controllers/v2/alarms.py index ff9ad3a4..3bb79329 100644 --- a/aodh/api/controllers/v2/alarms.py +++ b/aodh/api/controllers/v2/alarms.py @@ -525,6 +525,7 @@ class AlarmController(rest.RestController): user_id = pecan.request.headers.get('X-User-Id') project_id = pecan.request.headers.get('X-Project-Id') on_behalf_of = on_behalf_of or project_id + severity = scrubbed_data.get('severity') payload = dict(event_id=str(uuid.uuid4()), alarm_id=self._id, type=type, @@ -532,7 +533,8 @@ class AlarmController(rest.RestController): user_id=user_id, project_id=project_id, on_behalf_of=on_behalf_of, - timestamp=now) + timestamp=now, + severity=severity) try: self.conn.record_alarm_change(payload) @@ -702,6 +704,7 @@ class AlarmsController(rest.RestController): detail = json.dumps(scrubbed_data) user_id = pecan.request.headers.get('X-User-Id') project_id = pecan.request.headers.get('X-Project-Id') + severity = scrubbed_data.get('severity') payload = dict(event_id=str(uuid.uuid4()), alarm_id=alarm_id, type=type, @@ -709,7 +712,8 @@ class AlarmsController(rest.RestController): user_id=user_id, project_id=project_id, on_behalf_of=project_id, - timestamp=now) + timestamp=now, + severity=severity) try: conn.record_alarm_change(payload) diff --git a/aodh/storage/sqlalchemy/alembic/versions/bb07adac380_add_severity_to_alarm_history.py b/aodh/storage/sqlalchemy/alembic/versions/bb07adac380_add_severity_to_alarm_history.py new file mode 100644 index 00000000..756fb615 --- /dev/null +++ b/aodh/storage/sqlalchemy/alembic/versions/bb07adac380_add_severity_to_alarm_history.py @@ -0,0 +1,36 @@ +# Copyright 2015 OpenStack Foundation +# +# 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. +# + +"""add severity to alarm history + +Revision ID: bb07adac380 +Revises: 12fe8fac9fe4 +Create Date: 2015-08-06 15:15:43.717068 + +""" + +# revision identifiers, used by Alembic. +revision = 'bb07adac380' +down_revision = '12fe8fac9fe4' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.add_column('alarm_history', + sa.Column('severity', sa.String(length=50), nullable=True)) diff --git a/aodh/storage/sqlalchemy/models.py b/aodh/storage/sqlalchemy/models.py index 5ec4dd84..b8b5b72b 100644 --- a/aodh/storage/sqlalchemy/models.py +++ b/aodh/storage/sqlalchemy/models.py @@ -143,3 +143,4 @@ class AlarmChange(Base): type = Column(String(20)) detail = Column(Text) timestamp = Column(PreciseTimestamp, default=lambda: timeutils.utcnow()) + severity = Column(String(50)) diff --git a/aodh/tests/api/v2/test_alarm_scenarios.py b/aodh/tests/api/v2/test_alarm_scenarios.py index f5b0ed06..633aefc1 100644 --- a/aodh/tests/api/v2/test_alarm_scenarios.py +++ b/aodh/tests/api/v2/test_alarm_scenarios.py @@ -1992,6 +1992,15 @@ class TestAlarmsHistory(TestAlarmsBase): self.assertEqual(msg, resp.json['error_message']['faultstring']) + def test_get_alarm_history_constrained_by_severity(self): + alarm = self._get_alarm('a') + self._update_alarm(alarm, dict(severity='low')) + query = dict(field='severity', op='eq', value='low') + history = self._get_alarm_history('a', query=query) + self.assertEqual(1, len(history)) + self.assertEqual(jsonutils.dumps({'severity': 'low'}), + history[0]['detail']) + def test_get_nonexistent_alarm_history(self): # the existence of alarm history is independent of the # continued existence of the alarm itself