Skip to content

Commit

Permalink
Add AWS account id to config recorders and cloudwatch metrics (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoltis authored Apr 5, 2021
1 parent ea81ae0 commit 29dca1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions introspector/aws/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def _get_with_parent(key: str, args: Dict) -> Optional[str]:
return None


def _require_account_id(args: Dict) -> str:
path = _get_with_parent('path', args)
if path is None:
raise GFInternal(f'Missing path in {args}')
path_parts = path.split('$')
if len(path_parts) < 2:
raise GFInternal(f'Invalid path {path}')
return path_parts[1]

def _security_group_by_name(partition: str, **kwargs) -> DbFn:
from introspector.models.resource import Resource, ResourceId

Expand Down Expand Up @@ -124,7 +133,8 @@ def _config_uri_fn(resource_name: str, **kwargs) -> str:
region = _get_with_parent('region', kwargs)
if region is None:
raise GFInternal(f'Missing region in {kwargs}')
return f'configurationRecorders/{region}/{name}'
account_id = _require_account_id(kwargs)
return f'configurationRecorders/{account_id}/{region}/{name}'
raise GFInternal(f'Failed logs uri fn {resource_name} {kwargs}')


Expand Down Expand Up @@ -181,14 +191,15 @@ def _cloudwatch_metrics(**kwargs) -> str:
region = _get_with_parent('region', kwargs)
if region is None:
raise GFInternal(f'Missing region in {kwargs} for metric')
account_id = _require_account_id(kwargs)
name = kwargs['metric_name']
namespace = kwargs['metric_namespace']
dimensions = kwargs['metric_dimensions'] or []
flattened = '$'.join([
f'{dim["Name"]}_{dim["Value"]}' for dim in dimensions
if dim['Value'] is not None
])
return f'metrics/{region}/{namespace}/{name}/{flattened}'
return f'metrics/{account_id}/{region}/{namespace}/{name}/{flattened}'


def _elastic_beanstalk(partition: str, account_id: str, resource_name: str,
Expand Down
5 changes: 3 additions & 2 deletions introspector/delta/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ def map_resource_prefix(db: Session,
import_resource_name = resource_name \
if resource_name is not None \
else raw_import.resource_name
context = raw_import.context or {}
context['path'] = raw_import.path
for mapped, attrs in mapper.map_resources(raw_import.raw_resources(),
raw_import.context or {},
raw_import.service,
context, raw_import.service,
import_resource_name, uri_fn):
apply_mapped_attrs(db,
import_job,
Expand Down

0 comments on commit 29dca1d

Please sign in to comment.