+ def _on_create_datasource_action(self, event: ActionEvent) -> None:
+ """Handler for the create-datasource action event."""
+ url = event.params["url"]
+ if not self._is_valid_url(url):
+ event.fail(f"Invalid datasource url '{url}'")
+ return
+ grafana_config = self._get_grafana_config()
+ datasource_config = DatasourceConfig(event.params["name"], url)
+ response = GrafanaDataSourceHandler.create_datasource(grafana_config, datasource_config)
+ logger.debug(response)
+ if response.is_success:
+ event.set_results(response.results)
+ return
+ event.fail(response.message)
+
+ def _on_list_datasources_action(self, event: ActionEvent) -> None:
+ """Handler for the list-datasource action event."""
+ grafana_config = self._get_grafana_config()
+ response = GrafanaDataSourceHandler.list_datasources(grafana_config)
+ logger.debug(response)
+ if response.is_success:
+ event.set_results(response.results)
+ return
+ event.fail(response.message)
+
+ def _on_delete_datasource_action(self, event: ActionEvent) -> None:
+ """Handler for the delete-datasource action event."""
+ datasource_name = event.params["name"]
+ grafana_config = self._get_grafana_config()
+ response = GrafanaDataSourceHandler.delete_datasource(grafana_config, datasource_name)
+ logger.debug(response)
+ if not response.is_success:
+ event.fail(response.message)
+
+ def _get_grafana_config(self) -> GrafanaConfig:
+ return GrafanaConfig(
+ self.config.get("grafana-user", ""),
+ self.config.get("grafana-password", ""),
+ self.config.get("grafana-url", ""),
+ )
+