Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service: add CRUD actions for CheckConfigService #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions invenio_checks/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

from . import config
from .base import ChecksRegistry
from .services import (
CheckConfigService,
ChecksConfigServiceConfig,
)


class InvenioChecks(object):
Expand All @@ -22,6 +26,7 @@ def __init__(self, app=None):
def init_app(self, app):
"""Flask application initialization."""
self.init_config(app)
self.init_services(app)
app.extensions["invenio-checks"] = self
self.checks_registry = ChecksRegistry()
self.checks_registry.load_from_entry_points(app, "invenio_checks.check_types")
Expand All @@ -31,3 +36,19 @@ def init_config(self, app):
for k in dir(config):
if k.startswith("CHECKS_"):
app.config.setdefault(k, getattr(config, k))

def service_configs(self, app):
"""Customized service configs."""

class ServiceConfigs:
checks_configs = ChecksConfigServiceConfig.build(app)

return ServiceConfigs

def init_services(self, app):
"""Initialize the service and resource for Requests."""
service_configs = self.service_configs(app)

self.checks_configs_service = CheckConfigService(
config=service_configs.checks_configs,
)
5 changes: 5 additions & 0 deletions invenio_checks/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@
current_checks_registry = LocalProxy(
lambda: current_app.extensions["invenio-checks"].checks_registry
)

current_checks_configs_service = LocalProxy(
lambda: current_app.extensions["invenio-checks"].checks_configs_service
)
"""Proxy to the instantiated checks_configs service."""
95 changes: 87 additions & 8 deletions invenio_checks/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@

"""Checks services."""

from uuid import UUID

from invenio_records_resources.services.records import RecordService
from invenio_records_resources.services.uow import (
ModelCommitOp,
ModelDeleteOp,
unit_of_work,
)
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound

from ..models import CheckConfig


class BaseClass(RecordService):
Expand All @@ -23,25 +33,94 @@ def rebuild_index(self, identity, uow=None):
raise NotImplementedError()


def get_check_config(id_):
"""Get a check config by id."""
check_config = CheckConfig.query.get(id_)
if check_config == None:
raise ValueError(f"Check configuration with id '{id_}' not found")
return check_config


class CheckConfigService(RecordService):
"""Service for managing and check configurations."""
"""Service for managing check configurations."""

@unit_of_work()
def create(self, identity, data, uow=None, **kwargs):
"""Create a check configuration."""
self.require_permission(identity, "create")

try:
check_config = CheckConfig(
community_id=UUID(data["community_id"]),
check_id=data["check_id"],
params=data.get("params", {}),
severity=data.get("severity", "I"),
enabled=data.get("enabled", True),
)
uow.register(ModelCommitOp(check_config))
return check_config
except Exception as e:
raise ValueError(f"Failed to create check configuration: {str(e)}")

def read(self, identity, id_, **kwargs):
"""Read a check configuration."""
raise NotImplementedError()
self.require_permission(identity, "read")
check_config = get_check_config(id_)
return check_config

def search(self, identity, params=None, **kwargs):
"""Search for check configurations."""
raise NotImplementedError()
self.require_permission(identity, "search")

def create(self, identity, data, uow=None, **kwargs):
"""Create a check configuration."""
raise NotImplementedError()
if params is None:
params = {}
query = CheckConfig.query

if "community_id" in params:
query = query.filter_by(community_id=UUID(params["community_id"]))
if "check_id" in params:
query = query.filter_by(check_id=params["check_id"])
if "enabled" in params:
query = query.filter_by(enabled=params["enabled"])

return query.all()

@unit_of_work()
def update(self, identity, id_, data, revision_id=None, uow=None, **kwargs):
"""Update a check configuration."""
raise NotImplementedError()
self.require_permission(identity, "update")

try:
check_config = get_check_config(id_)
if "community_id" in data:
check_config.community_id = UUID(data["community_id"])
if "check_id" in data:
check_config.check_id = data["check_id"]
if "params" in data:
check_config.params = data["params"]
if "severity" in data:
check_config.severity = data["severity"]
if "enabled" in data:
check_config.enabled = data["enabled"]

uow.register(ModelCommitOp(check_config))
return check_config
except NoResultFound:
raise ValueError(f"Check configuration with id '{id_}' not found")
except Exception as e:
raise ValueError(f"Failed to update check configuration: {str(e)}")

@unit_of_work()
def delete(self, identity, id_, revision_id=None, uow=None, **kwargs):
"""Delete a check configuration."""
raise NotImplementedError()
self.require_permission(identity, "delete")

try:
check_config = get_check_config(id_)
uow.register(ModelDeleteOp(check_config))

return True
except NoResultFound:
raise ValueError(f"Check configuration with id '{id_}' not found")
except Exception as e:
raise ValueError(f"Failed to delete check configuration: {str(e)}")
Loading