diff --git a/moto/cloudtrail/models.py b/moto/cloudtrail/models.py index f1a8db2ab1ab..f7a7fe601cc1 100644 --- a/moto/cloudtrail/models.py +++ b/moto/cloudtrail/models.py @@ -335,11 +335,11 @@ def list_trails(self) -> Iterable[Trail]: return self.describe_trails(include_shadow_trails=True) def start_logging(self, name: str) -> None: - trail = self.trails[name] + trail = self.get_trail(name) trail.start_logging() def stop_logging(self, name: str) -> None: - trail = self.trails[name] + trail = self.get_trail(name) trail.stop_logging() def delete_trail(self, name: str) -> None: diff --git a/tests/test_cloudtrail/test_cloudtrail.py b/tests/test_cloudtrail/test_cloudtrail.py index aa933f7e4bed..61b1646b967d 100644 --- a/tests/test_cloudtrail/test_cloudtrail.py +++ b/tests/test_cloudtrail/test_cloudtrail.py @@ -262,6 +262,28 @@ def test_get_trail_status_arn_inactive(): assert "StartLoggingTime" not in status +@mock_aws +def test_start_and_stop_logging_by_arn(): + """StartLogging/StopLogging accept trail ARN as well as name (like AWS).""" + client = boto3.client("cloudtrail", region_name="us-east-1") + _, resp, trail_name = create_trail_simple() + arn = resp["TrailARN"] + + client.start_logging(Name=arn) + assert client.get_trail_status(Name=trail_name)["IsLogging"] is True + + client.stop_logging(Name=arn) + assert client.get_trail_status(Name=trail_name)["IsLogging"] is False + + +@mock_aws +def test_start_logging_unknown_trail_raises_not_found(): + client = boto3.client("cloudtrail", region_name="us-east-1") + with pytest.raises(ClientError) as exc: + client.start_logging(Name="unknowntrail") + assert exc.value.response["Error"]["Code"] == "TrailNotFoundException" + + @mock_aws def test_get_trail_status_after_starting(): client = boto3.client("cloudtrail", region_name="eu-west-3")