Release 1.7.0: Grafana catalog, ingress/IRM, tests
This commit is contained in:
@ -25,6 +25,12 @@ class PolicyCreate(BaseModel):
|
||||
steps: list[dict] = Field(default_factory=list)
|
||||
|
||||
|
||||
class PolicyPatch(BaseModel):
|
||||
name: str | None = Field(default=None, min_length=1, max_length=200)
|
||||
enabled: bool | None = None
|
||||
steps: list[dict] | None = None
|
||||
|
||||
|
||||
def register_events(_bus: EventBus, _pool: asyncpg.Pool | None = None) -> None:
|
||||
pass
|
||||
|
||||
@ -95,6 +101,67 @@ async def create_policy_api(body: PolicyCreate, pool: asyncpg.Pool | None = Depe
|
||||
}
|
||||
|
||||
|
||||
def _policy_dict(row) -> dict:
|
||||
steps = row["steps"]
|
||||
if isinstance(steps, str):
|
||||
steps = json.loads(steps)
|
||||
return {
|
||||
"id": str(row["id"]),
|
||||
"name": row["name"],
|
||||
"enabled": row["enabled"],
|
||||
"steps": steps if isinstance(steps, list) else [],
|
||||
"created_at": row["created_at"].isoformat() if row["created_at"] else None,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{policy_id}")
|
||||
async def get_policy_api(policy_id: UUID, pool: asyncpg.Pool | None = Depends(get_pool)):
|
||||
if pool is None:
|
||||
raise HTTPException(status_code=503, detail="database disabled")
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
SELECT id, name, enabled, steps, created_at
|
||||
FROM escalation_policies WHERE id = $1::uuid
|
||||
""",
|
||||
policy_id,
|
||||
)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return _policy_dict(row)
|
||||
|
||||
|
||||
@router.patch("/{policy_id}")
|
||||
async def patch_policy_api(
|
||||
policy_id: UUID,
|
||||
body: PolicyPatch,
|
||||
pool: asyncpg.Pool | None = Depends(get_pool),
|
||||
):
|
||||
if pool is None:
|
||||
raise HTTPException(status_code=503, detail="database disabled")
|
||||
if body.name is None and body.enabled is None and body.steps is None:
|
||||
raise HTTPException(status_code=400, detail="no fields to update")
|
||||
steps_json = json.dumps(body.steps) if body.steps is not None else None
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
UPDATE escalation_policies SET
|
||||
name = COALESCE($2, name),
|
||||
enabled = COALESCE($3, enabled),
|
||||
steps = COALESCE($4::jsonb, steps)
|
||||
WHERE id = $1::uuid
|
||||
RETURNING id, name, enabled, steps, created_at
|
||||
""",
|
||||
policy_id,
|
||||
body.name.strip() if body.name is not None else None,
|
||||
body.enabled,
|
||||
steps_json,
|
||||
)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return _policy_dict(row)
|
||||
|
||||
|
||||
@router.delete("/{policy_id}", status_code=204)
|
||||
async def delete_policy_api(policy_id: UUID, pool: asyncpg.Pool | None = Depends(get_pool)):
|
||||
if pool is None:
|
||||
|
||||
Reference in New Issue
Block a user