-
Notifications
You must be signed in to change notification settings - Fork 21
/
mysql_repo.py
132 lines (117 loc) · 5.31 KB
/
mysql_repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2015-2017 FUJITSU LIMITED
# (C) Copyright 2015,2016 Hewlett Packard Enterprise Development LP
#
# 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_log import log as logging
import pymysql
from monasca_notification.common.repositories.base import base_repo
from monasca_notification.common.repositories import exceptions as exc
log = logging.getLogger(__name__)
class MysqlRepo(base_repo.BaseRepo):
def __init__(self, config):
super(MysqlRepo, self).__init__(config)
self._mysql_ssl = config['mysql']['ssl'] or None
self._mysql_host = config['mysql']['host']
self._mysql_port = config['mysql']['port']
self._mysql_user = config['mysql']['user']
self._mysql_passwd = config['mysql']['passwd']
self._mysql_dbname = config['mysql']['db']
self._mysql = None
def _connect_to_mysql(self):
self._mysql = None
try:
self._mysql = pymysql.connect(host=self._mysql_host,
port=self._mysql_port,
user=self._mysql_user,
passwd=self._mysql_passwd,
db=self._mysql_dbname,
ssl=self._mysql_ssl,
use_unicode=True,
charset="utf8")
self._mysql.autocommit(True)
except pymysql.Error as e:
log.exception('MySQL connect failed %s', e)
raise
def fetch_notifications(self, alarm):
try:
if self._mysql is None:
self._connect_to_mysql()
cur = self._mysql.cursor()
cur.execute(
self._find_alarm_action_sql,
(alarm['alarmDefinitionId'],
alarm['newState']))
for row in cur:
yield (row[0], row[1].lower(), row[2], row[3], row[4])
except pymysql.Error as e:
self._mysql = None
log.exception("Couldn't fetch alarms actions %s", e)
raise exc.DatabaseException(e)
def get_alarm_current_state(self, alarm_id):
try:
if self._mysql is None:
self._connect_to_mysql()
cur = self._mysql.cursor()
cur.execute(self._find_alarm_state_sql, alarm_id)
row = cur.fetchone()
state = row[0] if row is not None else None
return state
except pymysql.Error as e:
self._mysql = None
log.exception("Couldn't fetch the current alarm state %s", e)
raise exc.DatabaseException(e)
def fetch_notification_method_types(self):
try:
if self._mysql is None:
self._connect_to_mysql()
cur = self._mysql.cursor()
cur.execute(self._find_all_notification_types_sql)
for row in cur:
yield (row[0])
except pymysql.Error as e:
self._mysql = None
log.exception("Couldn't fetch notification types %s", e)
raise exc.DatabaseException(e)
def insert_notification_method_types(self, notification_types):
try:
if self._mysql is None:
self._connect_to_mysql()
cur = self._mysql.cursor()
cur.executemany(self._insert_notification_types_sql, notification_types)
except pymysql.IntegrityError as ignoredException:
# If multiple instances of the notification engine tries to write the
# same content at the same time, only one of them will succeed and others will
# get duplicate primary key, integrity error. We can safely ignore this error.
# This may happen only during the first start when the tables are empty.
code, mesg = ignoredException.args
if code == pymysql.constants.ER.DUP_ENTRY:
log.debug("Notification type exists in DB. Ignoring the exception {}".format(mesg))
else:
raise exc.DatabaseException(ignoredException)
except pymysql.Error as e:
self._mysql = None
log.exception("Couldn't insert notification types %s", e)
raise exc.DatabaseException(e)
def get_notification(self, notification_id):
try:
if self._mysql is None:
self._connect_to_mysql()
cur = self._mysql.cursor()
cur.execute(self._get_notification_sql, notification_id)
row = cur.fetchone()
if row is None:
return None
else:
return [row[0], row[1].lower(), row[2], row[3]]
except pymysql.Error as e:
self._mysql = None
log.exception("Couldn't fetch the notification method %s", e)
raise exc.DatabaseException(e)