Configuration can be accesed on the config
object of the model
property on the charm object. On first access it will run juju config-get
, and cache the results transparently.
Example
from ops.charm import CharmBase
class MyCharm(CharmBase):
# ...
def on_some_event(self, event):
if self.model.config['foo'] == 'xyzzy':
# yak yak yak
Configuration changes are reported via a ConfigChangedEvent
, as
expected.
Example
from ops.charm import CharmBase
class MyCharm(CharmBase):
# ...
def on_config_changed(self, event):
# self.model.config will already have the new values
If the charm has a way to validate its config, it can do that and set the charm's status to blocked if it fails.
Example
from ops.charm import CharmBase
from ops.model import BlockedStatus
class MyCharm(CharmBase):
# ...
def on_config_changed(self, event):
try:
# call my custom validator that raises a custom exception on error
self._validate_config()
except MyValidationError as e:
self.status = BlockedStatus("fix yo stuff: {}".format(e))
The operator framework ties together Python's standard library's logging facility with juju's. Logging should just work™ (file bugs).
⚠️ The tieing-together is done in the framework'smain
; if you somehow manage to not use that, you need to reimplement that for it to work.
Example
from ops.charm import CharmBase
import logging
logger = logging.getLogger()
class MyCharm(CharmBase):
# ...
def on_frobnicated(self, event):
logger.warning("'tis the end of times")