Пример #1
0
    def create(self,
               context,
               size,
               name,
               description,
               snapshot=None,
               image_id=None,
               volume_type=None,
               metadata=None,
               availability_zone=None):
        client = cinderclient(context)

        if snapshot is not None:
            snapshot_id = snapshot['id']
        else:
            snapshot_id = None

        kwargs = dict(snapshot_id=snapshot_id,
                      volume_type=volume_type,
                      user_id=context.user_id,
                      project_id=context.project_id,
                      availability_zone=availability_zone,
                      metadata=metadata,
                      imageRef=image_id)

        if isinstance(client, v1_client.Client):
            kwargs['display_name'] = name
            kwargs['display_description'] = description
        else:
            kwargs['name'] = name
            kwargs['description'] = description

        try:
            item = client.volumes.create(size, **kwargs)
            return _untranslate_volume_summary_view(context, item)
        except cinder_exception.OverLimit:
            raise exception.OverQuota(overs='volumes')
        except (cinder_exception.BadRequest,
                keystone_exception.BadRequest) as e:
            raise exception.InvalidInput(reason=e)
Пример #2
0
    def create(self,
               context,
               size,
               name,
               description,
               snapshot=None,
               image_id=None,
               volume_type=None,
               metadata=None,
               availability_zone=None):

        if snapshot is not None:
            snapshot_id = snapshot['id']
        else:
            snapshot_id = None

        kwargs = dict(snapshot_id=snapshot_id,
                      volume_type=volume_type,
                      user_id=context.user_id,
                      project_id=context.project_id,
                      availability_zone=availability_zone,
                      metadata=metadata,
                      imageRef=image_id)

        version = get_cinder_client_version(context)
        if version == '1':
            kwargs['display_name'] = name
            kwargs['display_description'] = description
        elif version == '2':
            kwargs['name'] = name
            kwargs['description'] = description

        try:
            item = cinderclient(context).volumes.create(size, **kwargs)
            return _untranslate_volume_summary_view(context, item)
        except cinder_exception.OverLimit:
            raise exception.OverQuota(overs='volumes')
        except cinder_exception.BadRequest as e:
            raise exception.InvalidInput(reason=unicode(e))
Пример #3
0
    def limit_check(self, context, resources, values):
        """Check simple quota limits.

        For limits--those quotas for which there is no usage
        synchronization function--this method checks that a set of
        proposed values are permitted by the limit restriction.

        This method will raise a QuotaResourceUnknown exception if a
        given resource is unknown or if it is not a simple limit
        resource.

        If any of the proposed values is over the defined quota, an
        OverQuota exception will be raised with the sorted list of the
        resources which are too high.  Otherwise, the method returns
        nothing.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        :param values: A dictionary of the values to check against the
                       quota.
        """

        # Ensure no value is less than zero
        unders = [key for key, val in values.items() if val < 0]
        if unders:
            raise exception.InvalidQuotaValue(unders=sorted(unders))

        # Get the applicable quotas
        quotas = self._get_quotas(context, resources, values.keys(),
                                  has_sync=False)

        # Check the quotas and construct a list of the resources that
        # would be put over limit by the desired values
        overs = [key for key, val in values.items()
                 if quotas[key] >= 0 and quotas[key] < val]
        if overs:
            raise exception.OverQuota(overs=sorted(overs), quotas=quotas,
                                      usages={})
Пример #4
0
    def test_resize_quota_exceeds_fails(self):
        self.mox.StubOutWithMock(flavors, 'get_flavor_by_flavor_id')
        self.mox.StubOutWithMock(self.compute_api, '_upsize_quota_delta')
        self.mox.StubOutWithMock(self.compute_api, '_reserve_quota_delta')
        # Should never reach these.
        self.mox.StubOutWithMock(self.compute_api, 'update')
        self.mox.StubOutWithMock(quota.QUOTAS, 'commit')
        self.mox.StubOutWithMock(self.compute_api, '_record_action_start')
        self.mox.StubOutWithMock(self.compute_api.compute_task_api,
                                 'migrate_server')

        fake_inst = obj_base.obj_to_primitive(self._create_instance_obj())
        current_flavor = flavors.extract_flavor(fake_inst)
        fake_flavor = dict(id=200,
                           flavorid='flavor-id',
                           name='foo',
                           disabled=False)
        flavors.get_flavor_by_flavor_id(
            'flavor-id', read_deleted='no').AndReturn(fake_flavor)
        deltas = dict(resource=0)
        self.compute_api._upsize_quota_delta(self.context, fake_flavor,
                                             current_flavor).AndReturn(deltas)
        usage = dict(in_use=0, reserved=0)
        over_quota_args = dict(quotas={'resource': 0},
                               usages={'resource': usage},
                               overs=['resource'])
        self.compute_api._reserve_quota_delta(
            self.context, deltas, project_id=fake_inst['project_id']).AndRaise(
                exception.OverQuota(**over_quota_args))

        self.mox.ReplayAll()

        self.assertRaises(exception.TooManyInstances,
                          self.compute_api.resize,
                          self.context,
                          fake_inst,
                          flavor_id='flavor-id')
Пример #5
0
    def test_create_instance_over_quota_during_recheck(self,
                                                       check_deltas_mock):
        self.stub_out('nova.tests.unit.image.fake._FakeImageService.show',
                      self.fake_show)

        # Simulate a race where the first check passes and the recheck fails.
        fake_quotas = {'instances': 5, 'cores': 10, 'ram': 4096}
        fake_headroom = {'instances': 5, 'cores': 10, 'ram': 4096}
        fake_usages = {'instances': 5, 'cores': 10, 'ram': 4096}
        exc = exception.OverQuota(overs=['instances'],
                                  quotas=fake_quotas,
                                  headroom=fake_headroom,
                                  usages=fake_usages)
        check_deltas_mock.side_effect = [None, exc]

        inst_type = flavors.get_default_flavor()
        # Try to create 3 instances.
        self.assertRaises(exception.QuotaError,
                          self.compute_api.create,
                          self.context,
                          inst_type,
                          self.fake_image['id'],
                          min_count=3)

        project_id = self.context.project_id

        self.assertEqual(2, check_deltas_mock.call_count)
        call1 = mock.call(self.context, {
            'instances': 3,
            'cores': inst_type.vcpus * 3,
            'ram': inst_type.memory_mb * 3
        },
                          project_id,
                          user_id=None,
                          check_project_id=project_id,
                          check_user_id=None)
        call2 = mock.call(self.context, {
            'instances': 0,
            'cores': 0,
            'ram': 0
        },
                          project_id,
                          user_id=None,
                          check_project_id=project_id,
                          check_user_id=None)
        check_deltas_mock.assert_has_calls([call1, call2])

        # Verify we removed the artifacts that were added after the first
        # quota check passed.
        instances = objects.InstanceList.get_all(self.context)
        self.assertEqual(0, len(instances))
        build_requests = objects.BuildRequestList.get_all(self.context)
        self.assertEqual(0, len(build_requests))

        @db_api.api_context_manager.reader
        def request_spec_get_all(context):
            return context.session.query(api_models.RequestSpec).all()

        request_specs = request_spec_get_all(self.context)
        self.assertEqual(0, len(request_specs))

        instance_mappings = objects.InstanceMappingList.get_by_project_id(
            self.context, project_id)
        self.assertEqual(0, len(instance_mappings))
Пример #6
0
 def test_create_volume_backed_image_cinder_over_quota(self):
     self.assertRaises(
         webob.exc.HTTPForbidden,
         self._do_test_create_volume_backed_image, {},
         mock_vol_create_side_effect=exception.OverQuota(
             overs='snapshot'))
Пример #7
0
 def fake_reserve(context, **deltas):
     kwargs = dict(overs=[resource],
                   quotas=dict(gigabytes=1000, volumes=10),
                   usages=dict(gigabytes=dict(reserved=1, in_use=999),
                               volumes=dict(reserved=1, in_use=9)))
     raise exception.OverQuota(**kwargs)
Пример #8
0
 def test_volume_create_bad_quota(self):
     self._test_volume_translate_exception(
         exception.OverQuota(overs='fake'), webob.exc.HTTPForbidden)