Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧refactor: aws account add filter and extra vairables params #225

Merged
merged 11 commits into from
Dec 31, 2023
Merged
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/sld-dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
subdomain: primary
containers:
- name: sld-dashboard
image: d10s0vsky/sld-dashboard:v3.4.0
image: d10s0vsky/sld-dashboard:v3.4.1
env:
- name: PATH
value: "/home/sld/.asdf/shims:/home/sld/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Expand Down
12 changes: 7 additions & 5 deletions sld-api-backend/src/aws/api/container/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@ async def create_new_aws_profile(
current_user: schemas_users.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
# Check if the user has privileges
if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
if "string" in [aws.squad, aws.environment]:
raise HTTPException(
status_code=409,
detail="The squad or environment field must have a value that is not a string.",
)
db_aws_account = crud_aws.get_squad_aws_profile(
db=db, squad=aws.squad, environment=aws.environment
filters = schemas_aws.AwsAccountFilter()
filters.squad = aws.squad
filters.environment = aws.environment
db_aws_account = await crud_aws.get_all_aws_profile(
db=db, filters=filters
)
if db_aws_account:
raise HTTPException(status_code=409, detail="Account already exists")
try:
result = crud_aws.create_aws_profile(db=db, aws=aws)
result = await crud_aws.create_aws_profile(db=db, aws=aws)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f"Create AWS account {aws.squad} {aws.environment}",
)
return {"result": f"Create AWS account {aws.squad} {aws.environment}"}
return result
except Exception as err:
raise HTTPException(status_code=400, detail=str(err))
31 changes: 21 additions & 10 deletions sld-api-backend/src/aws/api/container/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@

from src.activityLogs.infrastructure import repositories as crud_activity
from src.aws.infrastructure import repositories as crud_aws
from src.aws.domain.entities import aws as schemas_aws
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 src.shared.domain.exeptions.in_use import ResourceInUseError


async def aws_account_by_id(
aws_account_id: int,
current_user: schemas_users.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
) -> schemas_aws.AwsAsumeProfile:

if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")

result = crud_aws.delete_aws_profile_by_id(db=db, aws_profile_id=aws_account_id)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f"Delete AWS account {aws_account_id}",
)
return result
filters = schemas_aws.AwsAccountFilter()
filters.id = aws_account_id
db_aws_account = await crud_aws.get_all_aws_profile(db=db, filters=filters)
if not db_aws_account:
raise HTTPException(status_code=404, detail="Account not found")
try:
result = await crud_aws.delete_aws_profile_by_id(db=db, aws_account_id=aws_account_id)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f"Delete AWS account {aws_account_id}",
)
return result
except ResourceInUseError as err:
raise HTTPException(status_code=409, detail=str(err))
except Exception as err:
raise err
16 changes: 9 additions & 7 deletions sld-api-backend/src/aws/api/container/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
from sqlalchemy.orm import Session

from src.aws.infrastructure import repositories as crud_aws
from src.aws.domain.entities import aws as schemas_aws
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


async def get_all_aws_accounts(
current_user: schemas_users.User = Depends(deps.get_current_active_user),
skip: int = 0,
limit: int = 100,
db: Session = Depends(deps.get_db),
):
# Check if the user has privileges
current_user: schemas_users.User = Depends(deps.get_current_active_user),
filters: schemas_aws.AwsAccountFilter = Depends(schemas_aws.AwsAccountFilter),

) -> list[schemas_aws.AwsAccountResponse]:
if not crud_users.is_master(db, current_user):
return crud_aws.get_squad_aws_profile(
db=db, squad=current_user.squad, environment=None
)
return crud_aws.get_all_aws_profile(db=db)
filters.squad = current_user.squad
return await crud_aws.get_all_aws_profile(db=db, filters=filters, skip=skip, limit=limit)
38 changes: 38 additions & 0 deletions sld-api-backend/src/aws/api/container/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from fastapi import Depends, HTTPException
from sqlalchemy.orm import Session

from src.activityLogs.infrastructure import repositories as crud_activity
from src.aws.domain.entities import aws as schemas_aws
from src.aws.infrastructure import repositories as crud_aws
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 src.shared.domain.exeptions.in_use import ResourceInUseError


async def update_aws_account(
aws_account_id: int,
aws: schemas_aws.AwsAccountUpdate,
current_user: schemas_users.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
) -> schemas_aws.AwsAsumeProfile:
if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
try:
filters = schemas_aws.AwsAccountFilter()
filters.id = aws_account_id
db_aws_account = await crud_aws.get_all_aws_profile(db=db, filters=filters)
if not db_aws_account:
raise HTTPException(status_code=404, detail="Account not found")
result = await crud_aws.update_aws_profile(db=db, aws_account_id=aws_account_id, updated_aws=aws)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f"Update AWS account {aws.squad} {aws.environment}",
)
return result
except ResourceInUseError as err:
raise HTTPException(status_code=409, detail=str(err))
except Exception as err:
raise err
15 changes: 12 additions & 3 deletions sld-api-backend/src/aws/api/v1/aws.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
from fastapi import APIRouter, Depends

from src.aws.api.container import create, delete, get
from src.aws.api.container import create, delete, get, update
from src.aws.domain.entities import aws as schemas_aws

router = APIRouter()


@router.post("/", status_code=200)
async def create_new_aws_profile(
async def create_new_aws_account(
create_aws_profile: schemas_aws.AwsAsumeProfile = Depends(
create.create_new_aws_profile
),
):
return create_aws_profile


@router.patch("/{aws_account_id}", status_code=200)
async def update_aws_account(
update_account: schemas_aws.AwsAsumeProfile = Depends(
update.update_aws_account
),
):
return update_account


@router.get("/", status_code=200, response_model=list[schemas_aws.AwsAccountResponse])
async def get_all_aws_accounts(
get_aws_profile: schemas_aws.AwsAsumeProfile = Depends(get.get_all_aws_accounts),
get_aws_profile: schemas_aws.AwsAccountResponse = Depends(get.get_all_aws_accounts),
):
return get_aws_profile

Expand Down
60 changes: 46 additions & 14 deletions sld-api-backend/src/aws/domain/entities/aws.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
from typing import Optional
import datetime
from typing import Optional, Dict, Any

from pydantic import BaseModel, Field, constr
from pydantic import BaseModel, constr, SecretStr


class AwsBase(BaseModel):
squad: constr(strip_whitespace=True)
environment: constr(strip_whitespace=True)
access_key_id: constr(strip_whitespace=True)
secret_access_key: Optional[constr(strip_whitespace=True)] = Field(
None, example="string"
)
secret_access_key: constr(strip_whitespace=True)
default_region: constr(strip_whitespace=True)
extra_variables: Optional[Dict[str, Any]] = None

default_region: constr(strip_whitespace=True)

class AwsAsumeProfile(AwsBase):
profile_name: Optional[constr(strip_whitespace=True)] = None
role_arn: Optional[constr(strip_whitespace=True)] = None
source_profile: Optional[constr(strip_whitespace=True)] = None


class Aws(AwsBase):
Expand All @@ -26,10 +23,45 @@ class Aws(AwsBase):
class Config:
from_attributes = True

class AwsAccountResponse(BaseModel):

class AwsAccountResponseBase(BaseModel):
id: int
squad: constr(strip_whitespace=True)
environment: constr(strip_whitespace=True)
profile_name: Optional[constr(strip_whitespace=True)] = None
role_arn: Optional[constr(strip_whitespace=True)] = None
source_profile: Optional[constr(strip_whitespace=True)] = None
squad: str
environment: str
default_region: Optional[str]
role_arn: Optional[str]
created_at: Optional[datetime.datetime] = None
updated_at: Optional[datetime.datetime] = None

class Config:
from_attributes = True


class AwsAccountResponse(AwsAccountResponseBase):
extra_variables: Optional[Dict[str, SecretStr]]

class Config:
from_attributes = True


class AwsAccountResponseRepo(AwsAccountResponseBase):
access_key_id: str
secret_access_key: str
extra_variables: Optional[Dict[str, Any]] = None

class Config:
from_attributes = True


class AwsAccountFilter(BaseModel):
id: Optional[int] = None
squad: Optional[str] = None
access_key_id: Optional[str] = None
environment: Optional[str] = None
default_region: Optional[str] = None
role_arn: Optional[str] = None


class AwsAccountUpdate(AwsAccountFilter):
secret_access_key: Optional[str] = None
extra_variables: Optional[Dict[str, Any]] = None
6 changes: 4 additions & 2 deletions sld-api-backend/src/aws/infrastructure/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

from config.database import Base
from sqlalchemy import Column, DateTime, Integer, String, UniqueConstraint
from sqlalchemy import Column, DateTime, Integer, String, UniqueConstraint, JSON


class Aws_provider(Base):
Expand All @@ -12,8 +12,10 @@ class Aws_provider(Base):
access_key_id = Column(String(200), nullable=False)
secret_access_key = Column(String(200), nullable=False)
default_region = Column(String(200))
profile_name = Column(String(200), nullable=False)
profile_name = Column(String(200), nullable=True)
role_arn = Column(String(200), nullable=True)
source_profile = Column(String(200), nullable=True)
extra_variables = Column(JSON, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.now())
updated_at = Column(DateTime, nullable=True)
__table_args__ = (UniqueConstraint("squad", "environment"),)
Loading
Loading