Skip to content

Commit

Permalink
Check Deployments instead of services for usage limit
Browse files Browse the repository at this point in the history
  • Loading branch information
emdoyle committed Aug 21, 2024
1 parent dc5cfe5 commit 80c8271
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 4 additions & 4 deletions api/src/deploy/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from src.core.models import DeployConfig, ServiceConfig
from src.db import get_db
from src.deploy import create_ecr_repository, deploy_python_lambda_function_from_ecr
from src.middleware import get_total_services_deployed_for_user, get_user
from src.middleware import get_total_deploys_for_user, get_user
from src.models import Deployment, Service, User

if TYPE_CHECKING:
Expand Down Expand Up @@ -82,13 +82,13 @@ async def deploy(
file: Annotated[UploadFile, File()],
json_data: Annotated[str, Form()],
user: User = Depends(get_user),
service_count: int = Depends(get_total_services_deployed_for_user),
deploy_count: int = Depends(get_total_deploys_for_user),
db: AsyncSession = Depends(get_db),
):
if service_count >= settings.MAX_SERVICES_PER_USER:
if deploy_count >= settings.MAX_DEPLOYS_PER_USER:
raise HTTPException(
status_code=403,
detail="User has reached the maximum number of services.",
detail=f"User has reached the maximum number of deploys ({settings.MAX_DEPLOYS_PER_USER}).",
)

try:
Expand Down
7 changes: 3 additions & 4 deletions api/src/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from src import settings
from src.db import get_db
from src.models import Deployment, Service, User
from src.models import Deployment, User

if TYPE_CHECKING:
from fastapi import FastAPI, Request
Expand All @@ -33,13 +33,12 @@ async def get_user(request: Request, db: AsyncSession = Depends(get_db)) -> User
return user


async def get_total_services_deployed_for_user(
async def get_total_deploys_for_user(
user: User = Depends(get_user), db: AsyncSession = Depends(get_db)
) -> int:
async with db as session:
query = (
select(func.count(Service.id))
.join(Service.deployment)
select(func.count(Deployment.id))
.join(Deployment.user)
.where(User.id == user.id)
)
Expand Down
2 changes: 1 addition & 1 deletion api/src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
PARE_API_KEY_HEADER: str = env.str("PARE_API_KEY_HEADER", "X-Pare-API-Key")


MAX_SERVICES_PER_USER: int = env.int("MAX_SERVICES_PER_USER", 50)
MAX_DEPLOYS_PER_USER: int = env.int("MAX_DEPLOYS_PER_USER", 50)

0 comments on commit 80c8271

Please sign in to comment.