def _paginate(context, query, model, session, filters, pagination_params, project_only=False): # NOTE(sigmavirus24) Retrieve the instance of the model represented by the # marker. try: marker = _marker_from(context, session, model, pagination_params['marker'], project_only) except sa_exc.NoResultFound: raise exceptions.BadRequest(message='Marker "{}" does not exist'. format(pagination_params['marker'])) except Exception as err: raise exceptions.UnknownException(message=err) filters.setdefault('sort_keys', ['created_at', 'id']) filters.setdefault('sort_dir', 'asc') # Retrieve the results based on the marker and the limit try: results = db_utils.paginate_query( query, model, limit=pagination_params['limit'], sort_keys=filters['sort_keys'], sort_dir=filters['sort_dir'], marker=marker, ).all() except sa_exc.NoResultFound: raise exceptions.NotFound() except db_exc.InvalidSortKey as invalid_key: raise exceptions.BadRequest( message='"{}" is an invalid sort key'.format(invalid_key.key)) except Exception as err: raise exceptions.UnknownException(message=err) try: links = _link_params_for( query, model, filters, pagination_params, marker, results, ) except Exception as err: raise exceptions.UnknownException(message=err) return results, links
def validate(self, value): value = self.type_convert(value) errors = sorted(e.message for e in self.validator.iter_errors(value)) if errors: msg = "The request included the following errors:\n- {}".format( "\n- ".join(errors)) raise exceptions.BadRequest(message=msg) return merge_default(self.validator.schema, value)
def hosts_update(context, host_id, values): """Update an existing host.""" session = get_session() with session.begin(): host_devices = with_polymorphic(models.Device, '*') query = model_query(context, host_devices, session=session, project_only=True) query = query.filter_by(id=host_id) host_ref = query.with_for_update().one() try: host_ref.update(values) except exceptions.ParentIDError as e: raise exceptions.BadRequest(message=str(e)) host_ref.save(session) return host_ref
def network_devices_update(context, network_device_id, values): """Update existing network device""" session = get_session() with session.begin(): device = with_polymorphic(models.Device, '*') query = model_query(context, device, session=session, project_only=True) query = query.filter_by(type='network_devices') query = query.filter_by(id=network_device_id) network_device_ref = query.with_for_update().one() try: network_device_ref.update(values) except exceptions.ParentIDError as e: raise exceptions.BadRequest(message=str(e)) network_device_ref.save(session) return network_device_ref