1919router = APIRouter (prefix = "/aws-connections" , tags = ["AWS Connections" ])
2020
2121
22+ async def _get_user_connection (
23+ connection_id : int ,
24+ user_id : int ,
25+ db : AsyncSession ,
26+ ) -> AWSConnection :
27+ """Fetch an AWS connection by ID scoped to the given user, or raise 404."""
28+ result = await db .execute (
29+ select (AWSConnection ).where (
30+ AWSConnection .id == connection_id ,
31+ AWSConnection .user_id == user_id ,
32+ )
33+ )
34+ connection = result .scalar_one_or_none ()
35+ if not connection :
36+ raise HTTPException (
37+ status_code = status .HTTP_404_NOT_FOUND ,
38+ detail = f"Connection { connection_id } not found" ,
39+ )
40+ return connection
41+
42+
2243@router .post ("/" , response_model = AWSConnectionRead , status_code = status .HTTP_201_CREATED )
2344async def create_connection (
2445 connection_data : AWSConnectionCreate ,
@@ -61,19 +82,7 @@ async def get_connection(
6182 db : AsyncSession = Depends (get_async_session ),
6283) -> AWSConnection :
6384 """Get a specific AWS connection by ID."""
64- result = await db .execute (
65- select (AWSConnection ).where (
66- AWSConnection .id == connection_id ,
67- AWSConnection .user_id == current_user .id ,
68- )
69- )
70- connection = result .scalar_one_or_none ()
71- if not connection :
72- raise HTTPException (
73- status_code = status .HTTP_404_NOT_FOUND ,
74- detail = f"Connection { connection_id } not found" ,
75- )
76- return connection
85+ return await _get_user_connection (connection_id , current_user .id , db )
7786
7887
7988@router .put ("/{connection_id}" , response_model = AWSConnectionRead )
@@ -84,20 +93,8 @@ async def update_connection(
8493 db : AsyncSession = Depends (get_async_session ),
8594) -> AWSConnection :
8695 """Update an AWS connection."""
87- result = await db .execute (
88- select (AWSConnection ).where (
89- AWSConnection .id == connection_id ,
90- AWSConnection .user_id == current_user .id ,
91- )
92- )
93- connection = result .scalar_one_or_none ()
94- if not connection :
95- raise HTTPException (
96- status_code = status .HTTP_404_NOT_FOUND ,
97- detail = f"Connection { connection_id } not found" ,
98- )
96+ connection = await _get_user_connection (connection_id , current_user .id , db )
9997
100- # Update only provided fields
10198 if update_data .name is not None :
10299 connection .name = update_data .name
103100 if update_data .account_id is not None :
@@ -123,19 +120,7 @@ async def delete_connection(
123120 db : AsyncSession = Depends (get_async_session ),
124121) -> None :
125122 """Delete an AWS connection (hard delete)."""
126- result = await db .execute (
127- select (AWSConnection ).where (
128- AWSConnection .id == connection_id ,
129- AWSConnection .user_id == current_user .id ,
130- )
131- )
132- connection = result .scalar_one_or_none ()
133- if not connection :
134- raise HTTPException (
135- status_code = status .HTTP_404_NOT_FOUND ,
136- detail = f"Connection { connection_id } not found" ,
137- )
138-
123+ connection = await _get_user_connection (connection_id , current_user .id , db )
139124 await db .delete (connection )
140125 await db .commit ()
141126
@@ -147,25 +132,11 @@ async def test_connection(
147132 db : AsyncSession = Depends (get_async_session ),
148133) -> AWSConnectionTestResult :
149134 """Test an AWS connection by verifying the credentials are valid."""
150- result = await db .execute (
151- select (AWSConnection ).where (
152- AWSConnection .id == connection_id ,
153- AWSConnection .user_id == current_user .id ,
154- )
155- )
156- connection = result .scalar_one_or_none ()
157- if not connection :
158- raise HTTPException (
159- status_code = status .HTTP_404_NOT_FOUND ,
160- detail = f"Connection { connection_id } not found" ,
161- )
135+ connection = await _get_user_connection (connection_id , current_user .id , db )
162136
163- # Decrypt credentials
164- secret_access_key = decrypt (connection .encrypted_secret_access_key )
137+ # Decrypt credentials — boto3 integration to be added for live validation
138+ _secret_access_key = decrypt (connection .encrypted_secret_access_key )
165139
166- # Placeholder: AWS credential validation will be implemented
167- # when the AWS SDK (boto3) integration is added.
168- # For now, return a success response confirming the connection is stored.
169140 return AWSConnectionTestResult (
170141 success = True ,
171142 message = "AWS connection credentials are stored. Live validation requires boto3 integration." ,
0 commit comments