Пример #1
0
 def create(self, req, body, tenant_id):
     
     # find the service id (cant be done yet at startup due to
     # inconsitencies w/ the load app paste
     # TODO(hub-cap): figure out how to get this to work in __init__ time
     # TODO(hub-cap): The problem with this in __init__ is that the paste
     #   config is generated w/ the same config file as the db flags that
     #   are needed for init. These need to be split so the db can be init'd
     #   w/o the paste stuff. Since the paste stuff inits the
     #   database.service module, it is a chicken before the egg problem.
     #   Simple refactor will fix it and we can move this into the __init__
     #   code. Or maybe we shouldnt due to the nature of changing images.
     #   This needs discussion.
     # TODO(hub-cap): turn this into middleware
     LOG.info("Creating a database instance for tenant '%s'" % tenant_id)
     LOG.info("req : '%s'\n\n" % req)
     LOG.info("body : '%s'\n\n" % body)
     context = rd_context.ReddwarfContext(
                       auth_tok=req.headers["X-Auth-Token"],
                       tenant=tenant_id)
     
     try:
         num_instances = self._check_instance_quota(context, 1)
     except exception.QuotaError, e:
         LOG.exception("Quota Error encountered for tenant %s" % tenant_id)
         maximum_instances_allowed = quota.get_tenant_quotas(context, context.tenant)['instances']
         return wsgi.Result(errors.wrap(errors.Instance.QUOTA_EXCEEDED, "You are only allowed to create %s instances on you account." % maximum_instances_allowed), 413)
Пример #2
0
    def test_get_default_quotas(self):
        """Tests the default quota for instances and snapshots is 0,
        unless there exist database values that override them"""

        self.mock.StubOutWithMock(models.Quota, "find_all")
        models.Quota.find_all(tenant_id="tenant_id", deleted=False).AndReturn(None)

        self.mock.ReplayAll()

        quotas = quota.get_tenant_quotas(self.DUMMY_CONTEXT, "tenant_id")
        self.assertTrue(
            quotas["instances"] == 0,
            "Expected 0 as default quota limit for instances, instead got %s" % quotas["instances"],
        )
        self.assertTrue(
            quotas["snapshots"] == 0,
            "Expected 0 as default quota limit for snapshots, instead got %s" % quotas["snapshots"],
        )
Пример #3
0
    def test_get_default_quotas_with_db_overrides(self):
        """Tests that the database quota values for a given tenant
        do override the default 0's"""

        default_quotas = [
            {"tenant_id": "12345", "hard_limit": 3, "resource": "instances"},
            {"tenant_id": "12345", "hard_limit": 10, "resource": "snapshots"},
        ]

        self.mock.StubOutWithMock(models.Quota, "find_all")
        models.Quota.find_all(tenant_id="tenant_id", deleted=False).AndReturn(default_quotas)

        self.mock.ReplayAll()

        quotas = quota.get_tenant_quotas(self.DUMMY_CONTEXT, "tenant_id")
        self.assertTrue(
            quotas["instances"] == 3, "Expected 3 as quota for instances, instead got %s" % quotas["instances"]
        )
        self.assertTrue(
            quotas["snapshots"] == 10, "Expected 10 as quota for snapshots, instead got %s" % quotas["snapshots"]
        )
Пример #4
0
                LOG.exception("Error creating new instance")
                return wsgi.Result(errors.wrap(errors.Instance.MALFORMED_BODY), 500)

        # Extract volume size info from the request and check Quota
        try:
            volume_size = self._extract_volume_size(body)
            if volume_size is None:
                volume_size = config.Config.get('default_volume_size', 20)
            
            self._check_volume_size_quota(context, volume_size)
        except exception.BadValue, e:
            LOG.exception("Bad value for volume size")
            return wsgi.Result(errors.wrap(errors.Instance.MALFORMED_BODY, 'Invalid volume size'), 400)
        except exception.QuotaError, e:
            LOG.exception("Unable to allocate volume, Volume Size Quota has been exceeded")
            maximum_snapshots_allowed = quota.get_tenant_quotas(context, context.tenant)['volume_space']
            return wsgi.Result(errors.wrap(errors.Instance.VOLUME_QUOTA_EXCEEDED, "You are only allowed to allocate %s GBs of Volume Space for your account." % maximum_snapshots_allowed), 413)
        except exception.ReddwarfError, e:
            LOG.exception()
            return wsgi.Result(errors.wrap(errors.Instance.REDDWARF_CREATE), 500)
        
        # Extract flavor info from the request
        try:
            flavor_ref = body['instance']['flavorRef']
        except KeyError, e:
            LOG.info("The body does not contain an [instance][flavorRef] key - using default flavor of medium")
            try:
                flavor_model = models.ServiceFlavor.find_by(service_name="database", flavor_name='large', deleted=False)
            except:
                LOG.exception("The ServiceFlavor table doesn't contain a default flavor named 'medium'!")
                return wsgi.Result(errors.wrap(errors.Instance.FLAVOR_NOT_FOUND_CREATE), 404)