Skip to content

Commit

Permalink
Move event codes logging to KATalogus client (#3956)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Klopper <[email protected]>
  • Loading branch information
Donnype and underdarknl authored Dec 16, 2024
1 parent 5a09e4d commit 969b6d2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
21 changes: 19 additions & 2 deletions rocky/katalogus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core.exceptions import ValidationError
from django.core.validators import validate_unicode_slug
from django.utils.translation import gettext_lazy as _
from httpx import HTTPStatusError, Response, codes
from httpx import HTTPError, HTTPStatusError, Response, codes
from jsonschema.exceptions import SchemaError
from jsonschema.validators import Draft202012Validator
from pydantic import AfterValidator, BaseModel, Field, field_serializer, field_validator
Expand Down Expand Up @@ -127,6 +127,13 @@ def __init__(self, error: httpx.HTTPStatusError):
super().__init__(_("An HTTP %d error occurred. Check logs for more info.").format(error.response.status_code))


class KATalogusHTTPError(KATalogusError):
def __init__(self, error: httpx.HTTPError):
self.error = error

super().__init__(_("An HTTP error occurred. Check logs for more info."))


class DuplicatePluginError(KATalogusError):
def __init__(self, error_message: str):
super().__init__(error_message)
Expand Down Expand Up @@ -164,6 +171,8 @@ def verify_response(response: Response) -> None:
raise KATalogusNotAllowedError("Access to resource not allowed")

raise KATalogusHTTPStatusError(error) from error
except HTTPError as error:
raise KATalogusError("KATalogus request failed") from error


class KATalogusClient:
Expand Down Expand Up @@ -213,18 +222,21 @@ def get_plugin_settings(self, organization_code: str, plugin_id: str) -> dict:
return response.json()

def upsert_plugin_settings(self, organization_code: str, plugin_id: str, values: dict) -> None:
logger.info("Adding plugin settings", event_code=800023, plugin=plugin_id)
self.session.put(f"/v1/organisations/{quote(organization_code)}/{quote(plugin_id)}/settings", json=values)

logger.info("Upsert plugin settings", plugin_id=plugin_id)

def delete_plugin_settings(self, organization_code: str, plugin_id: str) -> None:
logger.info("Deleting plugin settings", event_code=800024, plugin=plugin_id)
self.session.delete(f"/v1/organisations/{quote(organization_code)}/{quote(plugin_id)}/settings")

logger.info("Delete plugin settings", plugin_id=plugin_id)
logger.info("Deleted plugin settings", plugin_id=plugin_id)

def clone_all_configuration_to_organization(self, from_organization: str, to_organization: str):
to_organization = quote(to_organization)
from_organization = quote(from_organization)
logger.info("Cloning organization settings", event_code=910000, to_organization_code=to_organization)
response = self.session.post(f"/v1/organisations/{from_organization}/settings/clone/{to_organization}")

return response
Expand All @@ -236,12 +248,15 @@ def get_boefjes(self, organization_code: str) -> list[Plugin]:
return self.get_plugins(organization_code, plugin_type="boefje")

def enable_plugin(self, organization_code: str, plugin: Plugin) -> None:
logger.info("Enabling plugin", event_code=800021, plugin=plugin.id)

self._patch_plugin_state(organization_code, plugin.id, True)

def enable_boefje_by_id(self, organization_code: str, boefje_id: str) -> None:
self.enable_plugin(organization_code, self.get_plugin(organization_code, boefje_id))

def disable_plugin(self, organization_code: str, plugin: Plugin) -> None:
logger.info("Disabling plugin", event_code=800022, plugin=plugin.id)
self._patch_plugin_state(organization_code, plugin.id, False)

def get_enabled_boefjes(self, organization_code: str) -> list[Plugin]:
Expand All @@ -257,6 +272,7 @@ def get_cover(self, organization_code: str, plugin_id: str) -> BytesIO:

def create_plugin(self, organization_code: str, plugin: Plugin) -> None:
try:
logger.info("Creating boefje", event_code=800025, boefje=plugin)
response = self.session.post(
f"/v1/organisations/{quote(organization_code)}/plugins",
headers={"Content-Type": "application/json"},
Expand All @@ -272,6 +288,7 @@ def create_plugin(self, organization_code: str, plugin: Plugin) -> None:

def edit_plugin(self, organization_code: str, plugin: Plugin) -> None:
try:
logger.info("Editing boefje", event_code=800026, boefje=plugin.id)
response = self.session.patch(
f"/v1/organisations/{quote(organization_code)}/boefjes/{plugin.id}",
content=plugin.model_dump_json(exclude_none=True),
Expand Down
3 changes: 0 additions & 3 deletions rocky/katalogus/views/boefje_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def form_valid(self, form):
plugin = create_boefje_with_form_data(form_data, self.plugin_id, self.created)

try:
logger.info("Creating boefje", event_code=800025, boefje=plugin)
self.get_katalogus().create_plugin(plugin)
return super().form_valid(form)
except DuplicatePluginError as error:
Expand Down Expand Up @@ -98,7 +97,6 @@ def form_valid(self, form):
plugin = create_boefje_with_form_data(form_data, self.plugin_id, self.created)

try:
logger.info("Creating boefje", event_code=800025, boefje=plugin)
self.get_katalogus().create_plugin(plugin)
return super().form_valid(form)
except DuplicatePluginError as error:
Expand Down Expand Up @@ -168,7 +166,6 @@ def form_valid(self, form):
plugin = create_boefje_with_form_data(form_data, self.plugin_id, self.created)

try:
logger.info("Editing boefje", event_code=800026, boefje=plugin)
self.get_katalogus().edit_plugin(plugin)
return super().form_valid(form)
except DuplicatePluginError as error:
Expand Down
2 changes: 0 additions & 2 deletions rocky/katalogus/views/plugin_enable_disable.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def post(self, request, *args, **kwargs):
plugin_state = kwargs["plugin_state"]

if plugin_state == "True":
logger.info("Disabling plugin", event_code=800022, plugin=self.plugin.name)
self.katalogus_client.disable_plugin(self.plugin)
messages.add_message(
self.request,
Expand All @@ -25,7 +24,6 @@ def post(self, request, *args, **kwargs):
return HttpResponseRedirect(request.POST.get("current_url"))

if self.plugin.can_scan(self.organization_member):
logger.info("Enabling plugin", event_code=800021, plugin=self.plugin.name)
self.katalogus_client.enable_plugin(self.plugin)
messages.add_message(
self.request, messages.SUCCESS, _("{} '{}' enabled.").format(self.plugin.type.title(), self.plugin.name)
Expand Down
2 changes: 0 additions & 2 deletions rocky/katalogus/views/plugin_settings_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def form_valid(self, form):
return redirect(self.get_success_url())

try:
logger.info("Adding plugin settings", event_code=800023, plugin=self.plugin.name)
self.katalogus_client.upsert_plugin_settings(self.plugin.id, form.cleaned_data)
messages.add_message(self.request, messages.SUCCESS, _("Added settings for '{}'").format(self.plugin.name))
except HTTPError:
Expand All @@ -56,7 +55,6 @@ def form_valid(self, form):

if "add-enable" in self.request.POST:
try:
logger.info("Enabling plugin", event_code=800021, plugin=self.plugin.name)
self.katalogus_client.enable_plugin(self.plugin)
except HTTPError:
messages.add_message(self.request, messages.ERROR, _("Enabling {} failed").format(self.plugin.name))
Expand Down
1 change: 0 additions & 1 deletion rocky/katalogus/views/plugin_settings_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def get_success_url(self):

def delete(self, request, *args, **kwargs):
try:
logger.info("Deleting plugin settings", event_code=800024, plugin=self.plugin.name)
self.katalogus_client.delete_plugin_settings(self.plugin.id)
messages.add_message(
request, messages.SUCCESS, _("Settings for plugin {} successfully deleted.").format(self.plugin.name)
Expand Down
13 changes: 6 additions & 7 deletions rocky/reports/views/generate_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from httpx import HTTPError
from katalogus.client import get_katalogus
from katalogus.client import KATalogusHTTPError, KATalogusNotAllowedError, get_katalogus
from tools.view_helpers import Breadcrumb, PostRedirect

from reports.views.base import (
Expand Down Expand Up @@ -110,15 +109,15 @@ def post(self, request, *args, **kwargs):
if not selected_plugins:
return super().post(request, *args, **kwargs)

if not self.organization_member.has_perm("tools.can_enable_disable_boefje"):
messages.error(request, _("You do not have the required permissions to enable plugins."))
return PostRedirect(self.get_previous())

client = get_katalogus(self.organization_member)

for selected_plugin in selected_plugins:
try:
client.enable_boefje_by_id(selected_plugin)
except HTTPError:
except KATalogusNotAllowedError:
messages.error(request, _("You do not have the required permissions to enable plugins."))
return PostRedirect(self.get_previous())
except KATalogusHTTPError:
messages.error(
request,
_("An error occurred while enabling {}. The plugin is not available.").format(selected_plugin),
Expand Down
6 changes: 5 additions & 1 deletion rocky/rocky/locale/django.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-03 21:56+0000\n"
"POT-Creation-Date: 2024-12-12 10:54+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -611,6 +611,10 @@ msgstr ""
msgid "An HTTP %d error occurred. Check logs for more info."
msgstr ""

#: katalogus/client.py
msgid "An HTTP error occurred. Check logs for more info."
msgstr ""

#: katalogus/client.py
msgid "Boefje with this name already exists."
msgstr ""
Expand Down
6 changes: 0 additions & 6 deletions rocky/tools/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ def clone_katalogus_settings(self, request, pk=None):
serializer = ToOrganizationSerializer(data=request.data)
if serializer.is_valid():
to_organization = serializer.validated_data["to_organization"]
logger.info(
"Cloning organization settings",
event_code=910000,
organization_code=from_organization.code,
to_organization_code=to_organization.code,
)
get_katalogus_client().clone_all_configuration_to_organization(from_organization.code, to_organization.code)

return Response()
Expand Down

0 comments on commit 969b6d2

Please sign in to comment.