summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/controllers/settings.py
blob: a484580ee1c1a65887b5bc795851469e217361b4 (plain)
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from contextlib import contextmanager

import cherrypy

from . import ApiController, RESTController
from ..settings import Settings as SettingsModule, Options
from ..security import Scope


@ApiController('/settings', Scope.CONFIG_OPT)
class Settings(RESTController):
    """
    Enables to manage the settings of the dashboard (not the Ceph cluster).
    """
    @contextmanager
    def _attribute_handler(self, name):
        """
        :type name: str|dict[str, str]
        :rtype: str|dict[str, str]
        """
        if isinstance(name, dict):
            result = {self._to_native(key): value
                      for key, value in name.items()}
        else:
            result = self._to_native(name)

        try:
            yield result
        except AttributeError:
            raise cherrypy.NotFound(result)

    @staticmethod
    def _to_native(setting):
        return setting.upper().replace('-', '_')

    def list(self):
        return [
            self._get(name) for name in Options.__dict__
            if name.isupper() and not name.startswith('_')
        ]

    def _get(self, name):
        with self._attribute_handler(name) as sname:
            default, data_type = getattr(Options, sname)
        return {
            'name': sname,
            'default': default,
            'type': data_type.__name__,
            'value': getattr(SettingsModule, sname)
        }

    def get(self, name):
        return self._get(name)

    def set(self, name, value):
        with self._attribute_handler(name) as sname:
            setattr(SettingsModule, self._to_native(sname), value)

    def delete(self, name):
        with self._attribute_handler(name) as sname:
            delattr(SettingsModule, self._to_native(sname))

    def bulk_set(self, **kwargs):
        with self._attribute_handler(kwargs) as data:
            for name, value in data.items():
                setattr(SettingsModule, self._to_native(name), value)