示例#1
0
文件: quota.py 项目: darian19/what
def checkQuotaForInstanceAndRaise(conn, instanceName):
  """ Perform instance quota check and raise QuotaError exception if request to
  create a model for `instanceName` would result in a new "instance" being
  created beyond quota.

  This function answers the question:

    Is there room to add a new model for a new, or existing instance?

  And raises an exception if the answer is no.

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.Connection
  :param instanceName: Instance name
  :type instanceName str:

  :raises: YOMP.app.exceptions.QuotaError
  """
  instanceCount = repository.getInstanceCount(conn)
  instanceQuota = Quota.getInstanceQuota()

  if instanceCount >= instanceQuota:
    if not repository.listMetricIDsForInstance(conn, instanceName):
      raise QuotaError(
        "Server limit exceeded; edition=%s; count=%i; limit=%i." % (
          product.get("edition", "type").title(), instanceCount,
          instanceQuota))
示例#2
0
文件: quota.py 项目: bopopescu/what
def checkQuotaForInstanceAndRaise(conn, instanceName):
    """ Perform instance quota check and raise QuotaError exception if request to
  create a model for `instanceName` would result in a new "instance" being
  created beyond quota.

  This function answers the question:

    Is there room to add a new model for a new, or existing instance?

  And raises an exception if the answer is no.

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.Connection
  :param instanceName: Instance name
  :type instanceName str:

  :raises: YOMP.app.exceptions.QuotaError
  """
    instanceCount = repository.getInstanceCount(conn)
    instanceQuota = Quota.getInstanceQuota()

    if instanceCount >= instanceQuota:
        if not repository.listMetricIDsForInstance(conn, instanceName):
            raise QuotaError(
                "Server limit exceeded; edition=%s; count=%i; limit=%i." %
                (product.get("edition",
                             "type").title(), instanceCount, instanceQuota))
示例#3
0
    def DELETE(self):
        """
    Delete models for multiple instances

    ::

        DELETE /_instances

    DELETE data:

    ::

        [
            "{region}/{namespace}/{instanceId}",
            ...
        ]

    Returns:

    ::

        {
            "result": "success"
        }
    """
        try:
            instances = json.loads(web.data())
        except:
            raise InvalidRequestResponse({"result": "Invalid request"})

        if not instances:
            raise InvalidRequestResponse(
                {"result": ("Missing instances in DELETE"
                            " request")})

        deleted = []
        if instances:
            for server in instances:
                if server.count("/") == 4:
                    (lhs, _, identifier) = server.rpartition("/")
                    (regionAndNamespace, _, _) = lhs.rpartition("/")
                    serverSansDimension = regionAndNamespace + "/" + identifier
                else:
                    serverSansDimension = server
                with web.ctx.connFactory() as conn:
                    modelIds = repository.listMetricIDsForInstance(
                        conn, serverSansDimension)
                if modelIds:
                    for modelId in modelIds:
                        ModelHandler.deleteModel(modelId)
                    deleted.append(server)

        if instances == deleted:
            self.addStandardHeaders()
            return encodeJson({'result': 'success'})

        raise web.notfound("Not able to delete %s" %
                           encodeJson(list(set(instances) - set(deleted))))
示例#4
0
  def DELETE(self):
    """
    Delete models for multiple instances

    ::

        DELETE /_instances

    DELETE data:

    ::

        [
            "{region}/{namespace}/{instanceId}",
            ...
        ]

    Returns:

    ::

        {
            "result": "success"
        }
    """
    try:
      instances = json.loads(web.data())
    except:
      raise InvalidRequestResponse({"result": "Invalid request"})

    if not instances:
      raise InvalidRequestResponse({"result": ("Missing instances in DELETE"
                                               " request")})

    deleted = []
    if instances:
      for server in instances:
        if server.count("/") == 4:
          (lhs, _, identifier) = server.rpartition("/")
          (regionAndNamespace, _, _) = lhs.rpartition("/")
          serverSansDimension = regionAndNamespace + "/" + identifier
        else:
          serverSansDimension = server
        with web.ctx.connFactory() as conn:
          modelIds = repository.listMetricIDsForInstance(conn,
                                                         serverSansDimension)
        if modelIds:
          for modelId in modelIds:
            ModelHandler.deleteModel(modelId)
          deleted.append(server)

    if instances == deleted:
      self.addStandardHeaders()
      return encodeJson({'result': 'success'})

    raise web.notfound("Not able to delete %s" %
                       encodeJson(list(set(instances)-set(deleted))))