]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Restore defaults of settings
authorPatrick Nawracay <pnawracay@suse.com>
Tue, 29 May 2018 13:43:38 +0000 (15:43 +0200)
committerPatrick Nawracay <pnawracay@suse.com>
Tue, 29 May 2018 13:43:38 +0000 (15:43 +0200)
This commit introduces a new CLI command to restore the value of
settings to their defaults.

    ceph dashboard reset-<command-name>

Signed-off-by: Patrick Nawracay <pnawracay@suse.com>
src/pybind/mgr/dashboard/settings.py
src/pybind/mgr/dashboard/tests/test_settings.py

index 3875f268254a9d374d36b7ea152c8c74c345abe0..c9cdfd668c8ca62c230b1781cde4f118c1501d5c 100644 (file)
@@ -46,6 +46,10 @@ class SettingsMeta(type):
         else:
             setattr(SettingsMeta, attr, value)
 
+    def __delattr__(self, attr):
+        if not attr.startswith('_') and hasattr(Options, attr):
+            mgr.set_config(attr, None)
+
 
 # pylint: disable=no-init
 @add_metaclass(SettingsMeta)
@@ -63,8 +67,10 @@ def _options_command_map():
             continue
         key_get = 'dashboard get-{}'.format(option.lower().replace('_', '-'))
         key_set = 'dashboard set-{}'.format(option.lower().replace('_', '-'))
+        key_reset = 'dashboard reset-{}'.format(option.lower().replace('_', '-'))
         cmd_map[key_get] = {'name': option, 'type': None}
         cmd_map[key_set] = {'name': option, 'type': value[1]}
+        cmd_map[key_reset] = {'name': option, 'type': None}
     return cmd_map
 
 
@@ -85,19 +91,27 @@ def options_command_list():
 
     cmd_list = []
     for cmd, opt in _OPTIONS_COMMAND_MAP.items():
-        if not opt['type']:
+        if cmd.startswith('dashboard get'):
             cmd_list.append({
                 'cmd': '{}'.format(cmd),
                 'desc': 'Get the {} option value'.format(opt['name']),
                 'perm': 'r'
             })
-        else:
+        elif cmd.startswith('dashboard set'):
             cmd_list.append({
                 'cmd': '{} name=value,type={}'
                        .format(cmd, py2ceph(opt['type'])),
                 'desc': 'Set the {} option value'.format(opt['name']),
                 'perm': 'w'
             })
+        elif cmd.startswith('dashboard reset'):
+            desc = 'Reset the {} option to its default value'.format(
+                opt['name'])
+            cmd_list.append({
+                'cmd': '{}'.format(cmd),
+                'desc': desc,
+                'perm': 'w'
+            })
 
     return cmd_list
 
@@ -118,12 +132,14 @@ def options_schema_list():
 def handle_option_command(cmd):
     if cmd['prefix'] not in _OPTIONS_COMMAND_MAP:
         return (-errno.ENOSYS, '', "Command not found '{}'".format(cmd['prefix']))
-
     opt = _OPTIONS_COMMAND_MAP[cmd['prefix']]
-    if not opt['type']:
-        # get option
-        return 0, str(getattr(Settings, opt['name'])), ''
 
-    # set option
-    setattr(Settings, opt['name'], opt['type'](cmd['value']))
-    return 0, 'Option {} updated'.format(opt['name']), ''
+    if cmd['prefix'].startswith('dashboard reset'):
+        delattr(Settings, opt['name'])
+        return 0, 'Option {} reset to default value "{}"'.format(
+            opt['name'], getattr(Settings, opt['name'])), ''
+    elif cmd['prefix'].startswith('dashboard get'):
+        return 0, str(getattr(Settings, opt['name'])), ''
+    elif cmd['prefix'].startswith('dashboard set'):
+        setattr(Settings, opt['name'], opt['type'](cmd['value']))
+        return 0, 'Option {} updated'.format(opt['name']), ''
index 92fcf7ff349e6c702332e1a85464b71f85cced0f..13c569e520a391d3644942a161670ad31b60633d 100644 (file)
@@ -58,6 +58,15 @@ class SettingsTest(unittest.TestCase):
         self.assertEqual(out, 'Option GRAFANA_API_PORT updated')
         self.assertEqual(err, '')
 
+    def test_reset_cmd(self):
+        r, out, err = handle_option_command(
+            {'prefix': 'dashboard reset-grafana-enabled'}
+        )
+        self.assertEqual(r, 0)
+        self.assertEqual(out, 'Option {} reset to default value "{}"'.format(
+            'GRAFANA_ENABLED', Settings.GRAFANA_ENABLED))
+        self.assertEqual(err, '')
+
     def test_inv_cmd(self):
         r, out, err = handle_option_command(
             {'prefix': 'dashboard get-non-existent-option'})