-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MPT-4905 add create organization endpoint
- Loading branch information
Francesco Faraone
committed
Dec 27, 2024
1 parent
63b596c
commit 1f02815
Showing
13 changed files
with
407 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
API_MODIFIER_JWT_ALGORITHM = "HS256" | ||
API_MODIFIER_JWT_ISSUER = "SWO" | ||
API_MODIFIER_JWT_AUDIENCE = "modifier" | ||
API_MODIFIER_JWT_EXPIRE_AFTER_SECONDS = 300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,53 @@ | ||
from fastapi import APIRouter | ||
import httpx | ||
from fastapi import APIRouter, HTTPException, status | ||
from fastapi_pagination.limit_offset import LimitOffsetPage | ||
|
||
from app.models import OrganizationRead | ||
from app import settings | ||
from app.db.handlers import ConstraintViolationError | ||
from app.models import Organization, OrganizationCreate, OrganizationRead | ||
from app.pagination import paginate | ||
from app.repositories import OrganizationRepository | ||
from app.utils import get_api_modifier_jwt_token | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/", response_model=LimitOffsetPage[OrganizationRead]) | ||
async def get_organizations(organization_repo: OrganizationRepository): | ||
return await paginate(organization_repo) | ||
|
||
|
||
@router.post("/", response_model=OrganizationRead, status_code=status.HTTP_201_CREATED) | ||
async def create_organization(data: OrganizationCreate, organization_repo: OrganizationRepository): | ||
organization: Organization | None = None | ||
try: | ||
organization = await organization_repo.create(data) | ||
except ConstraintViolationError: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail=f"Organization with this external ID already exists: {data.external_id}.", | ||
) | ||
|
||
try: | ||
async with httpx.AsyncClient(base_url=settings.api_modifier_base_url) as client: | ||
response = await client.post( | ||
"/admin/organizations", | ||
headers={"Authorization": f"Bearer {get_api_modifier_jwt_token()}"}, | ||
json={ | ||
"org_name": data.name, | ||
"user_id": data.user_id, | ||
"currency": data.currency, | ||
}, | ||
) | ||
response.raise_for_status() | ||
ffc_organization = response.json() | ||
organization.organization_id = ffc_organization["id"] | ||
return await organization_repo.update(organization.id, organization) | ||
except httpx.HTTPStatusError as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_502_BAD_GATEWAY, | ||
detail=( | ||
"Error creating organization in FinOps for Cloud: " | ||
f"{e.response.status_code} - {e.response.text}.", | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from datetime import UTC, datetime, timedelta | ||
|
||
import jwt | ||
|
||
from app import settings | ||
from app.constants import ( | ||
API_MODIFIER_JWT_ALGORITHM, | ||
API_MODIFIER_JWT_AUDIENCE, | ||
API_MODIFIER_JWT_EXPIRE_AFTER_SECONDS, | ||
API_MODIFIER_JWT_ISSUER, | ||
) | ||
|
||
|
||
def get_api_modifier_jwt_token() -> str: | ||
now = datetime.now(UTC) | ||
return jwt.encode( | ||
{ | ||
"iss": API_MODIFIER_JWT_ISSUER, | ||
"aud": API_MODIFIER_JWT_AUDIENCE, | ||
"iat": int(now.timestamp()), | ||
"nbf": int(now.timestamp()), | ||
"exp": int( | ||
(now + timedelta(seconds=API_MODIFIER_JWT_EXPIRE_AFTER_SECONDS)).timestamp() | ||
), | ||
}, | ||
settings.api_modifier_jwt_secret, | ||
algorithm=API_MODIFIER_JWT_ALGORITHM, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Added organization_id | ||
Revision ID: 7915164f66be | ||
Revises: 8a5758ec8e50 | ||
Create Date: 2024-12-27 10:27:37.156714 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '7915164f66be' | ||
down_revision: Union[str, None] = '8a5758ec8e50' | ||
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.add_column('organizations', sa.Column('organization_id', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True)) | ||
op.create_index(op.f('ix_organizations_organization_id'), 'organizations', ['organization_id'], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_organizations_organization_id'), table_name='organizations') | ||
op.drop_column('organizations', 'organization_id') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.