Skip to content

Commit

Permalink
mgr/dashboard: Applied Exception Handling
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Wagner <sebastian.wagner@suse.com>
  • Loading branch information
sebastian-philipp committed Apr 19, 2018
1 parent 66ec0a2 commit c37d1ff
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 57 deletions.
11 changes: 11 additions & 0 deletions qa/tasks/mgr/dashboard/test_cephfs.py
Expand Up @@ -37,3 +37,14 @@ def test_cephfs_mds_counters(self):

self.assertIsInstance(data, dict)
self.assertIsNotNone(data)

@authenticate
def test_cephfs_mds_counters_wrong(self):
data = self._get("/api/cephfs/mds_counters/baadbaad")
self.assertStatus(400)
self.assertJsonBody({
"component": 'cephfs',
"code": "invalid_cephfs_id",
"detail": "Invalid cephfs id baadbaad"
})

15 changes: 12 additions & 3 deletions qa/tasks/mgr/dashboard/test_pool.py
Expand Up @@ -18,9 +18,6 @@ def tearDownClass(cls):
cls._ceph_cmd(['osd', 'pool', 'delete', name, name, '--yes-i-really-really-mean-it'])
cls._ceph_cmd(['osd', 'erasure-code-profile', 'rm', 'ecprofile'])




@authenticate
def test_pool_list(self):
data = self._get("/api/pool")
Expand Down Expand Up @@ -141,6 +138,18 @@ def test_pool_create(self):
for data in pools:
self._pool_create(data)

@authenticate
def test_pool_create_fail(self):
data = {'pool_type': u'replicated', 'rule_name': u'dnf', 'pg_num': u'8', 'pool': u'sadfs'}
self._post('/api/pool/', data)
self.assertStatus(400)
self.assertJsonBody({
'errno': -2,
'component': 'pool',
'code': "2",
'detail': "specified rule dnf doesn't exist"
})

@authenticate
def test_pool_info(self):
info_data = self._get("/api/pool/_info")
Expand Down
5 changes: 4 additions & 1 deletion qa/tasks/mgr/dashboard/test_rbd.py
Expand Up @@ -120,5 +120,8 @@ def test_create_rbd_twice(self):

self._post('/api/rbd', data)
self.assertStatus(400)
self.assertJsonBody({"success": False, "errno": 17,
self.assertJsonBody({"success": False,
"errno": 17,
"code":"17",
"component": "rbd",
"detail": "[errno 17] error creating image"})
5 changes: 4 additions & 1 deletion src/pybind/mgr/dashboard/controllers/cephfs.py
Expand Up @@ -5,6 +5,7 @@

import cherrypy

from ..services.exception import DashboardException
from . import ApiController, AuthRequired, BaseController
from .. import mgr
from ..services.ceph_service import CephService
Expand Down Expand Up @@ -80,7 +81,9 @@ def fs_id_to_int(fs_id):
try:
return int(fs_id)
except ValueError:
raise cherrypy.HTTPError(400, "Invalid cephfs id {}".format(fs_id))
raise DashboardException(code='invalid_cephfs_id',
msg="Invalid cephfs id {}".format(fs_id),
component='cephfs')

def _get_mds_names(self, filesystem_id=None):
names = []
Expand Down
22 changes: 4 additions & 18 deletions src/pybind/mgr/dashboard/controllers/osd.py
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import json

from mgr_module import CommandResult

from . import ApiController, AuthRequired, RESTController
from .. import logger, mgr
from .. import mgr
from ..services.ceph_service import CephService
from ..services.exception import c2d, handle_send_command_error


@ApiController('osd')
Expand Down Expand Up @@ -51,20 +48,9 @@ def get_osd_map(self):
osds[str(osd['id'])] = osd
return osds

@c2d(handle_send_command_error, 'osd')
def get(self, svc_id):
result = CommandResult('')
mgr.send_command(result, 'osd', svc_id,
json.dumps({
'prefix': 'perf histogram dump',
}),
'')
r, outb, outs = result.wait()
if r != 0:
logger.warning('Failed to load histogram for OSD %s', svc_id)
logger.debug(outs)
histogram = outs
else:
histogram = json.loads(outb)
histogram = CephService.send_command('osd', srv_spec=svc_id, prefix='perf histogram dump')
return {
'osd_map': self.get_osd_map()[svc_id],
'osd_metadata': mgr.get_metadata('osd', svc_id),
Expand Down
8 changes: 7 additions & 1 deletion src/pybind/mgr/dashboard/controllers/pool.py
Expand Up @@ -6,6 +6,7 @@
from . import ApiController, RESTController, AuthRequired
from .. import mgr
from ..services.ceph_service import CephService
from ..services.exception import c2d, handle_send_command_error


@ApiController('pool')
Expand Down Expand Up @@ -53,14 +54,19 @@ def list(self, attrs=None, stats=False):

def get(self, pool_name, attrs=None, stats=False):
pools = self.list(attrs, stats)
return [pool for pool in pools if pool['pool_name'] == pool_name][0]
pool = [pool for pool in pools if pool['pool_name'] == pool_name]
if not pool:
return cherrypy.NotFound('No such pool')
return pool[0]

@c2d(handle_send_command_error, 'pool')
def delete(self, pool_name):
return CephService.send_command('mon', 'osd pool delete', pool=pool_name, pool2=pool_name,
sure='--yes-i-really-really-mean-it')

# pylint: disable=too-many-arguments, too-many-locals
@RESTController.args_from_json
@c2d(handle_send_command_error, 'pool')
def create(self, pool, pg_num, pool_type, erasure_code_profile=None, flags=None,
application_metadata=None, rule_name=None, **kwargs):
ecp = erasure_code_profile if erasure_code_profile else None
Expand Down
37 changes: 16 additions & 21 deletions src/pybind/mgr/dashboard/controllers/rbd.py
Expand Up @@ -2,12 +2,13 @@
from __future__ import absolute_import

import math
import cherrypy

import rbd

from . import ApiController, AuthRequired, RESTController
from .. import mgr
from ..tools import ViewCache
from ..services.exception import c2d, handle_rados_error, handle_rbd_error


@ApiController('rbd')
Expand Down Expand Up @@ -95,42 +96,36 @@ def _rbd_list(self, pool_name):
result.append(stat)
return result

@c2d(handle_rbd_error)
@c2d(handle_rados_error, 'pool')
def get(self, pool_name):
# pylint: disable=unbalanced-tuple-unpacking
status, value = self._rbd_list(pool_name)
if status == ViewCache.VALUE_EXCEPTION:
raise value
return {'status': status, 'value': value}

def create(self, data):
@c2d(handle_rbd_error)
@c2d(handle_rados_error, 'pool')
@RESTController.args_from_json
def create(self, name, pool_name, size, obj_size=None, features=None, stripe_unit=None,
stripe_count=None, data_pool=None):
# pylint: disable=too-many-arguments
if not self.rbd:
self.rbd = rbd.RBD()

# Get input values
name = data.get('name')
pool_name = data.get('pool_name')
size = data.get('size')
obj_size = data.get('obj_size')
features = data.get('features')
stripe_unit = data.get('stripe_unit')
stripe_count = data.get('stripe_count')
data_pool = data.get('data_pool')
obj_size, features, stripe_unit, stripe_count, data_pool = [x if x else None for x in [
obj_size, features, stripe_unit, stripe_count, data_pool
]]

# Set order
order = None
if obj_size and obj_size > 0:
if obj_size and float(obj_size) > 0:
order = int(round(math.log(float(obj_size), 2)))

# Set features
feature_bitmask = self._format_features(features)

ioctx = mgr.rados.open_ioctx(pool_name)

try:
self.rbd.create(ioctx, name, size, order=order, old_format=False,
with mgr.rados.open_ioctx(pool_name) as ioctx:
self.rbd.create(ioctx, name, int(size), order=order, old_format=False,
features=feature_bitmask, stripe_unit=stripe_unit,
stripe_count=stripe_count, data_pool=data_pool)
except rbd.OSError as e:
cherrypy.response.status = 400
return {'success': False, 'detail': str(e), 'errno': e.errno}
return {'success': True}
7 changes: 4 additions & 3 deletions src/pybind/mgr/dashboard/controllers/rbd_mirroring.py
Expand Up @@ -13,6 +13,7 @@
from .. import logger, mgr
from ..services.ceph_service import CephService
from ..tools import ViewCache
from ..services.exception import c2d, handle_rbd_error


@ViewCache()
Expand Down Expand Up @@ -94,6 +95,7 @@ def get_pools(daemons): # pylint: disable=R0912, R0915
mirror_mode = rbdctx.mirror_mode_get(ioctx)
except: # noqa pylint: disable=W0702
logger.exception("Failed to query mirror mode %s", pool_name)
mirror_mode = None

stats = {}
if mirror_mode == rbd.RBD_MIRROR_MODE_DISABLED:
Expand Down Expand Up @@ -163,6 +165,7 @@ def __init__(self):

@cherrypy.expose
@cherrypy.tools.json_out()
@c2d(handle_rbd_error)
def __call__(self):
status, content_data = self._get_content_data()
return {'status': status, 'content_data': content_data}
Expand Down Expand Up @@ -233,6 +236,7 @@ def _get_pool_datum(self, pool_name):
pass
except: # noqa pylint: disable=W0702
logger.exception("Failed to list mirror image status %s", pool_name)
raise

return data

Expand All @@ -250,9 +254,6 @@ def get_pool_datum(pool_name):

pool_names = [pool['pool_name'] for pool in CephService.get_pool_list('rbd')]
_, data = get_daemons_and_pools()
if isinstance(data, Exception):
logger.exception("Failed to get rbd-mirror daemons list")
raise type(data)(str(data))
daemons = data.get('daemons', [])
pool_stats = data.get('pools', {})

Expand Down
2 changes: 1 addition & 1 deletion src/pybind/mgr/dashboard/controllers/rgw.py
Expand Up @@ -47,7 +47,7 @@ def get(self, svc_id):
}
service = CephService.get_service('rgw', svc_id)
if not service:
return daemon
raise cherrypy.NotFound('unknown RGW')

metadata = service['metadata']
status = service['status']
Expand Down
16 changes: 8 additions & 8 deletions src/pybind/mgr/dashboard/controllers/summary.py
Expand Up @@ -5,9 +5,10 @@

import cherrypy

from .. import mgr
from . import AuthRequired, ApiController, BaseController
from .. import logger, mgr
from ..controllers.rbd_mirroring import get_daemons_and_pools
from ..tools import ViewCacheNoDataException
from ..services.ceph_service import CephService
from ..tools import TaskManager

Expand All @@ -34,14 +35,13 @@ def _filesystems(self):
]

def _rbd_mirroring(self):
_, data = get_daemons_and_pools()
try:
_, data = get_daemons_and_pools()
except ViewCacheNoDataException:
return {}

if isinstance(data, Exception):
logger.exception("Failed to get rbd-mirror daemons and pools")
raise type(data)(str(data))
else:
daemons = data.get('daemons', [])
pools = data.get('pools', {})
daemons = data.get('daemons', [])
pools = data.get('pools', {})

warnings = 0
errors = 0
Expand Down

0 comments on commit c37d1ff

Please sign in to comment.