From 7f8d4fb008b3d047aa1e6279538fb91cd44c1d7d Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Thu, 7 Sep 2023 16:55:09 +0530 Subject: [PATCH 1/6] Add 'presets' table with alembic using postgres database 'tapi_db' Signed-off-by: Vallari Agrawal --- README.md | 20 +++ alembic.ini | 115 ++++++++++++++++++ alembic/README | 1 + alembic/env.py | 79 ++++++++++++ alembic/script.py.mako | 26 ++++ .../0684e3c62926_add_presets_table.py | 40 ++++++ setup.cfg | 3 + src/teuthology_api/models/__init__.py | 21 ++++ src/teuthology_api/models/presets.py | 13 ++ start_container.sh | 1 + 10 files changed, 319 insertions(+) create mode 100644 alembic.ini create mode 100644 alembic/README create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 alembic/versions/0684e3c62926_add_presets_table.py create mode 100644 src/teuthology_api/models/__init__.py create mode 100644 src/teuthology_api/models/presets.py diff --git a/README.md b/README.md index f30c764..b03ae05 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,34 @@ A REST API to execute [teuthology commands](https://docs.ceph.com/projects/teuth TEUTHOLOGY_API_SERVER_HOST: 0.0.0.0 TEUTHOLOGY_API_SERVER_PORT: 8080 PADDLES_URL: http://localhost:8080 + TEUTHOLOGY_API_SQLALCHEMY_URL: postgresql+psycopg2://admin:password@tapi_db:5432/tapi_db depends_on: - teuthology - paddles + - tapi_db links: - teuthology - paddles + - tapi_db healthcheck: test: [ "CMD", "curl", "-f", "http://0.0.0.0:8082" ] + tapi_db: + image: postgres:14 + healthcheck: + test: [ "CMD", "pg_isready", "-q", "-d", "tapi_db", "-U", "admin" ] + timeout: 5s + interval: 10s + retries: 2 + environment: + - POSTGRES_USER=root + - POSTGRES_PASSWORD=password + - APP_DB_USER=admin + - APP_DB_PASS=password + - APP_DB_NAME=tapi_db + volumes: + - ./db:/docker-entrypoint-initdb.d/ + ports: + - 5433:5432 ``` [optional] For developement use: Add following things in `teuthology_api` container: diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..608ca2b --- /dev/null +++ b/alembic.ini @@ -0,0 +1,115 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python-dateutil library that can be +# installed by adding `alembic[tz]` to the pip requirements +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/alembic/README b/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..e0d2d4b --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,79 @@ +from logging.config import fileConfig +import os + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from alembic import context + +from src.teuthology_api import models + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +config.set_main_option("sqlalchemy.url", models.DATABASE_URL) + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +target_metadata = models.Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..fbc4b07 --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/alembic/versions/0684e3c62926_add_presets_table.py b/alembic/versions/0684e3c62926_add_presets_table.py new file mode 100644 index 0000000..8b40c9e --- /dev/null +++ b/alembic/versions/0684e3c62926_add_presets_table.py @@ -0,0 +1,40 @@ +"""Add presets table + +Revision ID: 0684e3c62926 +Revises: +Create Date: 2023-09-07 12:56:56.526870 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '0684e3c62926' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('presets', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(), nullable=True), + sa.Column('name', sa.String(), nullable=True), + sa.Column('suite', sa.String(), nullable=True), + sa.Column('cmd', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('username', 'name') + ) + op.create_index(op.f('ix_presets_username'), 'presets', ['username'], unique=False) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_presets_username'), table_name='presets') + op.drop_table('presets') + # ### end Alembic commands ### diff --git a/setup.cfg b/setup.cfg index b2a08ed..03c9892 100644 --- a/setup.cfg +++ b/setup.cfg @@ -58,6 +58,9 @@ install_requires = # Temporarily, using teuthology without monkey patching the thread teuthology @ git+https://github.com/ceph/teuthology@teuth-api#egg=teuthology[test]" # Original: git+https://github.com/ceph/teuthology#egg=teuthology[test] + fastapi-sqlalchemy + alembic + psycopg2-binary [options.packages.find] diff --git a/src/teuthology_api/models/__init__.py b/src/teuthology_api/models/__init__.py new file mode 100644 index 0000000..9f10d81 --- /dev/null +++ b/src/teuthology_api/models/__init__.py @@ -0,0 +1,21 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker, Session +import os + +DATABASE_URL = os.getenv("TEUTHOLOGY_API_SQLALCHEMY_URL") +engine = create_engine(DATABASE_URL) + +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +Base = declarative_base() + + +def get_db() -> Session: + db = SessionLocal() + try: + yield db + finally: + db.close() + + +from .presets import Presets diff --git a/src/teuthology_api/models/presets.py b/src/teuthology_api/models/presets.py new file mode 100644 index 0000000..fcccfa6 --- /dev/null +++ b/src/teuthology_api/models/presets.py @@ -0,0 +1,13 @@ +from sqlalchemy import Column, Integer, String, UniqueConstraint +from . import Base + + +class Presets(Base): + __tablename__ = "presets" + id = Column(Integer, primary_key=True) + username = Column(String, index=True) + name = Column(String) + suite = Column(String) + cmd = Column(String) + + __table_args__ = (UniqueConstraint("username", "name"),) diff --git a/start_container.sh b/start_container.sh index 4c31cb1..911623f 100644 --- a/start_container.sh +++ b/start_container.sh @@ -5,6 +5,7 @@ trap exit TERM HOST=${TEUTHOLOGY_API_SERVER_HOST:-"0.0.0.0"} PORT=${TEUTHOLOGY_API_SERVER_PORT:-"8080"} +alembic -x verbose=1 upgrade head cd /teuthology_api/src/ From 0d5b3b2f8ce1955d0af9f57b64b68e68ec3ba6b7 Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Thu, 7 Sep 2023 19:41:48 +0530 Subject: [PATCH 2/6] Add presets routes, schema, model helper functions creates the following: models/presets.py: create_preset, get_user_presets, get_preset routes/presets.py: GET /, GET list/, POST /add schemas/presets.py: PresetSchema Signed-off-by: Vallari Agrawal --- src/teuthology_api/main.py | 3 ++- src/teuthology_api/models/presets.py | 23 ++++++++++++++++++ src/teuthology_api/routes/presets.py | 35 +++++++++++++++++++++++++++ src/teuthology_api/schemas/presets.py | 13 ++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/teuthology_api/routes/presets.py create mode 100644 src/teuthology_api/schemas/presets.py diff --git a/src/teuthology_api/main.py b/src/teuthology_api/main.py index 598e286..1b5ee21 100644 --- a/src/teuthology_api/main.py +++ b/src/teuthology_api/main.py @@ -5,7 +5,7 @@ from starlette.middleware.sessions import SessionMiddleware from dotenv import load_dotenv -from teuthology_api.routes import suite, kill, login, logout +from teuthology_api.routes import suite, kill, login, logout, presets load_dotenv() @@ -40,3 +40,4 @@ def read_root(request: Request): app.include_router(kill.router) app.include_router(login.router) app.include_router(logout.router) +app.include_router(presets.router) diff --git a/src/teuthology_api/models/presets.py b/src/teuthology_api/models/presets.py index fcccfa6..5c8d71c 100644 --- a/src/teuthology_api/models/presets.py +++ b/src/teuthology_api/models/presets.py @@ -1,5 +1,8 @@ from sqlalchemy import Column, Integer, String, UniqueConstraint +from sqlalchemy.orm import Session from . import Base +from teuthology_api.schemas.presets import PresetsSchema + class Presets(Base): @@ -11,3 +14,23 @@ class Presets(Base): cmd = Column(String) __table_args__ = (UniqueConstraint("username", "name"),) + + +def create_preset(db: Session, preset: PresetsSchema): + new_preset = Presets(**preset.model_dump()) + db.add(new_preset) + db.commit() + db.refresh(new_preset) + return new_preset + + +def get_user_presets(db: Session, username: str): + return db.query(Presets).filter(Presets.username == username).all() + + +def get_preset(db: Session, username: str, preset_name: str): + return ( + db.query(Presets) + .filter(Presets.username == username, Presets.name == preset_name) + .first() + ) diff --git a/src/teuthology_api/routes/presets.py b/src/teuthology_api/routes/presets.py new file mode 100644 index 0000000..c444f04 --- /dev/null +++ b/src/teuthology_api/routes/presets.py @@ -0,0 +1,35 @@ +from fastapi import APIRouter, HTTPException, Depends +from teuthology_api.models import get_db +from teuthology_api.models.presets import get_preset, get_user_presets, create_preset +from teuthology_api.schemas.presets import PresetSchema +from sqlalchemy.orm import Session +import logging + +log = logging.getLogger(__name__) + +router = APIRouter( + prefix="/presets", + tags=["presets"], +) + + +@router.get("/", status_code=200) +def read_preset(username: str, name: str, db: Session = Depends(get_db)): + db_preset = get_preset(db, username, name) + return db_preset + + +@router.get("/list", status_code=200) +def read_preset(username: str, db: Session = Depends(get_db)): + db_presets = get_user_presets(db, username) + return db_presets + + +@router.post("/add", status_code=200, response_model=PresetSchema) +def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): + db_preset = get_preset(db, username=preset.username, preset_name=preset.name) + if db_preset: + raise HTTPException( + status_code=400, detail=f"Preset '{preset.name}' already exists." + ) + return create_preset(db, preset) diff --git a/src/teuthology_api/schemas/presets.py b/src/teuthology_api/schemas/presets.py new file mode 100644 index 0000000..c74f936 --- /dev/null +++ b/src/teuthology_api/schemas/presets.py @@ -0,0 +1,13 @@ +from pydantic import BaseModel, Field +from typing import Union + + +class PresetSchema(BaseModel): + # pylint: disable=too-few-public-methods + """ + Class for Base Args. + """ + username: Union[str, None] = Field(default=None) + name: Union[str, None] = Field(default=None) + suite: Union[str, None] = Field(default=None) + cmd: Union[str, None] = Field(default=None) From 199ee06fc5810f1cb4073b1cd079dbb15df6ac69 Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Sat, 9 Sep 2023 00:42:53 +0530 Subject: [PATCH 3/6] Add '/edit' and '/delete' routes models/presets: add get_preset_id, update_preset, delete_preset and PresetsDatabaseException Signed-off-by: Vallari Agrawal --- src/teuthology_api/models/presets.py | 47 ++++++++++++++++++++++---- src/teuthology_api/routes/presets.py | 49 +++++++++++++++++++++++----- 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/src/teuthology_api/models/presets.py b/src/teuthology_api/models/presets.py index 5c8d71c..b240707 100644 --- a/src/teuthology_api/models/presets.py +++ b/src/teuthology_api/models/presets.py @@ -1,7 +1,7 @@ from sqlalchemy import Column, Integer, String, UniqueConstraint from sqlalchemy.orm import Session from . import Base -from teuthology_api.schemas.presets import PresetsSchema +from teuthology_api.schemas.presets import PresetSchema @@ -16,7 +16,13 @@ class Presets(Base): __table_args__ = (UniqueConstraint("username", "name"),) -def create_preset(db: Session, preset: PresetsSchema): +class PresetsDatabaseException(Exception): + def __init__(self, message, code): + super().__init__(message) + self.code = code + + +def create_preset(db: Session, preset: PresetSchema): new_preset = Presets(**preset.model_dump()) db.add(new_preset) db.commit() @@ -24,13 +30,42 @@ def create_preset(db: Session, preset: PresetsSchema): return new_preset -def get_user_presets(db: Session, username: str): - return db.query(Presets).filter(Presets.username == username).all() +def get_presets_by_username(db: Session, username: str): + db_preset = db.query(Presets).filter(Presets.username == username).all() + return db_preset -def get_preset(db: Session, username: str, preset_name: str): - return ( +def get_preset_by_username_name(db: Session, username: str, preset_name: str): + db_preset = ( db.query(Presets) .filter(Presets.username == username, Presets.name == preset_name) .first() ) + return db_preset + + +def get_preset_id(db: Session, preset_id: int): + db_preset = db.query(Presets).filter(Presets.id == preset_id).first() + return db_preset + + +def update_preset(db: Session, preset_id: int, update_data): + preset_query = db.query(Presets).filter(Presets.id == preset_id) + db_preset = preset_query.first() + if not db_preset: + raise PresetsDatabaseException("Preset does not exist - unable to update.", 404) + preset_query.filter(Presets.id == preset_id).update( + update_data, synchronize_session=False + ) + db.commit() + db.refresh(db_preset) + return db_preset + + +def delete_preset(db: Session, preset_id: int): + preset_query = db.query(Presets).filter(Presets.id == preset_id) + db_preset = preset_query.first() + if not db_preset: + raise PresetsDatabaseException("Preset does not exist - unable to delete.", 404) + preset_query.delete(synchronize_session=False) + db.commit() diff --git a/src/teuthology_api/routes/presets.py b/src/teuthology_api/routes/presets.py index c444f04..1795286 100644 --- a/src/teuthology_api/routes/presets.py +++ b/src/teuthology_api/routes/presets.py @@ -1,6 +1,7 @@ -from fastapi import APIRouter, HTTPException, Depends +from fastapi import APIRouter, HTTPException, Depends, Response from teuthology_api.models import get_db -from teuthology_api.models.presets import get_preset, get_user_presets, create_preset +from teuthology_api.models.presets import PresetsDatabaseException +from teuthology_api.models import presets as presets_model from teuthology_api.schemas.presets import PresetSchema from sqlalchemy.orm import Session import logging @@ -15,21 +16,53 @@ @router.get("/", status_code=200) def read_preset(username: str, name: str, db: Session = Depends(get_db)): - db_preset = get_preset(db, username, name) + db_preset = presets_model.get_preset_by_username_name(db, username, name) + if not db_preset: + raise HTTPException(status_code=404, detail=f"Preset does not exist.") return db_preset @router.get("/list", status_code=200) def read_preset(username: str, db: Session = Depends(get_db)): - db_presets = get_user_presets(db, username) + db_presets = presets_model.get_presets_by_username(db, username) + if not db_presets: + raise HTTPException(status_code=404, detail=f"User has no presets saved.") return db_presets -@router.post("/add", status_code=200, response_model=PresetSchema) +@router.post("/add", status_code=200) def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): - db_preset = get_preset(db, username=preset.username, preset_name=preset.name) + db_preset = presets_model.get_preset_by_username_name( + db, username=preset.username, preset_name=preset.name + ) if db_preset: raise HTTPException( - status_code=400, detail=f"Preset '{preset.name}' already exists." + status_code=400, detail=f"Preset of this username & name already exists." + ) + return presets_model.create_preset(db, preset) + + +@router.put("/edit/{preset_id}", status_code=200) +def update_preset( + preset_id: int, updated_data: PresetSchema, db: Session = Depends(get_db) +): + try: + return presets_model.update_preset( + db, preset_id, updated_data.model_dump(exclude_unset=True) + ) + except PresetsDatabaseException as exc: + raise HTTPException( + status_code=exc.code, + detail=str(exc), + ) + + +@router.delete("/delete/{preset_id}", status_code=204) +def delete_preset(preset_id: int, db: Session = Depends(get_db)): + try: + presets_model.delete_preset(db, preset_id) + except PresetsDatabaseException as exc: + raise HTTPException( + status_code=exc.code, + detail=str(exc), ) - return create_preset(db, preset) From 38bcaf46d2a2889d3398ef45fada3cd6f9f34692 Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Mon, 11 Sep 2023 18:23:48 +0530 Subject: [PATCH 4/6] Add github auth to /add /edit /delete Signed-off-by: Vallari Agrawal --- src/teuthology_api/models/presets.py | 5 ++-- src/teuthology_api/routes/presets.py | 43 ++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/teuthology_api/models/presets.py b/src/teuthology_api/models/presets.py index b240707..ea7a8eb 100644 --- a/src/teuthology_api/models/presets.py +++ b/src/teuthology_api/models/presets.py @@ -4,7 +4,6 @@ from teuthology_api.schemas.presets import PresetSchema - class Presets(Base): __tablename__ = "presets" id = Column(Integer, primary_key=True) @@ -22,8 +21,8 @@ def __init__(self, message, code): self.code = code -def create_preset(db: Session, preset: PresetSchema): - new_preset = Presets(**preset.model_dump()) +def create_preset(db: Session, preset): + new_preset = Presets(**preset) db.add(new_preset) db.commit() db.refresh(new_preset) diff --git a/src/teuthology_api/routes/presets.py b/src/teuthology_api/routes/presets.py index 1795286..e243f99 100644 --- a/src/teuthology_api/routes/presets.py +++ b/src/teuthology_api/routes/presets.py @@ -1,10 +1,12 @@ from fastapi import APIRouter, HTTPException, Depends, Response +from sqlalchemy.orm import Session +import logging + +from teuthology_api.services.helpers import get_token from teuthology_api.models import get_db from teuthology_api.models.presets import PresetsDatabaseException from teuthology_api.models import presets as presets_model from teuthology_api.schemas.presets import PresetSchema -from sqlalchemy.orm import Session -import logging log = logging.getLogger(__name__) @@ -31,7 +33,17 @@ def read_preset(username: str, db: Session = Depends(get_db)): @router.post("/add", status_code=200) -def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): +def add_preset( + preset: PresetSchema, + db: Session = Depends(get_db), + access_token: str = Depends(get_token), +): + if not access_token: + raise HTTPException( + status_code=401, + detail="You need to be logged in", + headers={"WWW-Authenticate": "Bearer"}, + ) db_preset = presets_model.get_preset_by_username_name( db, username=preset.username, preset_name=preset.name ) @@ -39,13 +51,22 @@ def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): raise HTTPException( status_code=400, detail=f"Preset of this username & name already exists." ) - return presets_model.create_preset(db, preset) + return presets_model.create_preset(db, preset.model_dump()) @router.put("/edit/{preset_id}", status_code=200) def update_preset( - preset_id: int, updated_data: PresetSchema, db: Session = Depends(get_db) + preset_id: int, + updated_data: PresetSchema, + db: Session = Depends(get_db), + access_token: str = Depends(get_token), ): + if not access_token: + raise HTTPException( + status_code=401, + detail="You need to be logged in", + headers={"WWW-Authenticate": "Bearer"}, + ) try: return presets_model.update_preset( db, preset_id, updated_data.model_dump(exclude_unset=True) @@ -58,7 +79,17 @@ def update_preset( @router.delete("/delete/{preset_id}", status_code=204) -def delete_preset(preset_id: int, db: Session = Depends(get_db)): +def delete_preset( + preset_id: int, + db: Session = Depends(get_db), + access_token: str = Depends(get_token), +): + if not access_token: + raise HTTPException( + status_code=401, + detail="You need to be logged in", + headers={"WWW-Authenticate": "Bearer"}, + ) try: presets_model.delete_preset(db, preset_id) except PresetsDatabaseException as exc: From ea05df5d70da77ab1b1eacb30f7756e74d58cb7e Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Tue, 12 Sep 2023 15:00:22 +0530 Subject: [PATCH 5/6] gh-actions/start.sh: add tapi_db Signed-off-by: Vallari Agrawal --- gh-actions/start.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gh-actions/start.sh b/gh-actions/start.sh index 7bd37e1..08cfc71 100755 --- a/gh-actions/start.sh +++ b/gh-actions/start.sh @@ -11,14 +11,34 @@ if [ ! -d "$folder" ] ; then environment: TEUTHOLOGY_API_SERVER_HOST: 0.0.0.0 TEUTHOLOGY_API_SERVER_PORT: 8080 + TEUTHOLOGY_API_SQLALCHEMY_URL: postgresql+psycopg2://admin:password@tapi_db:5432/tapi_db depends_on: - teuthology - paddles + - tapi_db links: - teuthology - paddles + - tapi_db healthcheck: test: [ "CMD", "curl", "-f", "http://0.0.0.0:8082" ] + tapi_db: + image: postgres:14 + healthcheck: + test: [ "CMD", "pg_isready", "-q", "-d", "tapi_db", "-U", "admin" ] + timeout: 5s + interval: 10s + retries: 2 + environment: + - POSTGRES_USER=root + - POSTGRES_PASSWORD=password + - APP_DB_USER=admin + - APP_DB_PASS=password + - APP_DB_NAME=tapi_db + volumes: + - ./db:/docker-entrypoint-initdb.d/ + ports: + - 5433:5432 " >> teuthology/docs/docker-compose/docker-compose.yml fi cd teuthology/docs/docker-compose From 513e51c1ead3624b6a90557a907a6fcdac73308f Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Fri, 29 Sep 2023 13:02:54 +0530 Subject: [PATCH 6/6] Add services/presets.py Signed-off-by: Vallari Agrawal --- src/teuthology_api/models/presets.py | 56 ------------------------ src/teuthology_api/routes/presets.py | 27 ++++++------ src/teuthology_api/schemas/presets.py | 4 +- src/teuthology_api/services/presets.py | 60 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 72 deletions(-) create mode 100644 src/teuthology_api/services/presets.py diff --git a/src/teuthology_api/models/presets.py b/src/teuthology_api/models/presets.py index ea7a8eb..6f58fbf 100644 --- a/src/teuthology_api/models/presets.py +++ b/src/teuthology_api/models/presets.py @@ -1,5 +1,4 @@ from sqlalchemy import Column, Integer, String, UniqueConstraint -from sqlalchemy.orm import Session from . import Base from teuthology_api.schemas.presets import PresetSchema @@ -13,58 +12,3 @@ class Presets(Base): cmd = Column(String) __table_args__ = (UniqueConstraint("username", "name"),) - - -class PresetsDatabaseException(Exception): - def __init__(self, message, code): - super().__init__(message) - self.code = code - - -def create_preset(db: Session, preset): - new_preset = Presets(**preset) - db.add(new_preset) - db.commit() - db.refresh(new_preset) - return new_preset - - -def get_presets_by_username(db: Session, username: str): - db_preset = db.query(Presets).filter(Presets.username == username).all() - return db_preset - - -def get_preset_by_username_name(db: Session, username: str, preset_name: str): - db_preset = ( - db.query(Presets) - .filter(Presets.username == username, Presets.name == preset_name) - .first() - ) - return db_preset - - -def get_preset_id(db: Session, preset_id: int): - db_preset = db.query(Presets).filter(Presets.id == preset_id).first() - return db_preset - - -def update_preset(db: Session, preset_id: int, update_data): - preset_query = db.query(Presets).filter(Presets.id == preset_id) - db_preset = preset_query.first() - if not db_preset: - raise PresetsDatabaseException("Preset does not exist - unable to update.", 404) - preset_query.filter(Presets.id == preset_id).update( - update_data, synchronize_session=False - ) - db.commit() - db.refresh(db_preset) - return db_preset - - -def delete_preset(db: Session, preset_id: int): - preset_query = db.query(Presets).filter(Presets.id == preset_id) - db_preset = preset_query.first() - if not db_preset: - raise PresetsDatabaseException("Preset does not exist - unable to delete.", 404) - preset_query.delete(synchronize_session=False) - db.commit() diff --git a/src/teuthology_api/routes/presets.py b/src/teuthology_api/routes/presets.py index e243f99..a14deb4 100644 --- a/src/teuthology_api/routes/presets.py +++ b/src/teuthology_api/routes/presets.py @@ -4,9 +4,8 @@ from teuthology_api.services.helpers import get_token from teuthology_api.models import get_db -from teuthology_api.models.presets import PresetsDatabaseException -from teuthology_api.models import presets as presets_model -from teuthology_api.schemas.presets import PresetSchema +from teuthology_api.services.presets import PresetsDatabaseException, PresetsService +from teuthology_api.schemas.presets import PresetsSchema log = logging.getLogger(__name__) @@ -18,7 +17,7 @@ @router.get("/", status_code=200) def read_preset(username: str, name: str, db: Session = Depends(get_db)): - db_preset = presets_model.get_preset_by_username_name(db, username, name) + db_preset = PresetsService(db).get_by_username_and_name(username, name) if not db_preset: raise HTTPException(status_code=404, detail=f"Preset does not exist.") return db_preset @@ -26,7 +25,7 @@ def read_preset(username: str, name: str, db: Session = Depends(get_db)): @router.get("/list", status_code=200) def read_preset(username: str, db: Session = Depends(get_db)): - db_presets = presets_model.get_presets_by_username(db, username) + db_presets = PresetsService(db).get_by_username(username) if not db_presets: raise HTTPException(status_code=404, detail=f"User has no presets saved.") return db_presets @@ -34,7 +33,7 @@ def read_preset(username: str, db: Session = Depends(get_db)): @router.post("/add", status_code=200) def add_preset( - preset: PresetSchema, + preset: PresetsSchema, db: Session = Depends(get_db), access_token: str = Depends(get_token), ): @@ -44,20 +43,20 @@ def add_preset( detail="You need to be logged in", headers={"WWW-Authenticate": "Bearer"}, ) - db_preset = presets_model.get_preset_by_username_name( - db, username=preset.username, preset_name=preset.name + db_preset_exists = PresetsService(db).get_by_username_and_name( + username=preset.username, preset_name=preset.name ) - if db_preset: + if db_preset_exists: raise HTTPException( status_code=400, detail=f"Preset of this username & name already exists." ) - return presets_model.create_preset(db, preset.model_dump()) + return PresetsService(db).create(preset.model_dump()) @router.put("/edit/{preset_id}", status_code=200) def update_preset( preset_id: int, - updated_data: PresetSchema, + updated_data: PresetsSchema, db: Session = Depends(get_db), access_token: str = Depends(get_token), ): @@ -68,8 +67,8 @@ def update_preset( headers={"WWW-Authenticate": "Bearer"}, ) try: - return presets_model.update_preset( - db, preset_id, updated_data.model_dump(exclude_unset=True) + return PresetsService(db).update( + preset_id, updated_data.model_dump(exclude_unset=True) ) except PresetsDatabaseException as exc: raise HTTPException( @@ -91,7 +90,7 @@ def delete_preset( headers={"WWW-Authenticate": "Bearer"}, ) try: - presets_model.delete_preset(db, preset_id) + PresetsService(db).delete(preset_id) except PresetsDatabaseException as exc: raise HTTPException( status_code=exc.code, diff --git a/src/teuthology_api/schemas/presets.py b/src/teuthology_api/schemas/presets.py index c74f936..87f8075 100644 --- a/src/teuthology_api/schemas/presets.py +++ b/src/teuthology_api/schemas/presets.py @@ -2,10 +2,10 @@ from typing import Union -class PresetSchema(BaseModel): +class PresetsSchema(BaseModel): # pylint: disable=too-few-public-methods """ - Class for Base Args. + Class for Presets Schema. """ username: Union[str, None] = Field(default=None) name: Union[str, None] = Field(default=None) diff --git a/src/teuthology_api/services/presets.py b/src/teuthology_api/services/presets.py new file mode 100644 index 0000000..85f99e8 --- /dev/null +++ b/src/teuthology_api/services/presets.py @@ -0,0 +1,60 @@ +from sqlalchemy.orm import Session +from teuthology_api.models.presets import Presets + + +class PresetsDatabaseException(Exception): + def __init__(self, message, code): + super().__init__(message) + self.code = code + + +class PresetsService: + def __init__(self, db: Session) -> None: + self.db = db + + def get_by_username(self, username: str): + db_preset = self.db.query(Presets).filter(Presets.username == username).all() + return db_preset + + def get_by_username_and_name(self, username: str, preset_name: str): + db_preset = ( + self.db.query(Presets) + .filter(Presets.username == username, Presets.name == preset_name) + .first() + ) + return db_preset + + def get_by_id(self, preset_id: int): + db_preset = self.db.query(Presets).filter(Presets.id == preset_id).first() + return db_preset + + def create(self, preset: dict) -> Presets: + new_preset = Presets(**preset) + self.db.add(new_preset) + self.db.commit() + self.db.refresh(new_preset) + return new_preset + + def update(self, preset_id: int, update_data): + preset_query = self.db.query(Presets).filter(Presets.id == preset_id) + db_preset = preset_query.first() + if not db_preset: + raise PresetsDatabaseException( + "Presets does not exist - unable to update.", 404 + ) + preset_query.filter(Presets.id == preset_id).update( + update_data, synchronize_session=False + ) + self.db.commit() + self.db.refresh(db_preset) + return db_preset + + def delete(self, preset_id: int): + preset_query = self.db.query(Presets).filter(Presets.id == preset_id) + db_preset = preset_query.first() + if not db_preset: + raise PresetsDatabaseException( + "Presets does not exist - unable to delete.", 404 + ) + preset_query.delete(synchronize_session=False) + self.db.commit()