Notification Engine for Monasca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pgsql_repo.py 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2015-2017 FUJITSU LIMITED
  2. # (C) Copyright 2016 Hewlett Packard Enterprise Development LP
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. # in compliance with the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the License
  10. # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. # or implied. See the License for the specific language governing permissions and limitations under
  12. # the License.
  13. from oslo_log import log as logging
  14. import psycopg2
  15. from monasca_notification.common.repositories.base import base_repo
  16. from monasca_notification.common.repositories import exceptions as exc
  17. log = logging.getLogger(__name__)
  18. class PostgresqlRepo(base_repo.BaseRepo):
  19. def __init__(self, config):
  20. super(PostgresqlRepo, self).__init__(config)
  21. self._pgsql_params = config['postgresql']
  22. self._pgsql = None
  23. def _connect_to_pgsql(self):
  24. self._pgsql = None
  25. try:
  26. self._pgsql = psycopg2.connect(**self._pgsql_params)
  27. self._pgsql.autocommit = True
  28. except psycopg2.Error as e:
  29. log.exception('Pgsql connect failed %s', e)
  30. raise
  31. def fetch_notifications(self, alarm):
  32. try:
  33. if self._pgsql is None:
  34. self._connect_to_pgsql()
  35. cur = self._pgsql.cursor()
  36. cur.execute(self._find_alarm_action_sql, (alarm['alarmDefinitionId'], alarm['newState']))
  37. for row in cur:
  38. yield (row[0], row[1].lower(), row[2], row[3], row[4])
  39. except psycopg2.Error as e:
  40. log.exception("Couldn't fetch alarms actions %s", e)
  41. raise exc.DatabaseException(e)
  42. def get_alarm_current_state(self, alarm_id):
  43. try:
  44. if self._pgsql is None:
  45. self._connect_to_pgsql()
  46. cur = self._pgsql.cursor()
  47. cur.execute(self._find_alarm_state_sql, alarm_id)
  48. row = cur.fetchone()
  49. state = row[0] if row is not None else None
  50. return state
  51. except psycopg2.Error as e:
  52. log.exception("Couldn't fetch current alarm state %s", e)
  53. raise exc.DatabaseException(e)
  54. def fetch_notification_method_types(self):
  55. try:
  56. if self._pgsql is None:
  57. self._connect_to_pgsql()
  58. cur = self._pgsql.cursor()
  59. cur.execute(self._find_all_notification_types_sql)
  60. for row in cur:
  61. yield (row[0])
  62. except psycopg2.Error as e:
  63. log.exception("Couldn't fetch notification types %s", e)
  64. raise exc.DatabaseException(e)
  65. def insert_notification_method_types(self, notification_types):
  66. try:
  67. if self._pgsql is None:
  68. self._connect_to_pgsql()
  69. cur = self._pgsql.cursor()
  70. cur.executemany(self._insert_notification_types_sql, notification_types)
  71. except psycopg2.Error as e:
  72. log.exception("Couldn't insert notification types %s", e)
  73. raise exc.DatabaseException(e)
  74. def get_notification(self, notification_id):
  75. try:
  76. if self._pgsql is None:
  77. self._connect_to_pgsql()
  78. cur = self._pgsql.cursor()
  79. cur.execute(self._get_notification_sql, notification_id)
  80. row = cur.fetchone()
  81. if row is None:
  82. return None
  83. else:
  84. return [row[0], row[1].lower(), row[2], row[3]]
  85. except psycopg2.Error as e:
  86. log.exception("Couldn't fetch the notification method %s", e)
  87. raise exc.DatabaseException(e)