41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from ..connectors.portainer import PortainerConnector
|
|
from ..core.redaction import redact_message
|
|
|
|
router = APIRouter(prefix='/api/stacks')
|
|
|
|
|
|
def _raise_connector_error(response: dict):
|
|
status = str(response.get('status', 'error'))
|
|
reason = redact_message(str(response.get('reason', 'connection_failed')))
|
|
status_code = 503 if status == 'missing_config' else 502
|
|
raise HTTPException(
|
|
status_code=status_code,
|
|
detail={
|
|
'status': status,
|
|
'reason': reason,
|
|
'connector': 'portainer',
|
|
},
|
|
)
|
|
|
|
|
|
@router.get('')
|
|
def list_stacks():
|
|
response = PortainerConnector().list_stacks()
|
|
if response.get('status') == 'ok':
|
|
return response.get('stacks', [])
|
|
_raise_connector_error(response)
|
|
|
|
|
|
@router.get('/{stack_id}')
|
|
def get_stack(stack_id: str):
|
|
response = PortainerConnector().get_stack_status(stack_id)
|
|
if response.get('status') == 'ok':
|
|
return response.get('stack')
|
|
if response.get('status') == 'not_found':
|
|
return None
|
|
_raise_connector_error(response)
|