Skip to content

Commit

Permalink
Logs: describe_log_groups() now returns the logStreamArn-property
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Dec 29, 2024
1 parent 9fa5890 commit 4de16c4
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 368 deletions.
36 changes: 29 additions & 7 deletions moto/logs/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from datetime import datetime, timedelta
from gzip import compress as gzip_compress
from typing import Any, Dict, Iterable, List, Optional, Tuple
Expand Down Expand Up @@ -598,7 +599,8 @@ def filter_log_events(

def to_describe_dict(self) -> Dict[str, Any]:
log_group = {
"arn": self.arn,
"arn": f"{self.arn}:*",
"logGroupArn": self.arn,
"creationTime": self.creation_time,
"logGroupName": self.name,
"metricFilterCount": 0,
Expand Down Expand Up @@ -898,12 +900,12 @@ def describe_log_streams(
descending: bool,
limit: int,
log_group_name: str,
log_group_id: str,
log_stream_name_prefix: str,
next_token: Optional[str],
order_by: str,
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
if log_group_name not in self.groups:
raise ResourceNotFoundException()
log_group = self._find_log_group(log_group_id, log_group_name)
if limit > 50:
raise InvalidParameterException(
constraint="Member must have value less than or equal to 50",
Expand All @@ -920,7 +922,6 @@ def describe_log_streams(
raise InvalidParameterException(
msg="Cannot order by LastEventTime with a logStreamNamePrefix."
)
log_group = self.groups[log_group_name]
return log_group.describe_log_streams(
descending=descending,
limit=limit,
Expand Down Expand Up @@ -968,22 +969,23 @@ def put_log_events(
def get_log_events(
self,
log_group_name: str,
log_group_id: str,
log_stream_name: str,
start_time: str,
end_time: str,
limit: int,
next_token: Optional[str],
start_from_head: str,
) -> Tuple[List[Dict[str, Any]], Optional[str], Optional[str]]:
if log_group_name not in self.groups:
raise ResourceNotFoundException()
log_group = self._find_log_group(
log_group_id=log_group_id, log_group_name=log_group_name
)
if limit and limit > 10000:
raise InvalidParameterException(
constraint="Member must have value less than or equal to 10000",
parameter="limit",
value=limit,
)
log_group = self.groups[log_group_name]
return log_group.get_log_events(
log_stream_name, start_time, end_time, limit, next_token, start_from_head
)
Expand Down Expand Up @@ -1327,5 +1329,25 @@ def tag_resource(self, arn: str, tags: Dict[str, str]) -> None:
def untag_resource(self, arn: str, tag_keys: List[str]) -> None:
self.tagger.untag_resource_using_names(arn, tag_keys)

def _find_log_group(self, log_group_id: str, log_group_name: str) -> LogGroup:
log_group: Optional[LogGroup] = None
if log_group_name:
log_group = self.groups.get(log_group_name)
elif log_group_id:
if not re.fullmatch(r"[\w#+=/:,.@-]*", log_group_id):
raise InvalidParameterException(
msg=f"1 validation error detected: Value '{log_group_id}' at 'logGroupIdentifier' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\w#+=/:,.@-]*"
)
for log_group in self.groups.values():
if log_group.arn == log_group_id:
log_group = log_group
else:
raise InvalidParameterException(

Check warning on line 1345 in moto/logs/models.py

View check run for this annotation

Codecov / codecov/patch

moto/logs/models.py#L1345

Added line #L1345 was not covered by tests
"Should provider either name or id, but not both"
)
if not log_group:
raise ResourceNotFoundException()
return log_group


logs_backends = BackendDict(LogsBackend, "logs")
30 changes: 17 additions & 13 deletions moto/logs/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,21 @@ def delete_log_stream(self) -> str:

def describe_log_streams(self) -> str:
log_group_name = self._get_param("logGroupName")
log_group_id = self._get_param("logGroupIdentifier")
log_stream_name_prefix = self._get_param("logStreamNamePrefix", "")
descending = self._get_param("descending", False)
limit = self._get_param("limit", 50)
next_token = self._get_param("nextToken")
order_by = self._get_param("orderBy", "LogStreamName")

streams, next_token = self.logs_backend.describe_log_streams(
descending,
limit,
log_group_name,
log_stream_name_prefix,
next_token,
order_by,
descending=descending,
limit=limit,
log_group_name=log_group_name,
log_group_id=log_group_id,
log_stream_name_prefix=log_stream_name_prefix,
next_token=next_token,
order_by=order_by,
)
return json.dumps({"logStreams": streams, "nextToken": next_token})

Expand All @@ -272,6 +274,7 @@ def put_log_events(self) -> str:

def get_log_events(self) -> str:
log_group_name = self._get_param("logGroupName")
log_group_id = self._get_param("logGroupIdentifier")
log_stream_name = self._get_param("logStreamName")
start_time = self._get_param("startTime")
end_time = self._get_param("endTime")
Expand All @@ -284,13 +287,14 @@ def get_log_events(self) -> str:
next_backward_token,
next_forward_token,
) = self.logs_backend.get_log_events(
log_group_name,
log_stream_name,
start_time,
end_time,
limit,
next_token,
start_from_head,
log_group_name=log_group_name,
log_group_id=log_group_id,
log_stream_name=log_stream_name,
start_time=start_time,
end_time=end_time,
limit=limit,
next_token=next_token,
start_from_head=start_from_head,
)
return json.dumps(
{
Expand Down
12 changes: 3 additions & 9 deletions tests/test_logs/test_export_tasks.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import copy
import json
import os
from datetime import datetime, timedelta
from uuid import UUID, uuid4

import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws, settings
from moto import mock_aws
from moto.core.utils import unix_time_millis

TEST_REGION = "us-east-1" if settings.TEST_SERVER_MODE else "us-west-2"
allow_aws_request = (
os.environ.get("MOTO_TEST_ALLOW_AWS_REQUEST", "false").lower() == "true"
)

from tests import allow_aws_request

S3_POLICY = {
"Version": "2012-10-17",
Expand Down Expand Up @@ -49,7 +43,7 @@

@pytest.fixture()
def logs():
if allow_aws_request:
if allow_aws_request():
yield boto3.client("logs", region_name="us-east-1")
else:
with mock_aws():
Expand Down
Loading

0 comments on commit 4de16c4

Please sign in to comment.