Merge pull request #1593 from fujimotos/sf/describe-log-groups
Implement describe_log_groups() method for CloudWatchLogs
This commit is contained in:
commit
c2b264d593
@ -132,6 +132,9 @@ class LogGroup:
|
|||||||
def __init__(self, region, name, tags):
|
def __init__(self, region, name, tags):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.region = region
|
self.region = region
|
||||||
|
self.arn = "arn:aws:logs:{region}:1:log-group:{log_group}".format(
|
||||||
|
region=region, log_group=name)
|
||||||
|
self.creationTime = unix_time_millis()
|
||||||
self.tags = tags
|
self.tags = tags
|
||||||
self.streams = dict() # {name: LogStream}
|
self.streams = dict() # {name: LogStream}
|
||||||
|
|
||||||
@ -197,6 +200,16 @@ class LogGroup:
|
|||||||
searched_streams = [{"logStreamName": stream.logStreamName, "searchedCompletely": True} for stream in streams]
|
searched_streams = [{"logStreamName": stream.logStreamName, "searchedCompletely": True} for stream in streams]
|
||||||
return events_page, next_token, searched_streams
|
return events_page, next_token, searched_streams
|
||||||
|
|
||||||
|
def to_describe_dict(self):
|
||||||
|
return {
|
||||||
|
"arn": self.arn,
|
||||||
|
"creationTime": self.creationTime,
|
||||||
|
"logGroupName": self.name,
|
||||||
|
"metricFilterCount": 0,
|
||||||
|
"retentionInDays": 30,
|
||||||
|
"storedBytes": sum(s.storedBytes for s in self.streams.values()),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class LogsBackend(BaseBackend):
|
class LogsBackend(BaseBackend):
|
||||||
def __init__(self, region_name):
|
def __init__(self, region_name):
|
||||||
@ -223,6 +236,21 @@ class LogsBackend(BaseBackend):
|
|||||||
raise ResourceNotFoundException()
|
raise ResourceNotFoundException()
|
||||||
del self.groups[log_group_name]
|
del self.groups[log_group_name]
|
||||||
|
|
||||||
|
def describe_log_groups(self, limit, log_group_name_prefix, next_token):
|
||||||
|
if log_group_name_prefix is None:
|
||||||
|
log_group_name_prefix = ''
|
||||||
|
if next_token is None:
|
||||||
|
next_token = 0
|
||||||
|
|
||||||
|
groups = sorted(group.to_describe_dict() for name, group in self.groups.items() if name.startswith(log_group_name_prefix))
|
||||||
|
groups_page = groups[next_token:next_token + limit]
|
||||||
|
|
||||||
|
next_token += limit
|
||||||
|
if next_token >= len(groups):
|
||||||
|
next_token = None
|
||||||
|
|
||||||
|
return groups_page, next_token
|
||||||
|
|
||||||
def create_log_stream(self, log_group_name, log_stream_name):
|
def create_log_stream(self, log_group_name, log_stream_name):
|
||||||
if log_group_name not in self.groups:
|
if log_group_name not in self.groups:
|
||||||
raise ResourceNotFoundException()
|
raise ResourceNotFoundException()
|
||||||
|
@ -33,6 +33,18 @@ class LogsResponse(BaseResponse):
|
|||||||
self.logs_backend.delete_log_group(log_group_name)
|
self.logs_backend.delete_log_group(log_group_name)
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def describe_log_groups(self):
|
||||||
|
log_group_name_prefix = self._get_param('logGroupNamePrefix')
|
||||||
|
next_token = self._get_param('nextToken')
|
||||||
|
limit = self._get_param('limit', 50)
|
||||||
|
assert limit <= 50
|
||||||
|
groups, next_token = self.logs_backend.describe_log_groups(
|
||||||
|
limit, log_group_name_prefix, next_token)
|
||||||
|
return json.dumps({
|
||||||
|
"logGroups": groups,
|
||||||
|
"nextToken": next_token
|
||||||
|
})
|
||||||
|
|
||||||
def create_log_stream(self):
|
def create_log_stream(self):
|
||||||
log_group_name = self._get_param('logGroupName')
|
log_group_name = self._get_param('logGroupName')
|
||||||
log_stream_name = self._get_param('logStreamName')
|
log_stream_name = self._get_param('logStreamName')
|
||||||
|
@ -13,6 +13,10 @@ def test_log_group_create():
|
|||||||
conn = boto3.client('logs', 'us-west-2')
|
conn = boto3.client('logs', 'us-west-2')
|
||||||
log_group_name = 'dummy'
|
log_group_name = 'dummy'
|
||||||
response = conn.create_log_group(logGroupName=log_group_name)
|
response = conn.create_log_group(logGroupName=log_group_name)
|
||||||
|
|
||||||
|
response = conn.describe_log_groups(logGroupNamePrefix=log_group_name)
|
||||||
|
assert len(response['logGroups']) == 1
|
||||||
|
|
||||||
response = conn.delete_log_group(logGroupName=log_group_name)
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user