Skip to content

Commit

Permalink
fix: use ops.main() instead of ops.main.main()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jameinel committed Dec 5, 2024
1 parent a5274b6 commit 98a8683
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"]
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -159,4 +155,4 @@ class AgentConfException(Exception):


if __name__ == "__main__":
main(JujuControllerCharm)
ops.main(JujuControllerCharm)

0 comments on commit 98a8683

Please sign in to comment.