Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate affected entities in non org mode #79

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 32 additions & 39 deletions handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,26 +365,6 @@ def send_org_email(
},
)


# non-organization view affected accounts
def get_health_accounts(health_client, event, event_arn):
affected_accounts = []
event_accounts_paginator = health_client.get_paginator("describe_affected_entities")
event_accounts_page_iterator = event_accounts_paginator.paginate(
filter={"eventArns": [event_arn]}
)
for event_accounts_page in event_accounts_page_iterator:
json_event_accounts = json.dumps(event_accounts_page, default=myconverter)
parsed_event_accounts = json.loads(json_event_accounts)
try:
affected_accounts.append(
parsed_event_accounts["entities"][0]["awsAccountId"]
)
except Exception:
affected_accounts = []
return affected_accounts


# organization view affected accounts
def get_health_org_accounts(health_client, event, event_arn):
affected_org_accounts = []
Expand All @@ -402,12 +382,15 @@ def get_health_org_accounts(health_client, event, event_arn):


# get the array of affected entities for all affected accounts and return as an array of JSON objects
def get_affected_entities(health_client, event_arn, affected_accounts, is_org_mode):
def get_affected_entities(health_client, event_arn, is_org_mode, affected_accounts = None):
affected_entity_array = []

for account in affected_accounts:
account_name = ""
if is_org_mode:
if is_org_mode:
if affected_accounts == None or affected_accounts == []:
raise ValueError("No affected accounts provided for organization view")

for account in affected_accounts:
account_name = ""
event_entities_paginator = health_client.get_paginator(
"describe_affected_entities_for_organization"
)
Expand All @@ -417,25 +400,35 @@ def get_affected_entities(health_client, event_arn, affected_accounts, is_org_mo
]
)
account_name = get_account_name(account)
else:
event_entities_paginator = health_client.get_paginator(
"describe_affected_entities"
)
event_entities_page_iterator = event_entities_paginator.paginate(
filter={"eventArns": [event_arn]}
)

for event_entities_page in event_entities_page_iterator:
json_event_entities = json.dumps(event_entities_page, default=myconverter)
parsed_event_entities = json.loads(json_event_entities)
for entity in parsed_event_entities['entities']:
entity.pop("entityArn", None) #remove entityArn to avoid confusion with the arn of the entityValue (not present)
entity.pop("eventArn", None) #remove eventArn duplicate of detail.arn
entity.pop("lastUpdatedTime", None) #remove for brevity
entity['awsAccountName'] = account_name
affected_entity_array.append(entity)

else:
event_entities_paginator = health_client.get_paginator(
"describe_affected_entities"
)
event_entities_page_iterator = event_entities_paginator.paginate(
filter={"eventArns": [event_arn]}
)

for event_entities_page in event_entities_page_iterator:
json_event_entities = json.dumps(event_entities_page, default=myconverter)
parsed_event_entities = json.loads(json_event_entities)
for entity in parsed_event_entities["entities"]:
entity.pop(
"entityArn"
"entityArn",
None
) # remove entityArn to avoid confusion with the arn of the entityValue (not present)
entity.pop("eventArn") # remove eventArn duplicate of detail.arn
entity.pop("lastUpdatedTime") # remove for brevity
if is_org_mode:
entity["awsAccountName"] = account_name
entity.pop("eventArn", None) # remove eventArn duplicate of detail.arn
entity.pop("lastUpdatedTime", None) # remove for brevity
affected_entity_array.append(entity)

return affected_entity_array
Expand Down Expand Up @@ -779,10 +772,10 @@ def describe_events(health_client):
str_update = str_update.strftime(str_ddb_format_sec)

# get non-organizational view requirements
affected_accounts = get_health_accounts(health_client, event, event_arn)
affected_entities = get_affected_entities(
health_client, event_arn, affected_accounts, is_org_mode=False
health_client, event_arn, is_org_mode=False
)
affected_accounts = [affected_entities[0]['awsAccountId']] if affected_entities and 'awsAccountId' in affected_entities[0] else []

# get event details
event_details = json.dumps(
Expand Down Expand Up @@ -887,7 +880,7 @@ def describe_org_events(health_client):
update_org_ddb_flag = True

affected_org_entities = get_affected_entities(
health_client, event_arn, affected_org_accounts, is_org_mode=True
health_client, event_arn, is_org_mode=True, affected_accounts=affected_org_accounts
)
# get event details
event_details = json.dumps(
Expand Down