Skip to content

Commit

Permalink
Defer database event when username is None (#1005)
Browse files Browse the repository at this point in the history
* Defer database event when `username` is `None`

Signed-off-by: Babak K. Shandiz <[email protected]>

* Check for `event.password` being None

Signed-off-by: Babak K. Shandiz <[email protected]>

* Fix lint issues

Signed-off-by: Babak K. Shandiz <[email protected]>

* Set `ops >= 2.0.0` to match data platforms lib requirements

Signed-off-by: Babak K. Shandiz <[email protected]>

---------

Signed-off-by: Babak K. Shandiz <[email protected]>
Co-authored-by: Kian Parvin <[email protected]>
  • Loading branch information
babakks and kian99 authored Jul 28, 2023
1 parent 43d2a96 commit b0bf509
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions charms/jimm-k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ def _on_dashboard_relation_joined(self, event: RelationJoinedEvent):
def _on_database_event(self, event: DatabaseEvent) -> None:
"""Database event handler."""

if event.username is None or event.password is None:
event.defer()
logger.info(
"(postgresql) Relation data is not complete (missing `username` or `password` field); "
"deferring the event."
)
return

# get the first endpoint from a comma separate list
ep = event.endpoints.split(",", 1)[0]
# compose the db connection string
Expand Down
2 changes: 1 addition & 1 deletion charms/jimm/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
markupsafe>=2.0.1
Jinja2 >= 2.11.3
ops >= 1.4.0
ops >= 2.0.0
charmhelpers >= 0.20.22
hvac >= 0.11.0
pydantic == 1.10.10
Expand Down
8 changes: 8 additions & 0 deletions charms/jimm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ def _on_database_event(self, event: DatabaseRequiresEvent):
event.defer()
return

if event.username is None or event.password is None:
event.defer()
logger.info(
"(postgresql) Relation data is not complete (missing `username` or `password` field); "
"deferring the event."
)
return

# get the first endpoint from a comma separate list
host = event.endpoints.split(",", 1)[0]
# compose the db connection string
Expand Down

0 comments on commit b0bf509

Please sign in to comment.