Exemple #1
0
    def _view_hypervisor(self,
                         hypervisor,
                         service,
                         detail,
                         req,
                         servers=None,
                         **kwargs):
        alive = self.servicegroup_api.service_is_up(service)
        # The 2.53 microversion returns the compute node uuid rather than id.
        uuid_for_id = api_version_request.is_supported(
            req, min_version=UUID_FOR_ID_MIN_VERSION)
        hyp_dict = {
            'id': hypervisor.uuid if uuid_for_id else hypervisor.id,
            'hypervisor_hostname': hypervisor.hypervisor_hostname,
            'state': 'up' if alive else 'down',
            'status': ('disabled' if service.disabled else 'enabled'),
        }

        if detail:
            for field in ('vcpus', 'memory_mb', 'local_gb', 'vcpus_used',
                          'memory_mb_used', 'local_gb_used', 'hypervisor_type',
                          'hypervisor_version', 'free_ram_mb', 'free_disk_gb',
                          'current_workload', 'running_vms',
                          'disk_available_least', 'host_ip'):
                hyp_dict[field] = getattr(hypervisor, field)

            # L3 CAT Support
            if api_version_request.wrs_is_supported(req):
                for field in ('l3_closids', 'l3_closids_used'):
                    hyp_dict[field] = getattr(hypervisor, field)

            service_id = service.uuid if uuid_for_id else service.id
            hyp_dict['service'] = {
                'id': service_id,
                'host': hypervisor.host,
                'disabled_reason': service.disabled_reason,
            }

            if api_version_request.is_supported(req, min_version='2.28'):
                if hypervisor.cpu_info:
                    hyp_dict['cpu_info'] = jsonutils.loads(hypervisor.cpu_info)
                else:
                    hyp_dict['cpu_info'] = {}
            else:
                hyp_dict['cpu_info'] = hypervisor.cpu_info

            if api_version_request.wrs_is_supported(req):
                self._add_stats_to_dict(hypervisor, hyp_dict)

        if servers:
            hyp_dict['servers'] = [
                dict(name=serv['name'], uuid=serv['uuid']) for serv in servers
            ]

        # Add any additional info
        if kwargs:
            hyp_dict.update(kwargs)

        return hyp_dict
Exemple #2
0
 def show(self, req, resp_obj, id):
     context = req.environ['nova.context']
     if context.can(wrs_res_policies.BASE_POLICY_NAME, fatal=False):
         server = resp_obj.obj['server']
         db_instance = req.get_db_instance(server['id'])
         # server['id'] is guaranteed to be in the cache due to
         # the core API adding it in its 'show' method.
         if api_version_request.wrs_is_supported(req):
             self._extend_server(context, server, db_instance)
Exemple #3
0
 def detail(self, req, resp_obj):
     context = req.environ['nova.context']
     if context.can(wrs_if_policies.BASE_POLICY_NAME, fatal=False):
         servers = list(resp_obj.obj['servers'])
         for server in servers:
             instance = req.get_db_instance(server['id'])
             # server['id'] is guaranteed to be in the cache due to
             # the core API adding it in its 'detail' method.
             if api_version_request.wrs_is_supported(req):
                 self._extend_server(context, server, instance)
Exemple #4
0
    def _process_stack(self, request, action, action_args, content_type, body,
                       accept):
        """Implement the processing stack."""

        # Get the implementing method
        try:
            meth, extensions = self.get_method(request, action, content_type,
                                               body)
        except (AttributeError, TypeError):
            return Fault(webob.exc.HTTPNotFound())
        except KeyError as ex:
            msg = _("There is no such action: %s") % ex.args[0]
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))
        except exception.MalformedRequestBody:
            msg = _("Malformed request body")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        if body:
            msg = _("Action: '%(action)s', calling method: %(meth)s, body: "
                    "%(body)s") % {
                        'action': action,
                        'body': six.text_type(body, 'utf-8'),
                        'meth': str(meth)
                    }
            LOG.debug(strutils.mask_password(msg))
        else:
            LOG.debug("Calling method '%(meth)s'", {'meth': str(meth)})

        # Now, deserialize the request body...
        try:
            contents = self._get_request_content(body, request)
        except exception.MalformedRequestBody:
            msg = _("Malformed request body")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        # Update the action args
        action_args.update(contents)

        project_id = action_args.pop("project_id", None)
        context = request.environ.get('nova.context')
        if (context and project_id and (project_id != context.project_id)):
            msg = _("Malformed request URL: URL's project_id '%(project_id)s'"
                    " doesn't match Context's project_id"
                    " '%(context_project_id)s'") % \
                    {'project_id': project_id,
                     'context_project_id': context.project_id}
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        response = None
        try:
            with ResourceExceptionHandler():
                action_result = self.dispatch(meth, request, action_args)
        except Fault as ex:
            response = ex

        if not response:
            # No exceptions; convert action_result into a
            # ResponseObject
            resp_obj = None
            if type(action_result) is dict or action_result is None:
                resp_obj = ResponseObject(action_result)
            elif isinstance(action_result, ResponseObject):
                resp_obj = action_result
            else:
                response = action_result

            # Run post-processing extensions
            if resp_obj:
                # Do a preserialize to set up the response object
                if hasattr(meth, 'wsgi_code'):
                    resp_obj._default_code = meth.wsgi_code
                # Process extensions
                response = self.process_extensions(extensions, resp_obj,
                                                   request, action_args)

            if resp_obj and not response:
                response = resp_obj.serialize(request, accept)

        if hasattr(response, 'headers'):
            # return with WRS header if request came with one
            if api_version.wrs_is_supported(request):
                response.headers['wrs-header'] = 'true'

            for hdr, val in list(response.headers.items()):
                if six.PY2:
                    # In Py2.X Headers must be byte strings
                    response.headers[hdr] = utils.utf8(val)
                else:
                    # In Py3.X Headers must be utf-8 strings
                    response.headers[hdr] = encodeutils.safe_decode(
                        utils.utf8(val))

            if not request.api_version_request.is_null():
                response.headers[API_VERSION_REQUEST_HEADER] = \
                    'compute ' + request.api_version_request.get_string()
                response.headers[LEGACY_API_VERSION_REQUEST_HEADER] = \
                    request.api_version_request.get_string()
                response.headers.add('Vary', API_VERSION_REQUEST_HEADER)
                response.headers.add('Vary', LEGACY_API_VERSION_REQUEST_HEADER)

        return response