Skip to content

Commit

Permalink
🔥feat: Add filter and metadata vars
Browse files Browse the repository at this point in the history
  • Loading branch information
D10S0VSkY-OSS committed Dec 6, 2023
1 parent 4318e9d commit f6b272f
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 20 deletions.
19 changes: 9 additions & 10 deletions sld-api-backend/src/deploy/api/container/deploy/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
from src.shared.security import deps
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users
from typing import List
from src.deploy.domain.entities.repository import DeployFilter, DeployFilterResponse
from config.api import settings





async def get_all_deploys(
current_user: schemas_users.User = Depends(deps.get_current_active_user),
skip: int = 0,
limit: int = 100,
db: Session = Depends(deps.get_db),
):
filters: DeployFilter = Depends(DeployFilter),
) -> List[DeployFilterResponse]:
try:
# Si el usuario no es un maestro, aplicar el filtro de escuadrón
if not crud_users.is_master(db, current_user):
squad = current_user.squad
return crud_deploys.get_all_deploys_by_squad(
db=db, squad=squad, skip=skip, limit=limit
)
return crud_deploys.get_all_deploys(db=db, skip=skip, limit=limit)
filters.squad = current_user.squad

return crud_deploys.get_deploys(db=db, filters=filters, skip=skip, limit=limit)
except Exception as err:
raise HTTPException(status_code=400, detail=f"{err}")
raise HTTPException(status_code=400, detail=str(err))


async def get_deploy_by_id(
Expand Down
6 changes: 4 additions & 2 deletions sld-api-backend/src/deploy/api/v1/deploy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from fastapi import APIRouter, Depends
from typing import List

from src.deploy.api.container.deploy import create, delete, destroy, get, update
from src.deploy.domain.entities import deploy as schemas_deploy
from src.deploy.domain.entities.repository import DeployFilterResponse, DeployFilter

router = APIRouter()

Expand Down Expand Up @@ -36,9 +38,9 @@ async def delete_infra_by_id(
return delete_deploy


@router.get("/")
@router.get("/", status_code=200, response_model=List[DeployFilterResponse])
async def get_all_deploys(
get_all_deploys: schemas_deploy.DeployBase = Depends(get.get_all_deploys),
get_all_deploys: DeployFilterResponse = Depends(get.get_all_deploys),
):
return get_all_deploys

Expand Down
37 changes: 37 additions & 0 deletions sld-api-backend/src/deploy/domain/entities/repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from pydantic import BaseModel
from typing import Optional
from datetime import datetime


class DeployFilter(BaseModel):
task_id: Optional[str] = None
name: Optional[str] = None
action: Optional[str] = None
stack_name: Optional[str] = None
stack_branch: Optional[str] = None
user_id: Optional[int] = None
username: Optional[str] = None
squad: Optional[str] = None
environment: Optional[str] = None
tfvar_file: Optional[str] = None
project_path: Optional[str] = None


class DeployFilterResponse(BaseModel):
id: int
task_id: str
name: str
action: str
start_time: Optional[str]
destroy_time: Optional[str]
stack_name: str
stack_branch: str
created_at: datetime
updated_at: Optional[datetime]
user_id: int
username: str
squad: str
variables: dict
environment: str
tfvar_file: Optional[str]
project_path: Optional[str]
14 changes: 13 additions & 1 deletion sld-api-backend/src/deploy/infrastructure/repositories.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime

from typing import List
from sqlalchemy.orm import Session
from sqlalchemy import desc

import src.deploy.domain.entities.deploy as schemas_deploy
from src.deploy.domain.entities.repository import DeployFilter, DeployFilterResponse
import src.deploy.infrastructure.models as models


Expand Down Expand Up @@ -204,3 +205,14 @@ def get_deploy_by_cloud_account(db: Session, squad: str, environment: str):
)
except Exception as err:
raise err


def get_deploys(db: Session, filters: DeployFilter, skip: int = 0, limit: int = 100) -> List[DeployFilterResponse]:
query = db.query(models.Deploy)

for field, value in filters.model_dump().items():
if value is not None:
query = query.filter(getattr(models.Deploy, field) == value)

deploys = query.order_by(desc(models.Deploy.id)).offset(skip).limit(limit).all()
return [DeployFilterResponse(**deploy.__dict__) for deploy in deploys]
102 changes: 95 additions & 7 deletions sld-api-backend/src/variables/api/container/get.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import re
from collections import defaultdict

from fastapi import Depends, HTTPException
from sqlalchemy.orm import Session

Expand All @@ -9,6 +12,89 @@
from src.users.infrastructure import repositories as crud_users


class MetadataProcessor:
def __init__(self, data):
self.data = data
self.grouped_metadata = defaultdict(
lambda: {"vars_group": "", "descriptions": []}
)

def add_metadata(self):
for key, value in self.data.items():
original_description = value.get("description")
metadata = self._parse_description(original_description)
if metadata is not None and "_no_metadata" in metadata:
continue
self._group_metadata(metadata)

if metadata is None or "error" in metadata:
value["metadata"] = {
"error": "Description format is incorrect or missing required fields."
}
else:
value["description"] = original_description
if "description_cleaned" in metadata:
metadata["description"] = metadata.pop("description_cleaned")
value["metadata"] = metadata

self._update_group_descriptions()

return self.data

def _parse_description(self, description):
key_values = dict(re.findall(r"(\w+): ([^\n]*)", description))
if all(key in key_values for key in ["order", "vars_group"]):
return self._process_newline_format(key_values)

if "|" not in description:
return {"_no_metadata": "Basic configuration"}

if description.count("|") >= 2:
return self._process_pipe_format(description)

return {"error": "Invalid format"}

def _process_pipe_format(self, description):
parts = [part.strip() for part in description.split("|")]
if parts[0]:
group_key = (
re.match(r"([A-Za-z])\d*", parts[0]).group(1) if parts[0] else ""
)
return {
"order": parts[0],
"vars_group": parts[1]
if parts[1]
else self.grouped_metadata[group_key]["vars_group"],
"description_cleaned": parts[2] if len(parts) > 2 else "",
"parameter_type": "pipe",
}
return {"error": "Invalid pipe format"}

def _process_newline_format(self, key_values):
metadata = {k: v.strip('"') for k, v in key_values.items()}
if "query_type" in metadata and "query" in metadata and "query_key" in metadata:
metadata["parameter_type"] = "EzeParams"
else:
metadata["parameter_type"] = "EOF"
metadata["description"] = metadata.pop("description", "").strip("\"'")
return metadata

def _group_metadata(self, metadata):
if "order" in metadata and not metadata.get("error"):
group_key = re.match(r"([A-Za-z])\d*", metadata["order"]).group(1)
if not self.grouped_metadata[group_key]["vars_group"]:
self.grouped_metadata[group_key]["vars_group"] = metadata.get(
"vars_group", ""
)
self.grouped_metadata[group_key]["descriptions"].append(
metadata.get("description", "")
)

def _update_group_descriptions(self):
for key, group in self.grouped_metadata.items():
group["description"] = "\n".join(group["descriptions"])


async def get_json(
stack,
current_user: schemas_users.User = Depends(deps.get_current_active_user),
Expand All @@ -20,30 +106,32 @@ async def get_json(
try:
if stack.isdigit():
result = crud_stacks.get_stack_by_id(db=db, stack_id=stack)
if result == None:
if result is None:
raise HTTPException(
status_code=404, detail=f"Not found"
status_code=404, detail="Not found"
)
if not crud_users.is_master(db, current_user):
if "*" not in result.squad_access:
if not check_squad_user(current_user.squad, result.squad_access):
raise HTTPException(
status_code=403, detail=f"Not enough permissions"
)
return result.var_json.get("variable")
metadata_result = MetadataProcessor(result.var_json.get("variable"))
return metadata_result.add_metadata()
else:
result = crud_stacks.get_stack_by_name(db=db, stack_name=stack)
if result == None:
if result is None:
raise HTTPException(
status_code=404, detail=f"Not found"
status_code=404, detail="Not found"
)
if not crud_users.is_master(db, current_user):
if "*" not in result.squad_access:
if not check_squad_user(current_user.squad, result.squad_access):
raise HTTPException(
status_code=403, detail=f"Not enough permissions"
status_code=403, detail="Not enough permissions"
)
return result.var_json.get("variable")
metadata_result = MetadataProcessor(result.var_json.get("variable"))
return metadata_result.add_metadata()
except Exception as err:
raise err

Expand Down

0 comments on commit f6b272f

Please sign in to comment.