]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | # -*- coding: utf-8 -*- |
11fdf7f2 TL |
2 | from contextlib import contextmanager |
3 | ||
4 | import cherrypy | |
5 | ||
11fdf7f2 | 6 | from ..security import Scope |
f67539c2 TL |
7 | from ..settings import Options |
8 | from ..settings import Settings as SettingsModule | |
a4b75251 | 9 | from . import APIDoc, APIRouter, EndpointDoc, RESTController, UIRouter |
f67539c2 TL |
10 | |
11 | SETTINGS_SCHEMA = [{ | |
12 | "name": (str, 'Settings Name'), | |
13 | "default": (bool, 'Default Settings'), | |
14 | "type": (str, 'Type of Settings'), | |
15 | "value": (bool, 'Settings Value') | |
16 | }] | |
11fdf7f2 TL |
17 | |
18 | ||
a4b75251 TL |
19 | @APIRouter('/settings', Scope.CONFIG_OPT) |
20 | @APIDoc("Settings Management API", "Settings") | |
11fdf7f2 TL |
21 | class Settings(RESTController): |
22 | """ | |
23 | Enables to manage the settings of the dashboard (not the Ceph cluster). | |
24 | """ | |
25 | @contextmanager | |
26 | def _attribute_handler(self, name): | |
27 | """ | |
28 | :type name: str|dict[str, str] | |
29 | :rtype: str|dict[str, str] | |
30 | """ | |
31 | if isinstance(name, dict): | |
9f95a23c TL |
32 | result = { |
33 | self._to_native(key): value | |
34 | for key, value in name.items() | |
35 | } | |
11fdf7f2 TL |
36 | else: |
37 | result = self._to_native(name) | |
38 | ||
39 | try: | |
40 | yield result | |
f6b5b4d7 TL |
41 | except AttributeError: # pragma: no cover - handling is too obvious |
42 | raise cherrypy.NotFound(result) # pragma: no cover - handling is too obvious | |
11fdf7f2 TL |
43 | |
44 | @staticmethod | |
45 | def _to_native(setting): | |
46 | return setting.upper().replace('-', '_') | |
47 | ||
f67539c2 TL |
48 | @EndpointDoc("Display Settings Information", |
49 | parameters={ | |
50 | 'names': (str, 'Name of Settings'), | |
51 | }, | |
52 | responses={200: SETTINGS_SCHEMA}) | |
9f95a23c TL |
53 | def list(self, names=None): |
54 | """ | |
55 | Get the list of available options. | |
56 | :param names: A comma separated list of option names that should | |
f67539c2 | 57 | be processed. Defaults to ``None``. |
9f95a23c TL |
58 | :type names: None|str |
59 | :return: A list of available options. | |
60 | :rtype: list[dict] | |
61 | """ | |
62 | option_names = [ | |
63 | name for name in Options.__dict__ | |
11fdf7f2 TL |
64 | if name.isupper() and not name.startswith('_') |
65 | ] | |
9f95a23c TL |
66 | if names: |
67 | names = names.split(',') | |
68 | option_names = list(set(option_names) & set(names)) | |
69 | return [self._get(name) for name in option_names] | |
11fdf7f2 TL |
70 | |
71 | def _get(self, name): | |
72 | with self._attribute_handler(name) as sname: | |
f67539c2 | 73 | setting = getattr(Options, sname) |
11fdf7f2 TL |
74 | return { |
75 | 'name': sname, | |
f67539c2 TL |
76 | 'default': setting.default_value, |
77 | 'type': setting.types_as_str(), | |
11fdf7f2 TL |
78 | 'value': getattr(SettingsModule, sname) |
79 | } | |
80 | ||
81 | def get(self, name): | |
9f95a23c TL |
82 | """ |
83 | Get the given option. | |
84 | :param name: The name of the option. | |
85 | :return: Returns a dict containing the name, type, | |
f67539c2 | 86 | default value and current value of the given option. |
9f95a23c TL |
87 | :rtype: dict |
88 | """ | |
11fdf7f2 TL |
89 | return self._get(name) |
90 | ||
91 | def set(self, name, value): | |
92 | with self._attribute_handler(name) as sname: | |
93 | setattr(SettingsModule, self._to_native(sname), value) | |
94 | ||
95 | def delete(self, name): | |
96 | with self._attribute_handler(name) as sname: | |
97 | delattr(SettingsModule, self._to_native(sname)) | |
98 | ||
99 | def bulk_set(self, **kwargs): | |
100 | with self._attribute_handler(kwargs) as data: | |
101 | for name, value in data.items(): | |
102 | setattr(SettingsModule, self._to_native(name), value) | |
9f95a23c TL |
103 | |
104 | ||
a4b75251 | 105 | @UIRouter('/standard_settings') |
9f95a23c TL |
106 | class StandardSettings(RESTController): |
107 | def list(self): | |
108 | """ | |
109 | Get various Dashboard related settings. | |
110 | :return: Returns a dictionary containing various Dashboard | |
f67539c2 | 111 | settings. |
9f95a23c TL |
112 | :rtype: dict |
113 | """ | |
f6b5b4d7 | 114 | return { # pragma: no cover - no complexity there |
9f95a23c TL |
115 | 'user_pwd_expiration_span': |
116 | SettingsModule.USER_PWD_EXPIRATION_SPAN, | |
117 | 'user_pwd_expiration_warning_1': | |
118 | SettingsModule.USER_PWD_EXPIRATION_WARNING_1, | |
119 | 'user_pwd_expiration_warning_2': | |
120 | SettingsModule.USER_PWD_EXPIRATION_WARNING_2, | |
121 | 'pwd_policy_enabled': | |
122 | SettingsModule.PWD_POLICY_ENABLED, | |
123 | 'pwd_policy_min_length': | |
124 | SettingsModule.PWD_POLICY_MIN_LENGTH, | |
125 | 'pwd_policy_check_length_enabled': | |
126 | SettingsModule.PWD_POLICY_CHECK_LENGTH_ENABLED, | |
127 | 'pwd_policy_check_oldpwd_enabled': | |
128 | SettingsModule.PWD_POLICY_CHECK_OLDPWD_ENABLED, | |
129 | 'pwd_policy_check_username_enabled': | |
130 | SettingsModule.PWD_POLICY_CHECK_USERNAME_ENABLED, | |
131 | 'pwd_policy_check_exclusion_list_enabled': | |
132 | SettingsModule.PWD_POLICY_CHECK_EXCLUSION_LIST_ENABLED, | |
133 | 'pwd_policy_check_repetitive_chars_enabled': | |
134 | SettingsModule.PWD_POLICY_CHECK_REPETITIVE_CHARS_ENABLED, | |
135 | 'pwd_policy_check_sequential_chars_enabled': | |
136 | SettingsModule.PWD_POLICY_CHECK_SEQUENTIAL_CHARS_ENABLED, | |
137 | 'pwd_policy_check_complexity_enabled': | |
138 | SettingsModule.PWD_POLICY_CHECK_COMPLEXITY_ENABLED | |
139 | } |