From 98a8683b2ca7d36483b7514ce731b75e2d0e5598 Mon Sep 17 00:00:00 2001 From: John A Meinel Date: Thu, 5 Dec 2024 10:15:36 -0500 Subject: [PATCH] fix: use ops.main() instead of ops.main.main() Ops changed to prefer importing just `ops` and having all of the functionality as an attribute of that top level package, rather than needing to know what module has what functionality. I'm only changing this because we're actually getting a lot of Deprecation warnings in our logs. ``` unit-controller-1: 09:38:39 WARNING unit.controller/1.leader-settings-changed /var/lib/juju/agents/unit-controller-1/charm/./src/charm.py:162: DeprecationWarning: Calling `ops.main.main()` is deprecated, call `ops.main()` instead unit-controller-1: 09:38:39 WARNING unit.controller/1.leader-settings-changed main(JujuControllerCharm) unit-controller-1: 09:38:39 INFO juju.worker.uniter.operation ran "leader-settings-changed" hook (via hook dispatching script: dispatch) unit-controller-1: 09:38:39 WARNING unit.controller/1.config-changed /var/lib/juju/agents/unit-controller-1/charm/./src/charm.py:162: DeprecationWarning: Calling `ops.main.main()` is deprecated, call `ops.main()` instead unit-controller-1: 09:38:39 WARNING unit.controller/1.config-changed main(JujuControllerCharm) ``` However, I went through and updated the other places as well. --- src/charm.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/charm.py b/src/charm.py index 43a5e1e..ba13b44 100755 --- a/src/charm.py +++ b/src/charm.py @@ -7,19 +7,15 @@ import secrets import urllib.parse import yaml +import ops from charms.prometheus_k8s.v0.prometheus_scrape import MetricsEndpointProvider -from ops.charm import CharmBase -from ops.framework import StoredState -from ops.charm import RelationJoinedEvent, RelationDepartedEvent -from ops.main import main -from ops.model import ActiveStatus, BlockedStatus, Relation from typing import List logger = logging.getLogger(__name__) -class JujuControllerCharm(CharmBase): - _stored = StoredState() +class JujuControllerCharm(ops.CharmBase): + _stored = ops.StoredState() def __init__(self, *args): super().__init__(*args) @@ -38,7 +34,7 @@ def __init__(self, *args): self.on.metrics_endpoint_relation_broken, self._on_metrics_endpoint_relation_broken) def _on_start(self, _): - self.unit.status = ActiveStatus() + self.unit.status = ops.ActiveStatus() def _on_config_changed(self, _): controller_url = self.config["controller-url"] @@ -61,7 +57,7 @@ def _on_website_relation_joined(self, event): port = self.api_port() if port is None: logger.error("machine does not appear to be a controller") - self.unit.status = BlockedStatus('machine does not appear to be a controller') + self.unit.status = ops.BlockedStatus('machine does not appear to be a controller') return address = None @@ -75,7 +71,7 @@ def _on_website_relation_joined(self, event): 'port': str(port) }) - def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent): + def _on_metrics_endpoint_relation_created(self, event: ops.RelationJoinedEvent): username = metrics_username(event.relation) password = generate_password() self.control_socket.add_metrics_user(username, password) @@ -84,7 +80,7 @@ def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent): try: api_port = self.api_port() except AgentConfException as e: - self.unit.status = BlockedStatus( + self.unit.status = ops.BlockedStatus( f"can't read controller API port from agent.conf: {e}") return @@ -110,7 +106,7 @@ def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent): ) metrics_endpoint.set_scrape_job_spec() - def _on_metrics_endpoint_relation_broken(self, event: RelationDepartedEvent): + def _on_metrics_endpoint_relation_broken(self, event: ops.RelationDepartedEvent): username = metrics_username(event.relation) self.control_socket.remove_metrics_user(username) @@ -141,7 +137,7 @@ def ca_cert(self) -> str: return self._agent_conf('cacert') -def metrics_username(relation: Relation) -> str: +def metrics_username(relation: ops.Relation) -> str: """ Return the username used to access the metrics endpoint, for the given relation. This username has the form @@ -159,4 +155,4 @@ class AgentConfException(Exception): if __name__ == "__main__": - main(JujuControllerCharm) + ops.main(JujuControllerCharm)