summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/controllers/crush_rule.py
blob: 250f657b2bae691622a84a6a2c0025525c813d52 (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 cherrypy import NotFound

from .. import mgr
from ..security import Scope
from ..services.ceph_service import CephService
from . import APIDoc, APIRouter, Endpoint, EndpointDoc, ReadPermission, RESTController, UIRouter
from ._version import APIVersion

LIST_SCHEMA = {
    "rule_id": (int, 'Rule ID'),
    "rule_name": (str, 'Rule Name'),
    "ruleset": (int, 'RuleSet related to the rule'),
    "type": (int, 'Type of Rule'),
    "min_size": (int, 'Minimum size of Rule'),
    "max_size": (int, 'Maximum size of Rule'),
    'steps': ([{str}], 'Steps included in the rule')
}


@APIRouter('/crush_rule', Scope.POOL)
@APIDoc("Crush Rule Management API", "CrushRule")
class CrushRule(RESTController):
    @EndpointDoc("List Crush Rule Configuration",
                 responses={200: LIST_SCHEMA})
    @RESTController.MethodMap(version=APIVersion(2, 0))
    def list(self):
        return mgr.get('osd_map_crush')['rules']

    @RESTController.MethodMap(version=APIVersion(2, 0))
    def get(self, name):
        rules = mgr.get('osd_map_crush')['rules']
        for r in rules:
            if r['rule_name'] == name:
                return r
        raise NotFound('No such crush rule')

    def create(self, name, root, failure_domain, device_class=None):
        rule = {
            'name': name,
            'root': root,
            'type': failure_domain,
            'class': device_class
        }
        CephService.send_command('mon', 'osd crush rule create-replicated', **rule)

    def delete(self, name):
        CephService.send_command('mon', 'osd crush rule rm', name=name)


@UIRouter('/crush_rule', Scope.POOL)
@APIDoc("Dashboard UI helper function; not part of the public API", "CrushRuleUi")
class CrushRuleUi(CrushRule):
    @Endpoint()
    @ReadPermission
    def info(self):
        '''Used for crush rule creation modal'''
        osd_map = mgr.get_osdmap()
        crush = osd_map.get_crush()
        crush.dump()
        return {
            'names': [r['rule_name'] for r in mgr.get('osd_map_crush')['rules']],
            'nodes': mgr.get('osd_map_tree')['nodes'],
            'roots': crush.find_roots()
        }