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