def delete(self, _id): """Marks volume types as deleted. :param _id: id of volume type to be deleted """ context = t_context.extract_context_from_environ() if not context.is_admin: return utils.format_cinder_error( 403, _("Policy doesn't allow volume_extension:types_manage " "to be performed.")) session = core.get_session() with session.begin(): try: db_api.volume_type_get(context, _id, session) except exceptions.VolumeTypeNotFound as e: return utils.format_cinder_error(404, e.message) try: db_api.volume_type_delete(context, _id, session) except Exception as e: LOG.exception( _LE('Fail to update volume type: %(id)s,' '%(exception)s'), { 'id': _id, 'exception': e }) return utils.format_cinder_error( 500, _('Fail to delete volume type.')) pecan.response.status = 202 return pecan.response
def quota_class_create(context, class_name, resource, limit): quota_class_ref = models.QuotaClasses() quota_class_ref.class_name = class_name quota_class_ref.resource = resource quota_class_ref.hard_limit = limit session = core.get_session() with session.begin(): quota_class_ref.save(session) return quota_class_ref
def quota_create(context, project_id, resource, limit, allocated=0): quota_ref = models.Quotas() quota_ref.project_id = project_id quota_ref.resource = resource quota_ref.hard_limit = limit if allocated: quota_ref.allocated = allocated session = core.get_session() with session.begin(): quota_ref.save(session) return quota_ref
def volume_type_update(context, volume_type_id, values): """Update volume type by volume_type_id. :param volume_type_id: id of volume type to be updated :param values: dictionary of values to be updated :returns: updated volume type """ session = core.get_session() with session.begin(): try: # Check it exists volume_type_ref = _volume_type_ref_get(context, volume_type_id, session) if not volume_type_ref: raise exceptions.VolumeTypeNotFound(type_id=volume_type_id) # No description change if values['description'] is None: del values['description'] # No is_public change if values['is_public'] is None: del values['is_public'] # No name change if values['name'] is None: del values['name'] else: # Volume type name is unique. If change to a name that # belongs to a different volume_type , it should be # prevented. check_vol_type = None try: check_vol_type = \ volume_type_get_by_name(context, values['name'], session=session) except exceptions.VolumeTypeNotFoundByName: pass else: if check_vol_type.get('id') != volume_type_id: raise exceptions.VolumeTypeExists(id=values['name']) volume_type_ref.update(values) volume_type_ref.save(session=session) except Exception: raise exceptions.VolumeTypeUpdateFailed(id=volume_type_id) return _dict_with_extra_specs_if_authorized(context, volume_type_ref)
def volume_type_get_all(context, inactive=False, filters=None, list_result=False): """Returns a dict describing all volume_types with name as key. :param context: context to query under :param inactive: Pass true as argument if you want deleted volume types returned also. :param filters: dictionary of filters; values that are in lists, tuples, or sets cause an 'IN' operation, while exact matching is used for other values, see _process_volume_type_filters function for more information :param list_result: For compatibility, if list_result = True, return a list instead of dict. :returns: list/dict of matching volume types """ read_deleted = 'yes' if inactive else 'no' session = core.get_session() with session.begin(): filters = filters or {} filters['context'] = context # Generate the query query = _volume_type_get_query(context, session=session, read_deleted=read_deleted) query = _process_volume_types_filters(query, filters) # No volume types would match, return empty dict or list if query is None: if list_result: return [] return {} rows = query.all() if list_result: result = [_dict_with_extra_specs_if_authorized(context, row) for row in rows] return result result = {row['name']: _dict_with_extra_specs_if_authorized(context, row) for row in rows} return result
def session(self): if not self._session: self._session = core.get_session() return self._session
def post(self, **kw): """Creates volume types.""" context = t_context.extract_context_from_environ() if not context.is_admin: return utils.format_cinder_error( 403, _("Policy doesn't allow volume_extension:types_manage " "to be performed.")) if 'volume_type' not in kw: return utils.format_cinder_error( 400, _("Missing required element 'volume_type' in " "request body.")) projects = [] if self.tenant_id is not None: projects = [self.tenant_id] vol_type = kw['volume_type'] name = vol_type.get('name', None) description = vol_type.get('description') specs = vol_type.get('extra_specs', {}) is_public = vol_type.pop('os-volume-type-access:is_public', True) if name is None or len(name.strip()) == 0: return utils.format_cinder_error( 400, _("Volume type name can not be empty.")) try: utils.check_string_length(name, 'Type name', min_len=1, max_len=255) except exceptions.InvalidInput as e: return utils.format_cinder_error(400, e.message) if description is not None: try: utils.check_string_length(description, 'Type description', min_len=0, max_len=255) except exceptions.InvalidInput as e: return utils.format_cinder_error(400, e.message) if not utils.is_valid_boolstr(is_public): msg = _("Invalid value '%(is_public)s' for is_public. " "Accepted values: True or False.") % { 'is_public': is_public } return utils.format_cinder_error(400, msg) vol_type['extra_specs'] = specs vol_type['is_public'] = is_public vol_type['id'] = uuidutils.generate_uuid() session = core.get_session() with session.begin(): try: db_api.volume_type_get_by_name(context, vol_type['name'], session) return utils.format_cinder_error( 409, _("Volume Type %(id)s already exists.") % {'id': vol_type['id']}) except exceptions.VolumeTypeNotFoundByName: pass try: extra_specs = vol_type['extra_specs'] vol_type['extra_specs'] = \ self._metadata_refs(vol_type.get('extra_specs'), models.VolumeTypeExtraSpecs) volume_type_ref = models.VolumeTypes() volume_type_ref.update(vol_type) session.add(volume_type_ref) for project in set(projects): access_ref = models.VolumeTypeProjects() access_ref.update({ "volume_type_id": volume_type_ref.id, "project_id": project }) access_ref.save(session=session) except Exception as e: LOG.exception( _LE('Fail to create volume type: %(name)s,' '%(exception)s'), { 'name': vol_type['name'], 'exception': e }) return utils.format_cinder_error( 500, _('Fail to create volume type')) vol_type['extra_specs'] = extra_specs return {'volume_type': vol_type}