def get_request_extensions(self): request_extensions = [] def _get_and_extend_one(context, server_id, body): compute_api = compute.API() try: inst_ref = compute_api.routing_get(context, server_id) except exception.NotFound: explanation = _("Server not found.") raise exc.HTTPNotFound(explanation=explanation) for state in ['task_state', 'vm_state', 'power_state']: key = "%s:%s" % (Extended_status.alias, state) body['server'][key] = inst_ref[state] def _get_and_extend_all(context, body): # TODO(mdietz): This is a brilliant argument for this to *not* # be an extension. The problem is we either have to 1) duplicate # the logic from the servers controller or 2) do what we did # and iterate over the list of potentially sorted, limited # and whatever else elements and find each individual. compute_api = compute.API() for server in body['servers']: try: inst_ref = compute_api.routing_get(context, server['id']) except exception.NotFound: explanation = _("Server not found.") raise exc.HTTPNotFound(explanation=explanation) #TODO(bcwaldon): these attributes should be prefixed with # something specific to this extension for state in ['task_state', 'vm_state', 'power_state']: key = "%s:%s" % (Extended_status.alias, state) server[key] = inst_ref[state] def _extended_status_handler(req, res, body): context = req.environ['nova.context'] server_id = req.environ['wsgiorg.routing_args'][1].get('id') if 'nova.template' in req.environ: tmpl = req.environ['nova.template'] tmpl.attach(ExtendedStatusTemplate()) if server_id: _get_and_extend_one(context, server_id, body) else: _get_and_extend_all(context, body) return res req_ext = extensions.RequestExtension('GET', '/:(project_id)/servers/:(id)', _extended_status_handler) request_extensions.append(req_ext) return request_extensions
def get_request_extensions(self): request_exts = [] def _goose_handler(req, res, body): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. body['flavor']['googoose'] = req.GET.get('chewing') return res req_ext1 = extensions.RequestExtension( 'GET', '/v2/:(project_id)/flavors/:(id)', _goose_handler) request_exts.append(req_ext1) def _bands_handler(req, res, body): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. body['big_bands'] = 'Pig Bands!' return res req_ext2 = extensions.RequestExtension( 'GET', '/v2/:(project_id)/flavors/:(id)', _bands_handler) request_exts.append(req_ext2) return request_exts
def test_get_resources_with_stub_mgr(self): def _req_handler(req, res, body): # only handle JSON responses body['flavor']['googoose'] = req.GET.get('chewing') return res req_ext = extensions.RequestExtension('GET', '/v2/123/flavors/:(id)', _req_handler) manager = StubExtensionManager(None, None, req_ext) app = fakes.wsgi_app(serialization=base_wsgi.Middleware) ext_midware = extensions.ExtensionMiddleware(app, manager) ser_midware = wsgi.LazySerializationMiddleware(ext_midware) request = webob.Request.blank("/v2/123/flavors/1?chewing=bluegoo") request.environ['api.version'] = '2' response = request.get_response(ser_midware) self.assertEqual(200, response.status_int) response_data = json.loads(response.body) self.assertEqual('bluegoo', response_data['flavor']['googoose'])