Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,44 @@ jobs:
else
echo "Invalid branch name!" && exit 1
fi
echo "STACK_NAME=ReservationStackScheduleANDCourts${{env.STAGE}}" >> $GITHUB_ENV
echo "STACK_NAME=ReservationStackApi" >> $GITHUB_ENV

- name: Fetch config from SSM
run: |
STAGE="${{ github.ref_name }}"

USER_API_BASE=$(aws ssm get-parameter \
--name "/reservationmssuser/$STAGE/api/url" \
--query "Parameter.Value" --output text) || true

if [ -n "$USER_API_BASE" ]; then
echo "::add-mask::$USER_API_BASE"
fi

echo "USER_API_BASE=$USER_API_BASE" >> $GITHUB_ENV

- name: Validate variables from ssm
shell: /usr/bin/bash -e {0}
run: |
missing=()

check_var() {
local key=$1
local value=$2
if [ -z "$value" ]; then
missing+=("$key")
fi
}

check_var "USER_API_BASE" "$USER_API_BASE"

if [ ${#missing[@]} -gt 0 ]; then
echo "❌ Missing environment variables:"
printf ' - %s\n' "${missing[@]}"
exit 1
fi

echo "✅ All environment variables from ssm are set"

- uses: actions/checkout@v3
- uses: actions/setup-python@v4
Expand All @@ -66,7 +103,7 @@ jobs:
run: |
npm install -g aws-cdk
cd iac
pip install -r requirements.txt
pip install -r requirements-infra.txt

- name: DeployWithCDK
run: |
Expand All @@ -85,4 +122,4 @@ jobs:
FROM_EMAIL: ${{ vars.FROM_EMAIL }}
HIDDEN_COPY: ${{ vars.HIDDEN_COPY }}
REPLY_TO_EMAIL: ${{ vars.REPLY_TO_EMAIL }}
USER_API_URL: ${{ secrets.USER_API_URL }}
USER_API_URL: ${{ env.USER_API_BASE }}
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ on:

jobs:
testApp:
uses: maua-dev/ci_workflows_reusable/.github/workflows/pytest_ci.yml@V1.0
uses: maua-dev/ci_workflows_reusable/.github/workflows/pytest_ci_311.yml@V1.2
2 changes: 1 addition & 1 deletion iac/adjust_layer_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# --- Configurações ---
BUILD_DIRECTORY = "build"
PYTHON_TOP_LEVEL_DIR = os.path.join(BUILD_DIRECTORY, "python")
REQUIREMENTS_FILE = "requirements-layer.txt"
REQUIREMENTS_FILE = "requirements-app.txt"

# --- CONSTRUÇÃO CORRETA DO CAMINHO ---
# Pega o diretório do projeto (a raiz 'reservation_api') subindo um nível a partir do script atual.
Expand Down
33 changes: 18 additions & 15 deletions iac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import aws_cdk as cdk
from adjust_layer_directory import adjust_layer_directory

from stacks.iac_stack import IacStack
from stack.iac_stack import IacStack



Expand All @@ -19,26 +19,29 @@
aws_region = os.environ.get("AWS_REGION")
aws_account_id = os.environ.get("AWS_ACCOUNT_ID")
stack_name = os.environ.get("STACK_NAME")
github_ref = os.environ.get("GITHUB_REF_NAME")

stage = ''
if 'prod' in github_ref:
stage = 'PROD'
elif 'homolog' in github_ref:
stage = 'HOMOLOG'
elif 'dev' in github_ref:
stage = 'DEV'
else:
stage = 'TEST'

# CD ja checa se esta dentro do branch name dev, hmlg, prod
# É ideal manter o CD como capitalize para seguir no mesmo esquema do stackname
stage = os.environ.get("GITHUB_REF_NAME").capitalize()

tags = {
'project': 'Reservation Courts and Schedule MSS',
'project': 'Reservation Api MSS',
'stage': stage,
'stack': 'BACK',
'stack': stack_name,
'owner': 'DevCommunity'
}

IacStack(app, stack_name, env=cdk.Environment(account=aws_account_id, region=aws_region), tags=tags)
IacStack(
app,
stack_id=stack_name,
stack_name=stack_name,
stage=stage,
env=cdk.Environment(
account=aws_account_id,
region=aws_region
),
tags=tags
)


app.synth()
84 changes: 84 additions & 0 deletions iac/components/apigw_construct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from aws_cdk import Resource, aws_apigateway as apigateway
from constructs import Construct
from aws_cdk.aws_apigateway import RestApi, Cors, CorsOptions, GatewayResponse, ResponseType


class ApigwConstruct(Construct):
rest_api: RestApi
api_gateway_resource: Resource

def __init__(
self,
scope: Construct,
construct_id: str,
stage: str,
stack_name: str,
**kwargs
):

stage = stage.capitalize()

super().__init__(scope, construct_id, **kwargs)

cors_options = CorsOptions(
allow_origins =
[
"https://reservation.maua.br",
"https://reservation.devmaua.com"
]
if stage == 'Prod'
else
[
"https://reservation.hml.devmaua.com",
"https://reservation.dev.devmaua.com",
"https://localhost:3000",
"http://localhost:3000"
],
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=Cors.DEFAULT_HEADERS
)

self.rest_api = RestApi(
self,
id=f"{stack_name}_RestApi_{stage}",
rest_api_name=f"{stack_name}_RestApi_{stage}",
description="This is the Maua Reservation RestApi",
deploy_options=apigateway.StageOptions(
stage_name=stage.lower(),
logging_level=apigateway.MethodLoggingLevel.INFO,
data_trace_enabled=False,
metrics_enabled=True,
),
default_cors_preflight_options=cors_options
)

GatewayResponse(
self,
"AuthorizerDenyResponse",
rest_api=self.rest_api,
type=ResponseType.ACCESS_DENIED,
response_headers={
"Access-Control-Allow-Origin": "'*'",
"Access-Control-Allow-Headers": "'*'",
"Access-Control-Allow-Methods": "'*'",
},
status_code="403"
)

GatewayResponse(
self,
"AuthorizerUnauthorizedResponse",
rest_api=self.rest_api,
type=ResponseType.UNAUTHORIZED,
response_headers={
"Access-Control-Allow-Origin": "'*'",
"Access-Control-Allow-Headers": "'*'",
"Access-Control-Allow-Methods": "'*'",
},
status_code="401"
)

self.api_gateway_resource = self.rest_api.root.add_resource(
"reservation-api",
default_cors_preflight_options=cors_options
)
39 changes: 39 additions & 0 deletions iac/components/dynamo_construct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from aws_cdk import (
aws_dynamodb as dynamodb, RemovalPolicy,
)
from constructs import Construct

class DynamoConstruct(Construct):
table: dynamodb.Table

def __init__(
self,
scope: Construct,
construct_id: str,
stage: str,
stack_name: str,
**kwargs
) -> None:
super().__init__(scope, construct_id, **kwargs)

stage = stage.capitalize()

self.table = dynamodb.Table(
self,
id=f"{stack_name}_DynamoTable_{stage}",
table_name=f"{stack_name}_DynamoTable_{stage}",
partition_key=dynamodb.Attribute(
name="PK",
type=dynamodb.AttributeType.STRING
),
sort_key=dynamodb.Attribute(
name="SK",
type=dynamodb.AttributeType.STRING
),
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST,
removal_policy=RemovalPolicy.RETAIN if stage == "Prod" else RemovalPolicy.DESTROY
)




Loading
Loading