Merge "Check for service existance in capabilities API"
This commit is contained in:
commit
6268c4bc0d
@ -14,10 +14,14 @@
|
||||
# under the License.
|
||||
|
||||
from oslo_log import log as logging
|
||||
import oslo_messaging
|
||||
|
||||
from cinder.api import extensions
|
||||
from cinder.api.openstack import wsgi
|
||||
from cinder.api.views import capabilities as capabilities_view
|
||||
from cinder import exception
|
||||
from cinder.i18n import _
|
||||
from cinder import objects
|
||||
from cinder.volume import rpcapi
|
||||
|
||||
|
||||
@ -44,7 +48,15 @@ class CapabilitiesController(wsgi.Controller):
|
||||
"""Return capabilities list of given backend."""
|
||||
context = req.environ['cinder.context']
|
||||
authorize(context, 'capabilities')
|
||||
capabilities = self.volume_api.get_capabilities(context, id, False)
|
||||
filters = {'host': id, 'binary': 'cinder-volume'}
|
||||
service = objects.ServiceList.get_all(context, filters)
|
||||
if not service:
|
||||
msg = (_("Can't find service: %s") % id)
|
||||
raise exception.NotFound(msg)
|
||||
try:
|
||||
capabilities = self.volume_api.get_capabilities(context, id, False)
|
||||
except oslo_messaging.MessagingTimeout:
|
||||
raise exception.RPCTimeout(service=id)
|
||||
return self._view_builder.summary(req, capabilities, id)
|
||||
|
||||
|
||||
|
@ -270,6 +270,12 @@ class APITimeout(APIException):
|
||||
message = _("Timeout while requesting %(service)s API.")
|
||||
|
||||
|
||||
class RPCTimeout(CinderException):
|
||||
message = _("Timeout while requesting capabilities from backend "
|
||||
"%(service)s.")
|
||||
code = 502
|
||||
|
||||
|
||||
class NotFound(CinderException):
|
||||
message = _("Resource could not be found.")
|
||||
code = 404
|
||||
|
@ -15,8 +15,11 @@
|
||||
|
||||
import mock
|
||||
|
||||
import oslo_messaging
|
||||
|
||||
from cinder.api.contrib import capabilities
|
||||
from cinder import context
|
||||
from cinder import exception
|
||||
from cinder import test
|
||||
from cinder.tests.unit.api import fakes
|
||||
|
||||
@ -55,8 +58,6 @@ def rpcapi_get_capabilities(self, context, host, discover):
|
||||
return capabilities
|
||||
|
||||
|
||||
@mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities',
|
||||
rpcapi_get_capabilities)
|
||||
class CapabilitiesAPITest(test.TestCase):
|
||||
def setUp(self):
|
||||
super(CapabilitiesAPITest, self).setUp()
|
||||
@ -64,7 +65,11 @@ class CapabilitiesAPITest(test.TestCase):
|
||||
self.controller = capabilities.CapabilitiesController()
|
||||
self.ctxt = context.RequestContext('admin', 'fake', True)
|
||||
|
||||
def test_capabilities_summary(self):
|
||||
@mock.patch('cinder.db.service_get_all')
|
||||
@mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities',
|
||||
rpcapi_get_capabilities)
|
||||
def test_capabilities_summary(self, mock_services):
|
||||
mock_services.return_value = [{'name': 'fake'}]
|
||||
req = fakes.HTTPRequest.blank('/fake/capabilities/fake')
|
||||
req.environ['cinder.context'] = self.ctxt
|
||||
res = self.controller.show(req, 'fake')
|
||||
@ -102,3 +107,23 @@ class CapabilitiesAPITest(test.TestCase):
|
||||
}
|
||||
|
||||
self.assertDictMatch(expected, res)
|
||||
|
||||
@mock.patch('cinder.db.service_get_all')
|
||||
@mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities')
|
||||
def test_get_capabilities_rpc_timeout(self, mock_rpc, mock_services):
|
||||
mock_rpc.side_effect = oslo_messaging.MessagingTimeout
|
||||
mock_services.return_value = [{'name': 'fake'}]
|
||||
|
||||
req = fakes.HTTPRequest.blank('/fake/capabilities/fake')
|
||||
req.environ['cinder.context'] = self.ctxt
|
||||
self.assertRaises(exception.RPCTimeout,
|
||||
self.controller.show, req, 'fake')
|
||||
|
||||
@mock.patch('cinder.db.service_get_all')
|
||||
def test_get_capabilities_service_not_found(self, mock_services):
|
||||
mock_services.return_value = []
|
||||
|
||||
req = fakes.HTTPRequest.blank('/fake/capabilities/fake')
|
||||
req.environ['cinder.context'] = self.ctxt
|
||||
self.assertRaises(exception.NotFound,
|
||||
self.controller.show, req, 'fake')
|
||||
|
Loading…
Reference in New Issue
Block a user