From b1ed287a112f64d51354283fcdd1259d84428ce5 Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Mon, 2 Sep 2024 16:05:30 +0200 Subject: [PATCH] Fix cloud account enabled bug (#608) --- fixbackend/cloud_accounts/schemas.py | 4 +++ .../fixbackend/cloud_accounts/router_test.py | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/fixbackend/cloud_accounts/schemas.py b/fixbackend/cloud_accounts/schemas.py index 92b013c3..e0179897 100644 --- a/fixbackend/cloud_accounts/schemas.py +++ b/fixbackend/cloud_accounts/schemas.py @@ -95,6 +95,10 @@ def from_model(model: CloudAccount) -> "CloudAccountRead": scan = model.state.scan is_configured = True + case CloudAccountStates.Degraded(): + enabled = model.state.enabled + scan = model.state.scan + last_scan_finished = None if model.last_scan_started_at: last_scan_finished = model.last_scan_started_at + timedelta(seconds=model.last_scan_duration_seconds) diff --git a/tests/fixbackend/cloud_accounts/router_test.py b/tests/fixbackend/cloud_accounts/router_test.py index bbae20e9..6815ffe6 100644 --- a/tests/fixbackend/cloud_accounts/router_test.py +++ b/tests/fixbackend/cloud_accounts/router_test.py @@ -310,6 +310,7 @@ async def test_last_scan(client: AsyncClient, workspace: Workspace) -> None: async def test_get_cloud_account(client: AsyncClient, workspace: Workspace) -> None: cloud_account_service.accounts = {} cloud_account_id = FixCloudAccountId(uuid.uuid4()) + degraded_cloud_account_id = FixCloudAccountId(uuid.uuid4()) next_scan = datetime.utcnow() started_at = datetime.utcnow() cloud_account_service.accounts[cloud_account_id] = CloudAccount( @@ -335,6 +336,29 @@ async def test_get_cloud_account(client: AsyncClient, workspace: Workspace) -> N last_task_id=None, last_degraded_scan_started_at=None, ) + cloud_account_service.accounts[degraded_cloud_account_id] = CloudAccount( + id=degraded_cloud_account_id, + account_id=account_id, + workspace_id=workspace.id, + cloud=CloudNames.AWS, + state=CloudAccountStates.Degraded(AwsCloudAccess(external_id, role_name), enabled=True, scan=True, error="foo"), + account_name=CloudAccountName("foo"), + account_alias=CloudAccountAlias("foo_alias"), + user_account_name=UserCloudAccountName("foo_user"), + privileged=True, + last_scan_duration_seconds=10, + last_scan_resources_scanned=100, + last_scan_started_at=started_at, + last_scan_resources_errors=456, + next_scan=next_scan, + created_at=utc(), + updated_at=utc(), + state_updated_at=utc(), + cf_stack_version=42, + failed_scan_count=123, + last_task_id=None, + last_degraded_scan_started_at=None, + ) response = await client.get(f"/api/workspaces/{workspace.id}/cloud_account/{cloud_account_id}") assert response.status_code == 200 @@ -356,6 +380,13 @@ async def test_get_cloud_account(client: AsyncClient, workspace: Workspace) -> N assert data["cf_stack_version"] == 42 assert data["errors"] == 456 + degraded_response = await client.get(f"/api/workspaces/{workspace.id}/cloud_account/{degraded_cloud_account_id}") + assert degraded_response.status_code == 200 + data = degraded_response.json() + assert data["state"] == "degraded" + assert data["enabled"] is True + assert data["scan"] is True + @pytest.mark.asyncio async def test_list_cloud_accounts(client: AsyncClient, workspace: Workspace) -> None: