Commit d6eb55b0 authored by fleischmann's avatar fleischmann
Browse files

Improve Squid Charm code



Remove unnecesary code in charm.py for a simpler
example.

Signed-off-by: default avatarDominik Fleischmann <dominik.fleischmann@canonical.com>
parent dc2cfc3b
Loading
Loading
Loading
Loading
+0 −24
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ class SquidK8SCharm(CharmBase):

        if not self.state.installed:
            logging.warning("Config changed called before install complete, deferring event: {}.".format(event.handle))
            self._defer_once(event)

            return

@@ -81,7 +80,6 @@ class SquidK8SCharm(CharmBase):

        if not self.state.configured:
            logging.warning("Start called before configuration complete, deferring event: {}".format(event.handle))
            self._defer_once(event)

            return
        self.unit.status = MaintenanceStatus("Applying pod spec")
@@ -93,28 +91,6 @@ class SquidK8SCharm(CharmBase):
        self.state.started = True
        logging.info("Started")

    def _defer_once(self, event):
        """Defer the given event, but only once."""
        notice_count = 0
        handle = str(event.handle)

        for event_path, _, _ in self.framework._storage.notices(None):
            if event_path.startswith(handle.split('[')[0]):
                notice_count += 1
                logging.debug("Found event: {} x {}".format(event_path, notice_count))

        if notice_count > 1:
            logging.debug("Not deferring {} notice count of {}".format(handle, notice_count))
        else:
            logging.debug("Deferring {} notice count of {}".format(handle, notice_count))
            event.defer()

    def on_addconfig_action(self, event):
        """Handle the example_action action."""
        event.log("Hello from the example action.")
        event.set_results({"success": "true"})


if __name__ == "__main__":
    from ops.main import main
    main(SquidK8SCharm)