Merge ~cjwatson/launchpad-layers:endpoint-from-flag into launchpad-layers:main

Proposed by Colin Watson
Status: Merged
Merged at revision: e4d85d4993268ca4c484c3e0b2715886159c9018
Proposed branch: ~cjwatson/launchpad-layers:endpoint-from-flag
Merge into: launchpad-layers:main
Diff against target: 49 lines (+14/-4)
1 file modified
launchpad-base/reactive/launchpad-base.py (+14/-4)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+439082@code.launchpad.net

Commit message

Use endpoint_from_flag rather than handler arguments

Description of the change

https://charmsreactive.readthedocs.io/en/latest/charms.reactive.decorators.html
says:

  For backwards compatibility, some decorators will pass endpoint
  instances if the handler function specifies them as arguments.
  However, explicit instance access using `endpoint_from_flag` is
  recommended, because ensuring proper argument order can be confusing:
  they are passed in bottom-up, left-to-right, and no negative or
  ambiguous decorators, such as `when_not()` or `when_any()` will ever
  pass arguments.

I also ran into some weird behaviour during an `upgrade-charm` attempt where the backward-compatibility mode doesn't quite seem to work, so use the recommended explicit form instead.

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) wrote :

Not entirely tested as yet, but I'll be able to test this properly when integrating it into the Launchpad charms.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/launchpad-base/reactive/launchpad-base.py b/launchpad-base/reactive/launchpad-base.py
2index 9145b65..b3bd439 100644
3--- a/launchpad-base/reactive/launchpad-base.py
4+++ b/launchpad-base/reactive/launchpad-base.py
5@@ -12,7 +12,14 @@ from charms.launchpad.base import (
6 strip_dsn_authentication,
7 update_pgpass,
8 )
9-from charms.reactive import hook, remove_state, set_state, when, when_not
10+from charms.reactive import (
11+ endpoint_from_flag,
12+ hook,
13+ remove_state,
14+ set_state,
15+ when,
16+ when_not,
17+)
18 from ols import base, postgres
19 from psycopg2.extensions import parse_dsn
20
21@@ -30,7 +37,8 @@ base.create_virtualenv = create_virtualenv
22
23
24 @when("rabbitmq.connected")
25-def prepare_rabbitmq(rabbitmq):
26+def prepare_rabbitmq():
27+ rabbitmq = endpoint_from_flag("rabbitmq.connected")
28 config = hookenv.config()
29 rabbitmq.request_access(config["rabbitmq_user"], config["domain"])
30
31@@ -50,7 +58,9 @@ def get_rabbitmq_uris(rabbitmq):
32
33 @when("ols.configured", "db.master.available", "rabbitmq.available")
34 @when_not("launchpad.base.configured")
35-def configure(db, rabbitmq):
36+def configure():
37+ db = endpoint_from_flag("db.master.available")
38+ rabbitmq = endpoint_from_flag("rabbitmq.available")
39 ensure_lp_directories()
40 config = get_service_config()
41 db_primary, db_standby = postgres.get_db_uris(db)
42@@ -109,6 +119,6 @@ def config_changed():
43
44
45 @hook("{requires:rabbitmq}-relation-changed")
46-def rabbitmq_relation_changed(rabbitmq):
47+def rabbitmq_relation_changed():
48 remove_state("launchpad.base.configured")
49 remove_state("service.configured")

Subscribers

People subscribed via source and target branches