Repository navigation
Expand file tree
/
Copy pathservice.py
More file actions
112 lines (83 loc) · 3.75 KB
/
Copy pathservice.py
File metadata and controls
112 lines (83 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import logging
from typing import List, Optional
from fastapi.encoders import jsonable_encoder
from dispatch.exceptions import InvalidConfiguration
from dispatch.plugins.bases import OncallPlugin
from dispatch.service import service as service_service
from .models import Plugin, PluginCreate, PluginUpdate
log = logging.getLogger(__name__)
def get(*, db_session, plugin_id: int) -> Optional[Plugin]:
"""Returns a plugin based on the given plugin id."""
return db_session.query(Plugin).filter(Plugin.id == plugin_id).one_or_none()
def get_active(*, db_session, plugin_type: str) -> Optional[Plugin]:
"""Fetches the current active plugin for the given type."""
plugin = (
db_session.query(Plugin)
.filter(Plugin.type == plugin_type)
.filter(Plugin.enabled == True) # noqa
.one_or_none()
)
if not plugin:
log.warning(
f"Attempted to fetch active plugin, but none were found. PluginType: {plugin_type}"
)
return plugin
def get_by_type(*, db_session, plugin_type: str) -> List[Optional[Plugin]]:
"""Fetches all plugins for a given type."""
return db_session.query(Plugin).filter(Plugin.type == plugin_type).all()
def get_enabled_by_type(*, db_session, plugin_type: str) -> List[Optional[Plugin]]:
"""Fetches all enabled plugins for a given type."""
return (
db_session.query(Plugin)
.filter(Plugin.type == plugin_type)
.filter(Plugin.enabled == True) # noqa
.all()
)
def get_by_slug(*, db_session, slug: str) -> Plugin:
"""Fetches a plugin by slug."""
return db_session.query(Plugin).filter(Plugin.slug == slug).one_or_none()
def get_all(*, db_session) -> List[Optional[Plugin]]:
"""Returns all plugins."""
return db_session.query(Plugin)
def create(*, db_session, plugin_in: PluginCreate) -> Plugin:
"""Creates a new plugin."""
plugin = Plugin(**plugin_in.dict())
db_session.add(plugin)
db_session.commit()
return plugin
def update(*, db_session, plugin: Plugin, plugin_in: PluginUpdate) -> Plugin:
"""Updates a plugin."""
plugin_data = jsonable_encoder(plugin)
update_data = plugin_in.dict(skip_defaults=True)
if plugin_in.enabled: # user wants to enable the plugin
if not plugin.multiple:
# we can't have multiple plugins of this type disable the currently enabled one
enabled_plugins = get_enabled_by_type(db_session=db_session, plugin_type=plugin.type)
if enabled_plugins:
enabled_plugins[0].enabled = False
db_session.add(enabled_plugins[0])
if not plugin_in.enabled: # user wants to disable the plugin
if plugin.required:
enabled_plugins = get_enabled_by_type(db_session=db_session, plugin_type=plugin.type)
if len(enabled_plugins) == 1:
raise InvalidConfiguration(
f"Cannot disable plugin: {plugin.title}. It is required and no other plugins of type {plugin.type} are enabled."
)
if plugin.type == OncallPlugin.type:
oncall_services = service_service.get_all_by_type_and_status(
db_session=db_session, service_type=plugin.slug, is_active=True
)
if oncall_services:
raise InvalidConfiguration(
f"Cannot disable plugin: {plugin.title}. One or more oncall services depend on it. "
)
for field in plugin_data:
if field in update_data:
setattr(plugin, field, update_data[field])
db_session.add(plugin)
db_session.commit()
return plugin
def delete(*, db_session, plugin_id: int):
"""Deletes a plugin."""
db_session.query(Plugin).filter(Plugin.id == plugin_id).delete()
db_session.commit()