-
Notifications
You must be signed in to change notification settings - Fork 5
/
requires.py
93 lines (79 loc) · 3.13 KB
/
requires.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
#!/usr/bin/python
# 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 charms.reactive import Endpoint
from charms.reactive import when, when_not
from charms.reactive import set_flag, clear_flag
from charms.reactive import data_changed
class MySQLClient(Endpoint):
@when('endpoint.{endpoint_name}.joined')
def _handle_joined(self):
# translate automatic internal joined flag to published connected flag
set_flag(self.expand_name('{endpoint_name}.connected'))
@when('endpoint.{endpoint_name}.changed')
def _handle_changed(self):
set_flag(self.expand_name('{endpoint_name}.connected'))
if self.connection_string():
set_flag(self.expand_name('{endpoint_name}.available'))
data_key = self.expand_name('endpoint.{endpoint_name}.data')
if data_changed(data_key, self.connection_string()):
set_flag(self.expand_name('{endpoint_name}.changed'))
@when_not('endpoint.{endpoint_name}.joined')
def _handle_broken(self):
clear_flag(self.expand_name('{endpoint_name}.connected'))
clear_flag(self.expand_name('{endpoint_name}.available'))
def connection_string(self):
"""
Get the connection string, if available, or None.
The connection string will be in the format::
'host={host} port={port} dbname={database} '
'user={user} password={password}'
"""
data = {
'host': self.host(),
'port': self.port(),
'database': self.database(),
'user': self.user(),
'password': self.password(),
}
if all(data.values()):
return str.format(
'host={host} port={port} dbname={database} '
'user={user} password={password}',
**data)
return None
def database(self):
"""
Return the name of the provided database.
"""
return self.all_joined_units.received_raw['database']
def host(self):
"""
Return the host for the provided database.
"""
return self.all_joined_units.received_raw['host']
def port(self):
"""
Return the port the provided database.
If not available, returns the default port of 3306.
"""
return self.all_joined_units.received_raw.get('port', 3306)
def user(self):
"""
Return the username for the provided database.
"""
return self.all_joined_units.received_raw['user']
def password(self):
"""
Return the password for the provided database.
"""
return self.all_joined_units.received_raw['password']