예제 #1
0
def get_commission(request, serial):
    data = request.GET
    client_key = unicode(request.component_instance)
    try:
        serial = int(serial)
    except ValueError:
        raise BadRequest("Serial should be an integer.")

    try:
        data = qh.get_commission(clientkey=client_key, serial=serial)
        status_code = 200
        return json_response(data, status_code)
    except qh_exception.NoCommissionError:
        return HttpResponse(status=404)
예제 #2
0
파일: quotas.py 프로젝트: konsP/synnefo
def get_commission(request, serial):
    data = request.GET
    client_key = unicode(request.component_instance)
    try:
        serial = int(serial)
    except ValueError:
        raise BadRequest("Serial should be an integer.")

    try:
        data = qh.get_commission(clientkey=client_key, serial=serial)
        status_code = 200
        return json_response(data, status_code)
    except qh_exception.NoCommissionError:
        return HttpResponse(status=404)
예제 #3
0
파일: tests.py 프로젝트: kins299/mycloud
    def test_010_qh(self):
        holder = 'h0'
        source = 'system'
        resource1 = 'r1'
        resource2 = 'r2'
        limit1 = 10
        limit2 = 20

        qh.set_quota([((holder, source, resource1), limit1),
                      ((holder, source, resource1), limit2)])

        r = qh.get_quota(holders=[holder], sources=[source],
                         resources=[resource1, resource2])
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})

        r = qh.get_quota()
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})

        # issuing commissions

        qh.set_quota([((holder, source, resource1), limit1),
                      ((holder, source, resource2), limit2)])

        s1 = self.issue_commission([((holder, source, resource1), limit1/2),
                                    ((holder, source, resource2), limit2)],
                                   name="initial")
        assertGreater(s1, 0)

        r = qh.get_commission(self.client, s1)
        provisions = [
            {'holder': holder,
             'source': source,
             'resource': resource1,
             'quantity': limit1/2,
             },
            {'holder': holder,
             'source': source,
             'resource': resource2,
             'quantity': limit2,
             }
        ]
        self.assertEqual(r['serial'], s1)
        ps = r['provisions']
        for p in ps:
            assertIn(p, provisions)

        with assertRaises(NoCommissionError):
            qh.get_commission(self.client, s1+1)

        r = qh.get_quota()
        self.assertEqual(r,
                         {(holder, source, resource1): (limit1, 0, limit1/2),
                          (holder, source, resource2): (limit2, 0, limit2),
                          })

        # commission exceptions

        with assertRaises(NoCapacityError) as cm:
            self.issue_commission([((holder, source, resource1), 1),
                                   ((holder, source, resource2), 1)])

        e = cm.exception
        provision = e.data['provision']
        self.assertEqual(provision['holder'], holder)
        self.assertEqual(provision['source'], source)
        self.assertEqual(provision['resource'], resource2)
        self.assertEqual(provision['quantity'], 1)
        self.assertEqual(e.data['usage'], limit2)
        self.assertEqual(e.data['limit'], limit2)

        with assertRaises(NoQuantityError) as cm:
            self.issue_commission([((holder, source, resource1), -1)])

        e = cm.exception
        provision = e.data['provision']
        self.assertEqual(provision['holder'], holder)
        self.assertEqual(provision['source'], source)
        self.assertEqual(provision['resource'], resource1)
        self.assertEqual(provision['quantity'], -1)
        self.assertEqual(e.data['usage'], 0)
        self.assertEqual(e.data['limit'], 0)

        with assertRaises(NoHoldingError) as cm:
            self.issue_commission([((holder, source, resource1), 1),
                                   (('nonex', source, resource1), 1)])

        e = cm.exception
        provision = e.data['provision']
        self.assertEqual(provision['holder'], 'nonex')
        self.assertEqual(provision['source'], source)
        self.assertEqual(provision['resource'], resource1)
        self.assertEqual(provision['quantity'], 1)

        r = qh.get_quota(holders=[holder])
        quotas = {(holder, source, resource1): (limit1, 0, limit1/2),
                  (holder, source, resource2): (limit2, 0, limit2),
                  }
        self.assertEqual(r, quotas)

        # resolve commission

        r = qh.get_pending_commissions(clientkey=self.client)
        self.assertEqual(len(r), 1)
        serial = r[0]
        self.assertEqual(serial, s1)
        r = qh.resolve_pending_commission(self.client, serial)
        self.assertEqual(r, True)
        r = qh.get_pending_commissions(clientkey=self.client)
        self.assertEqual(r, [])
        r = qh.resolve_pending_commission(self.client, serial)
        self.assertEqual(r, False)

        r = qh.get_quota(holders=[holder])
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2),
                  (holder, source, resource2): (limit2, limit2, limit2),
                  }
        self.assertEqual(r, quotas)

        logs = models.ProvisionLog.objects.filter(serial=serial)
        self.assertEqual(len(logs), 2)
        log1 = filter(lambda p: p.resource == resource1
                      and p.delta_quantity == limit1/2
                      and p.usage_min == limit1/2, logs)
        self.assertEqual(len(log1), 1)

        # resolve commissions

        serial = self.issue_commission([((holder, source, resource1), 1),
                                        ((holder, source, resource2), -1)])

        r = qh.get_quota(holders=[holder])
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2+1),
                  (holder, source, resource2): (limit2, limit2-1, limit2),
                  }
        self.assertEqual(r, quotas)

        r = qh.resolve_pending_commission(self.client, serial, accept=False)
        self.assertEqual(r, True)

        serial1 = self.issue_commission([((holder, source, resource1), 1),
                                         ((holder, source, resource2), -1)])

        serial2 = self.issue_commission([((holder, source, resource1), 1),
                                         ((holder, source, resource2), -1)])

        serial3 = self.issue_commission([((holder, source, resource1), 1),
                                         ((holder, source, resource2), -1)])

        r = qh.resolve_pending_commissions(clientkey=self.client,
                                           accept_set=[serial1, serial2, 0],
                                           reject_set=[serial2, serial3])
        self.assertEqual(r, ([serial1], [serial3], [0], [serial2]))

        r = qh.get_pending_commissions(clientkey=self.client)
        self.assertEqual(r, [serial2])

        # forced commission

        r = qh.get_quota(holders=[holder])
        quotas = {
            (holder, source, resource1): (limit1, limit1/2+1, limit1/2+2),
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
        }
        self.assertEqual(r, quotas)

        with assertRaises(NoCapacityError):
            self.issue_commission(
                [((holder, source, resource1), limit1/2+1)])

        serial = self.issue_commission(
            [((holder, source, resource1), limit1/2+1)],
            force=True)

        r = qh.resolve_pending_commission(self.client, serial, accept=True)
        self.assertEqual(r, True)

        r = qh.get_quota(holders=[holder])
        quotas = {
            (holder, source, resource1): (limit1, limit1+2, limit1+3),
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
        }
        self.assertEqual(r, quotas)

        with assertRaises(NoQuantityError):
            self.issue_commission(
                [((holder, source, resource1), -2*limit1)],
                force=True)

        # release off upper limit

        serial = self.issue_commission([((holder, source, resource1), -1)])
        r = qh.resolve_pending_commission(self.client, serial)
        self.assertEqual(r, True)

        r = qh.get_quota(holders=[holder], resources=[resource1])
        quotas = {
            (holder, source, resource1): (limit1, limit1+1, limit1+2),
        }
        self.assertEqual(r, quotas)