Merge remote-tracking branch 'origin/master' into paas
[osm/devops.git] / installers / charm / osm-temporal / src / legacy_interfaces.py
1 #!/usr/bin/env python3
2 # Copyright 2022 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 #
22 # flake8: noqa
23
24 import ops
25
26
27 class BaseRelationClient(ops.framework.Object):
28
29 def __init__(
30 self,
31 charm: ops.charm.CharmBase,
32 relation_name: str,
33 mandatory_fields: list = [],
34 ):
35 super().__init__(charm, relation_name)
36 self.relation_name = relation_name
37 self.mandatory_fields = mandatory_fields
38 self._update_relation()
39
40 def get_data_from_unit(self, key: str):
41 if not self.relation:
42 # This update relation doesn't seem to be needed, but I added it because apparently
43 # the data is empty in the unit tests.
44 # In reality, the constructor is called in every hook.
45 # In the unit tests when doing an update_relation_data, apparently it is not called.
46 self._update_relation()
47 if self.relation:
48 for unit in self.relation.units:
49 data = self.relation.data[unit].get(key)
50 if data:
51 return data
52
53 def get_data_from_app(self, key: str):
54 if not self.relation or self.relation.app not in self.relation.data:
55 # This update relation doesn't seem to be needed, but I added it because apparently
56 # the data is empty in the unit tests.
57 # In reality, the constructor is called in every hook.
58 # In the unit tests when doing an update_relation_data, apparently it is not called.
59 self._update_relation()
60 if self.relation and self.relation.app in self.relation.data:
61 data = self.relation.data[self.relation.app].get(key)
62 if data:
63 return data
64
65 def is_missing_data_in_unit(self):
66 return not all([self.get_data_from_unit(field) for field in self.mandatory_fields])
67
68 def is_missing_data_in_app(self):
69 return not all([self.get_data_from_app(field) for field in self.mandatory_fields])
70
71 def _update_relation(self):
72 self.relation = self.framework.model.get_relation(self.relation_name)
73
74
75 class MysqlClient(BaseRelationClient):
76 """Requires side of a Mysql Endpoint"""
77
78 mandatory_fields = ["host", "port", "user", "password", "root_password"]
79
80 def __init__(self, charm: ops.charm.CharmBase, relation_name: str):
81 super().__init__(charm, relation_name, self.mandatory_fields)
82
83 @property
84 def host(self):
85 return self.get_data_from_unit("host")
86
87 @property
88 def port(self):
89 return self.get_data_from_unit("port")
90
91 @property
92 def user(self):
93 return self.get_data_from_unit("user")
94
95 @property
96 def password(self):
97 return self.get_data_from_unit("password")
98
99 @property
100 def root_password(self):
101 return self.get_data_from_unit("root_password")
102
103 @property
104 def database(self):
105 return self.get_data_from_unit("database")
106
107 def get_root_uri(self, database: str):
108 """
109 Get the URI for the mysql connection with the root user credentials
110 :param: database: Database name
111 :return: A string with the following format:
112 mysql://root:<root_password>@<mysql_host>:<mysql_port>/<database>
113 """
114 return "mysql://root:{}@{}:{}/{}".format(
115 self.root_password, self.host, self.port, database
116 )
117
118 def get_uri(self):
119 """
120 Get the URI for the mysql connection with the standard user credentials
121 :param: database: Database name
122 :return: A string with the following format:
123 mysql://<user>:<password>@<mysql_host>:<mysql_port>/<database>
124 """
125 return "mysql://{}:{}@{}:{}/{}".format(
126 self.user, self.password, self.host, self.port, self.database
127 )