16
16
from charms .data_platform_libs .v0 .data_interfaces import DatabaseProvides
17
17
from charms .mongodb .v0 .mongo import MongoConfiguration , MongoConnection
18
18
from charms .mongodb .v1 .helpers import generate_password
19
- from ops .charm import CharmBase , EventBase , RelationBrokenEvent , RelationChangedEvent
19
+ from ops .charm import (
20
+ CharmBase ,
21
+ EventBase ,
22
+ RelationBrokenEvent ,
23
+ RelationChangedEvent ,
24
+ RelationEvent ,
25
+ )
20
26
from ops .framework import Object
21
27
from ops .model import Relation
22
28
from pymongo .errors import PyMongoError
31
37
32
38
# Increment this PATCH version before using `charmcraft publish-lib` or reset
33
39
# to 0 if you are raising the major API version
34
- LIBPATCH = 13
40
+ LIBPATCH = 15
35
41
36
42
logger = logging .getLogger (__name__ )
37
43
REL_NAME = "database"
@@ -88,6 +94,11 @@ def pass_sanity_hook_checks(self) -> bool:
88
94
if not self .charm .db_initialised :
89
95
return False
90
96
97
+ # Warning: the sanity_hook_checks can pass when the call is
98
+ # issued by a config-sever because the config-server is allowed to manage the users
99
+ # in MongoDB. This is not well named and does not protect integration of a config-server
100
+ # to a client application. The mongos charm however doesn't care
101
+ # because it supports only one integration that uses MongoDBProvider.
91
102
if not self .charm .is_role (Config .Role .MONGOS ) and not self .charm .is_relation_feasible (
92
103
self .get_relation_name ()
93
104
):
@@ -99,8 +110,14 @@ def pass_sanity_hook_checks(self) -> bool:
99
110
100
111
return True
101
112
102
- def pass_hook_checks (self , event : EventBase ) -> bool :
113
+ def pass_hook_checks (self , event : RelationEvent ) -> bool :
103
114
"""Runs the pre-hooks checks for MongoDBProvider, returns True if all pass."""
115
+ # First, ensure that the relation is valid, useless to do anything else otherwise
116
+ if not self .charm .is_role (Config .Role .MONGOS ) and not self .charm .is_relation_feasible (
117
+ event .relation .name
118
+ ):
119
+ return False
120
+
104
121
if not self .pass_sanity_hook_checks ():
105
122
return False
106
123
@@ -372,6 +389,7 @@ def _get_config(
372
389
mongo_args ["port" ] = Config .MONGOS_PORT
373
390
if self .substrate == Config .Substrate .K8S :
374
391
mongo_args ["hosts" ] = self .charm .get_mongos_hosts_for_client ()
392
+ mongo_args ["port" ] = self .charm .get_mongos_port ()
375
393
else :
376
394
mongo_args ["replset" ] = self .charm .app .name
377
395
0 commit comments