From 4127cb7b9f78beb18d50e15c5ba91cfbb937dba6 Mon Sep 17 00:00:00 2001 From: JohannesKoenigTMH <112848318+JohannesKoenigTMH@users.noreply.github.com> Date: Mon, 20 Nov 2023 23:43:13 +0100 Subject: [PATCH] LakeFormation: raise AlreadyExistsException when registering an existing resource (#7052) --- moto/lakeformation/exceptions.py | 5 +++++ moto/lakeformation/models.py | 6 +++++- tests/test_lakeformation/test_lakeformation.py | 6 +++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/moto/lakeformation/exceptions.py b/moto/lakeformation/exceptions.py index a0e3b4a17..86727c390 100644 --- a/moto/lakeformation/exceptions.py +++ b/moto/lakeformation/exceptions.py @@ -10,3 +10,8 @@ class EntityNotFound(JsonRESTError): class InvalidInput(JsonRESTError): def __init__(self, message: str) -> None: super().__init__("InvalidInputException", message) + + +class AlreadyExists(JsonRESTError): + def __init__(self, message: str) -> None: + super().__init__("AlreadyExistsException", message) diff --git a/moto/lakeformation/models.py b/moto/lakeformation/models.py index f1217b93a..796242174 100644 --- a/moto/lakeformation/models.py +++ b/moto/lakeformation/models.py @@ -4,7 +4,7 @@ from typing import Any, Dict, List, Tuple, Optional from moto.core import BaseBackend, BackendDict, BaseModel from moto.utilities.tagging_service import TaggingService -from .exceptions import EntityNotFound, InvalidInput +from .exceptions import EntityNotFound, InvalidInput, AlreadyExists class RessourceType(Enum): @@ -187,6 +187,10 @@ class LakeFormationBackend(BaseBackend): del self.resources[resource_arn] def register_resource(self, resource_arn: str, role_arn: str) -> None: + if resource_arn in self.resources: + raise AlreadyExists( + "An error occurred (AlreadyExistsException) when calling the RegisterResource operation: Resource is already registered" + ) self.resources[resource_arn] = Resource(resource_arn, role_arn) def list_resources(self) -> List[Resource]: diff --git a/tests/test_lakeformation/test_lakeformation.py b/tests/test_lakeformation/test_lakeformation.py index d2501c2f7..3a72f6d88 100644 --- a/tests/test_lakeformation/test_lakeformation.py +++ b/tests/test_lakeformation/test_lakeformation.py @@ -13,12 +13,12 @@ from moto import mock_lakeformation @mock_lakeformation def test_register_resource(): client = boto3.client("lakeformation", region_name="us-east-2") - resp = client.register_resource( - ResourceArn="some arn", - ) + resp = client.register_resource(ResourceArn="some arn", RoleArn="another arn") del resp["ResponseMetadata"] assert resp == {} + with pytest.raises(client.exceptions.AlreadyExistsException): + client.register_resource(ResourceArn="some arn", RoleArn="another arn") @mock_lakeformation