Ejemplo n.º 1
0
Archivo: rapi.py Proyecto: azet/ganeti
  def HandleRequest(self, req):
    """Handles a request.

    """
    ctx = self._GetRequestContext(req)

    # Deserialize request parameters
    if req.request_body:
      # RFC2616, 7.2.1: Any HTTP/1.1 message containing an entity-body SHOULD
      # include a Content-Type header field defining the media type of that
      # body. [...] If the media type remains unknown, the recipient SHOULD
      # treat it as type "application/octet-stream".
      req_content_type = req.request_headers.get(http.HTTP_CONTENT_TYPE,
                                                 http.HTTP_APP_OCTET_STREAM)
      if req_content_type.lower() != http.HTTP_APP_JSON.lower():
        raise http.HttpUnsupportedMediaType()

      try:
        ctx.body_data = serializer.LoadJson(req.request_body)
      except Exception:
        raise http.HttpBadRequest(message="Unable to parse JSON data")
    else:
      ctx.body_data = None

    try:
      result = ctx.handler_fn()
    except rpcerr.TimeoutError:
      raise http.HttpGatewayTimeout()
    except rpcerr.ProtocolError, err:
      raise http.HttpBadGateway(str(err))
Ejemplo n.º 2
0
    def SubmitJob(self, op, cl=None):
        """Generic wrapper for submit job, for better http compatibility.

    @type op: list
    @param op: the list of opcodes for the job
    @type cl: None or luxi.Client
    @param cl: optional luxi client to use
    @rtype: string
    @return: the job ID

    """
        if cl is None:
            cl = self.GetClient()
        try:
            return cl.SubmitJob(op)
        except errors.JobQueueFull:
            raise http.HttpServiceUnavailable(
                "Job queue is full, needs archiving")
        except errors.JobQueueDrainError:
            raise http.HttpServiceUnavailable(
                "Job queue is drained, cannot submit")
        except rpcerr.NoMasterError as err:
            raise http.HttpBadGateway("Master seems to be unreachable: %s" %
                                      err)
        except rpcerr.PermissionError:
            raise http.HttpInternalServerError(
                "Internal error: no permission to"
                " connect to the master daemon")
        except rpcerr.TimeoutError as err:
            raise http.HttpGatewayTimeout("Timeout while talking to the master"
                                          " daemon: %s" % err)
Ejemplo n.º 3
0
            return cl.SubmitJob(op)
        except errors.JobQueueFull:
            raise http.HttpServiceUnavailable(
                "Job queue is full, needs archiving")
        except errors.JobQueueDrainError:
            raise http.HttpServiceUnavailable(
                "Job queue is drained, cannot submit")
        except rpcerr.NoMasterError, err:
            raise http.HttpBadGateway("Master seems to be unreachable: %s" %
                                      err)
        except rpcerr.PermissionError:
            raise http.HttpInternalServerError(
                "Internal error: no permission to"
                " connect to the master daemon")
        except rpcerr.TimeoutError, err:
            raise http.HttpGatewayTimeout("Timeout while talking to the master"
                                          " daemon: %s" % err)


def GetResourceOpcodes(cls):
    """Returns all opcodes used by a resource.

  """
    return frozenset(
        filter(None, (getattr(cls, method_attrs.opcode, None)
                      for method_attrs in OPCODE_ATTRS)))


def GetHandlerAccess(handler, method):
    """Returns the access rights for a method on a handler.

  @type handler: L{ResourceBase}