Skip to content

Commit

Permalink
LakeFormation: raise AlreadyExistsException when registering an exist…
Browse files Browse the repository at this point in the history
…ing resource (#7052)
  • Loading branch information
JohannesKoenigTMH authored Nov 20, 2023
1 parent 7c5f0ef commit 4127cb7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions moto/lakeformation/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ def __init__(self) -> None:
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)
6 changes: 5 additions & 1 deletion moto/lakeformation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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):
Expand Down Expand Up @@ -187,6 +187,10 @@ def deregister_resource(self, resource_arn: str) -> None:
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]:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_lakeformation/test_lakeformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
@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
Expand Down

0 comments on commit 4127cb7

Please sign in to comment.