From d0a62103a9b9e7fe1123b074d64a1ba5b4dfe580 Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Mon, 11 Mar 2024 17:50:40 +0000 Subject: [PATCH] add account name to the message --- fixbackend/cloud_accounts/service_impl.py | 5 ++++- fixbackend/domain_events/events.py | 1 + .../notification/email/email_messages.py | 13 ++++++++++--- .../email/templates/account_degraded.html | 7 ++++--- .../fixbackend/cloud_accounts/service_test.py | 2 ++ tests/fixbackend/domain_events/test_events.py | 3 ++- .../notification/notification_service_test.py | 19 ++++++++++++------- 7 files changed, 35 insertions(+), 15 deletions(-) diff --git a/fixbackend/cloud_accounts/service_impl.py b/fixbackend/cloud_accounts/service_impl.py index e4e78118..4497d52f 100644 --- a/fixbackend/cloud_accounts/service_impl.py +++ b/fixbackend/cloud_accounts/service_impl.py @@ -354,7 +354,9 @@ def compute_failed_scan_count(acc: CloudAccount) -> int: await self.notification_service.send_message_to_workspace( workspace_id=degraded_event.tenant_id, message=email.AccountDegraded( - cloud_account_id=degraded_event.aws_account_id, tenant_id=degraded_event.tenant_id + cloud_account_id=degraded_event.aws_account_id, + tenant_id=degraded_event.tenant_id, + account_name=degraded_event.aws_account_name, ), ) await send_pub_sub_message(degraded_event) @@ -765,6 +767,7 @@ def set_degraded(cloud_account: CloudAccount) -> CloudAccount: cloud_account_id=account.id, tenant_id=account.workspace_id, aws_account_id=account.account_id, + aws_account_name=account.final_name(), error=error, ) ) diff --git a/fixbackend/domain_events/events.py b/fixbackend/domain_events/events.py index b9402161..f3f08306 100644 --- a/fixbackend/domain_events/events.py +++ b/fixbackend/domain_events/events.py @@ -122,6 +122,7 @@ class AwsAccountDegraded(Event): cloud_account_id: FixCloudAccountId tenant_id: WorkspaceId aws_account_id: CloudAccountId + aws_account_name: Optional[str] error: str diff --git a/fixbackend/notification/email/email_messages.py b/fixbackend/notification/email/email_messages.py index b0b6c642..1661b4df 100644 --- a/fixbackend/notification/email/email_messages.py +++ b/fixbackend/notification/email/email_messages.py @@ -12,7 +12,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Any, Union +from typing import Any, Optional, Union from attrs import frozen from pathlib import Path @@ -144,13 +144,20 @@ def html(self) -> str: @frozen(kw_only=True) class AccountDegraded: cloud_account_id: CloudAccountId + account_name: Optional[str] tenant_id: WorkspaceId + def account_info(self) -> str: + formatted = ( + f"{self.account_name} ({self.cloud_account_id})" if self.account_name else f"{self.cloud_account_id}" + ) + return formatted + def subject(self) -> str: - return f"Account {self.cloud_account_id} cannot be accessed due to permission issues." + return f"""Account {self.account_info()} cannot be accessed due to permission issues.""" def text(self) -> str: - return f"""We were not able to collect latest resource information for Account {self.cloud_account_id}. Please ensure the account exists and that the necessary permissions are granted for access. + return f"""We were not able to collect latest resource information for account {self.account_info()}. Please ensure the account exists and that the necessary permissions are granted for access. Please visit https://app.global.fixcloud.io/workspace-settings/accounts#{self.tenant_id} for more details.""" diff --git a/fixbackend/notification/email/templates/account_degraded.html b/fixbackend/notification/email/templates/account_degraded.html index 6185dde4..ea289313 100644 --- a/fixbackend/notification/email/templates/account_degraded.html +++ b/fixbackend/notification/email/templates/account_degraded.html @@ -5,14 +5,15 @@

- Account {{ message.cloud_account_id }} cannot be accessed due to permission issues. + Account {{ message.account_info() }} cannot be accessed due to permission issues.

- We were not able to collect latest resource information for Account {{ message.cloud_account_id }}. Please + We were not able to collect latest resource information for account {{ message.account_info() }}. Please ensure the account exists and that the necessary permissions are granted for access. - Please visit https://app.global.fixcloud.io/workspace-settings/accounts#{{ message.tenant_id}} for more details. + Please visit the + settings page for more details.

diff --git a/tests/fixbackend/cloud_accounts/service_test.py b/tests/fixbackend/cloud_accounts/service_test.py index 30276da2..70c474b2 100644 --- a/tests/fixbackend/cloud_accounts/service_test.py +++ b/tests/fixbackend/cloud_accounts/service_test.py @@ -867,6 +867,7 @@ def get_account(state_updated_at: datetime) -> CloudAccount: assert event.cloud_account_id == account.id assert event.aws_account_id == account_id assert event.tenant_id == account.workspace_id + assert event.aws_account_name == account.final_name() @pytest.mark.asyncio @@ -958,3 +959,4 @@ async def test_move_to_degraded( assert published_event.aws_account_id == account_id assert published_event.tenant_id == account.workspace_id assert published_event.error == "Too many consecutive failed scans" + assert published_event.aws_account_name == account.final_name() diff --git a/tests/fixbackend/domain_events/test_events.py b/tests/fixbackend/domain_events/test_events.py index b2e89550..462e695e 100644 --- a/tests/fixbackend/domain_events/test_events.py +++ b/tests/fixbackend/domain_events/test_events.py @@ -43,7 +43,7 @@ AwsAccountDiscovered(fix_cloud_account_id, workspace_id, cloud_account_id), AwsAccountConfigured(fix_cloud_account_id, workspace_id, cloud_account_id), AwsAccountDeleted(user_id, fix_cloud_account_id, workspace_id, cloud_account_id), - AwsAccountDegraded(fix_cloud_account_id, workspace_id, cloud_account_id, "some error"), + AwsAccountDegraded(fix_cloud_account_id, workspace_id, cloud_account_id, "aws final name", "some error"), TenantAccountsCollected(workspace_id, {fix_cloud_account_id: collect_info}, now + timedelta(hours=1)), WorkspaceCreated(workspace_id, user_id), ] @@ -75,6 +75,7 @@ "cloud_account_id": "69dea3e9-bafe-4e80-9c9d-5a7e1b519767", "tenant_id": "35dfca88-3028-4990-9d30-a269228d0b01", "aws_account_id": "123", + "aws_accoun_name": "aws final name", "error": "some error", }, TenantAccountsCollected: { diff --git a/tests/fixbackend/notification/notification_service_test.py b/tests/fixbackend/notification/notification_service_test.py index 9c63aafa..2e028787 100644 --- a/tests/fixbackend/notification/notification_service_test.py +++ b/tests/fixbackend/notification/notification_service_test.py @@ -282,16 +282,21 @@ async def test_send_degraded_message( workspace: Workspace, email_sender: InMemoryEmailSender, ) -> None: - message = AccountDegraded(cloud_account_id=CloudAccountId("12345"), tenant_id=workspace.id) + message = AccountDegraded( + cloud_account_id=CloudAccountId("12345"), tenant_id=workspace.id, account_name="Development" + ) await notification_service.send_message_to_workspace(workspace_id=workspace.id, message=message) assert len(email_sender.call_args) == 1 - assert email_sender.call_args[0].subject == "Account 12345 cannot be accessed due to permission issues." - assert "Account 12345 cannot be accessed due to permission issues." in (email_sender.call_args[0].html or "") - assert "We were not able to collect latest resource information for Account 12345." in ( + assert ( + email_sender.call_args[0].subject == "Account Development (12345) cannot be accessed due to permission issues." + ) + assert "Account Development (12345) cannot be accessed due to permission issues." in ( email_sender.call_args[0].html or "" ) - assert ( - f"Please visit https://app.global.fixcloud.io/workspace-settings/accounts#{workspace.id} for more details." - in (email_sender.call_args[0].html or "") + assert "We were not able to collect latest resource information for account Development (12345)." in ( + email_sender.call_args[0].html or "" + ) + assert f"""Please visit """ in ( + email_sender.call_args[0].html or "" )