def test_run_with_quotas(self): f = Mock() run_with_quotas(FAKE_TENANT1, {'instances': 1, 'volumes': 5}, f) self.assertTrue(QUOTAS.reserve.called) self.assertTrue(QUOTAS.commit.called) self.assertFalse(QUOTAS.rollback.called) self.assertTrue(f.called)
def test_run_with_quotas(self): f = Mock() run_with_quotas(FAKE_TENANT1, {'instances': 1, 'volumes': 5}, f) self.assertTrue(QUOTAS.reserve.called) self.assertTrue(QUOTAS.commit.called) self.assertFalse(QUOTAS.rollback.called) self.assertTrue(f.called)
def create(cls, context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id): def _create_resources(): client = create_nova_client(context) security_groups = None try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) if backup_id is not None: backup_info = Backup.get_by_id(backup_id) if backup_info.is_running: raise exception.BackupNotCompleteError(backup_id=backup_id) location = backup_info.location LOG.info(_("Checking if backup exist in '%s'") % location) if not Backup.check_object_exist(context, location): raise exception.BackupFileNotFound(location=location) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug(_("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups, backup_id) return SimpleInstance(context, db_info, service_status) validate_volume_size(volume_size) return run_with_quotas(context.tenant, {'instances': 1, 'volumes': volume_size}, _create_resources)
def create(cls, context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id): def _create_resources(): client = create_nova_client(context) security_groups = None try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) if backup_id is not None: backup_info = Backup.get_by_id(backup_id) if backup_info.is_running: raise exception.BackupNotCompleteError(backup_id=backup_id) location = backup_info.location LOG.info(_("Checking if backup exist in '%s'") % location) if not Backup.check_object_exist(context, location): raise exception.BackupFileNotFound(location=location) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug( _("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups, backup_id) return SimpleInstance(context, db_info, service_status) validate_volume_size(volume_size) return run_with_quotas(context.tenant, { 'instances': 1, 'volumes': volume_size }, _create_resources)
def delete(self): def _delete_resources(): if self.is_building: raise exception.UnprocessableEntity("Instance %s is not ready." % self.id) LOG.debug(_(" ... deleting compute id = %s") % self.db_info.compute_instance_id) LOG.debug(_(" ... setting status to DELETING.")) self.update_db(task_status=InstanceTasks.DELETING) task_api.API(self.context).delete_instance(self.id) return run_with_quotas(self.tenant_id, {'instances': -1, 'volumes': -self.volume_size}, _delete_resources)
def delete(self): def _delete_resources(): if self.is_building: raise exception.UnprocessableEntity( "Instance %s is not ready." % self.id) LOG.debug( _(" ... deleting compute id = %s") % self.db_info.compute_instance_id) LOG.debug(_(" ... setting status to DELETING.")) self.update_db(task_status=InstanceTasks.DELETING) task_api.API(self.context).delete_instance(self.id) deltas = {'instances': -1} if CONF.reddwarf_volume_support: deltas['volumes'] = -self.volume_size return run_with_quotas(self.tenant_id, deltas, _delete_resources)
def delete(cls, context, backup_id): """ update Backup table on deleted flag for given Backup :param cls: :param context: context containing the tenant id and token :param backup_id: Backup uuid :return: """ def _delete_resources(): backup = cls.get_by_id(backup_id) if backup.is_running: msg = ("Backup %s cannot be delete because it is running." % backup_id) raise exception.UnprocessableEntity(msg) cls.verify_swift_auth_token(context) api.API(context).delete_backup(backup_id) return run_with_quotas(context.tenant, {'backups': -1}, _delete_resources)
def resize_volume(self, new_size): def _resize_resources(): self.validate_can_perform_action() LOG.info("Resizing volume of instance %s..." % self.id) if not self.volume_size: raise exception.BadRequest("Instance %s has no volume." % self.id) old_size = self.volume_size if int(new_size) <= old_size: msg = ("The new volume 'size' must be larger than the current " "volume size of '%s'") raise exception.BadRequest(msg % old_size) # Set the task to Resizing before sending off to the taskmanager self.update_db(task_status=InstanceTasks.RESIZING) task_api.API(self.context).resize_volume(new_size, self.id) new_size_l = long(new_size) validate_volume_size(new_size_l) return run_with_quotas(self.tenant_id, {'volumes': new_size_l - self.volume_size}, _resize_resources)
def delete(cls, context, backup_id): """ update Backup table on deleted flag for given Backup :param cls: :param context: context containing the tenant id and token :param backup_id: Backup uuid :return: """ def _delete_resources(): backup = cls.get_by_id(backup_id) if backup.is_running: msg = ("Backup %s cannot be delete because it is running." % backup_id) raise exception.UnprocessableEntity(msg) cls.verify_swift_auth_token(context) api.API(context).delete_backup(backup_id) return run_with_quotas(context.tenant, {'backups': -1}, _delete_resources)
def resize_volume(self, new_size): def _resize_resources(): self.validate_can_perform_action() LOG.info("Resizing volume of instance %s..." % self.id) if not self.volume_size: raise exception.BadRequest("Instance %s has no volume." % self.id) old_size = self.volume_size if int(new_size) <= old_size: msg = ("The new volume 'size' must be larger than the current " "volume size of '%s'") raise exception.BadRequest(msg % old_size) # Set the task to Resizing before sending off to the taskmanager self.update_db(task_status=InstanceTasks.RESIZING) task_api.API(self.context).resize_volume(new_size, self.id) new_size_l = long(new_size) validate_volume_size(new_size_l) return run_with_quotas(self.tenant_id, {'volumes': new_size_l - self.volume_size}, _resize_resources)
def create(cls, context, instance, name, description=None): """ create db record for Backup :param cls: :param context: tenant_id included :param instance: :param name: :param description: :return: """ def _create_resources(): # parse the ID from the Ref instance_id = utils.get_id_from_href(instance) # verify that the instance exist and can perform actions from reddwarf.instance.models import Instance instance_model = Instance.load(context, instance_id) instance_model.validate_can_perform_action() cls.verify_swift_auth_token(context) try: db_info = DBBackup.create(name=name, description=description, tenant_id=context.tenant, state=BackupState.NEW, instance_id=instance_id, deleted=False) except exception.InvalidModelError as ex: LOG.exception("Unable to create Backup record:") raise exception.BackupCreationError(str(ex)) api.API(context).create_backup(db_info.id, instance_id) return db_info return run_with_quotas(context.tenant, {'backups': 1}, _create_resources)
def create(cls, context, instance, name, description=None): """ create db record for Backup :param cls: :param context: tenant_id included :param instance: :param name: :param description: :return: """ def _create_resources(): # parse the ID from the Ref instance_id = utils.get_id_from_href(instance) # verify that the instance exist and can perform actions from reddwarf.instance.models import Instance instance_model = Instance.load(context, instance_id) instance_model.validate_can_perform_action() cls.verify_swift_auth_token(context) try: db_info = DBBackup.create(name=name, description=description, tenant_id=context.tenant, state=BackupState.NEW, instance_id=instance_id, deleted=False) except exception.InvalidModelError as ex: LOG.exception("Unable to create Backup record:") raise exception.BackupCreationError(str(ex)) api.API(context).create_backup(db_info.id, instance_id) return db_info return run_with_quotas(context.tenant, {'backups': 1}, _create_resources)