Skip to content

Commit

Permalink
Merge pull request #17 from port-labs/PORT-4656-aws-exporter-support-…
Browse files Browse the repository at this point in the history
…items-to-parse

support items to parse
  • Loading branch information
talsabagport authored Sep 6, 2023
2 parents 23f1323 + cc10687 commit 7eed0e5
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions lambda_function/port/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ def create_entities_json(
def run_jq_query(jq_query):
return jq.first(jq_query, resource_object)

def raise_missing_exception(missing_field, mapping):
raise Exception(
f"Missing required field value for entity, field: {missing_field}, mapping: {mapping.get(missing_field)}"
)

def dedup_list(lst):
return [dict(tup) for tup in {tuple(obj.items()) for obj in lst}]

Expand All @@ -55,34 +50,54 @@ def dedup_list(lst):
if selector_jq_query and not run_jq_query(selector_jq_query):
return []

return [
{
k: v
for k, v in {
"identifier": run_jq_query(mapping.get("identifier", "null"))
or raise_missing_exception("identifier", mapping),
"title": run_jq_query(mapping.get("title", "null"))
if mapping.get("title")
else None,
"blueprint": mapping.get("blueprint", "").strip('"')
or raise_missing_exception("blueprint", mapping),
"icon": run_jq_query(mapping.get("icon", "null"))
if mapping.get("icon")
else None,
"team": run_jq_query(mapping.get("team", "null"))
if mapping.get("team")
else None,
"properties": {
prop_key: run_jq_query(prop_val)
for prop_key, prop_val in mapping.get("properties", {}).items()
},
"relations": {
rel_key: run_jq_query(rel_val)
for rel_key, rel_val in mapping.get("relations", {}).items()
}
or None,
}.items()
if v is not None
}
for mapping in jq_mappings
]
entities = []
for mapping in jq_mappings:
items_to_parse = mapping.get('itemsToParse')
if items_to_parse:
items = run_jq_query(items_to_parse)
items = items if isinstance(items, list) else []
for item in items:
entities.append(create_upsert_entity_json(mapping, resource_object | {'item': item}))
else:
entities.append(create_upsert_entity_json(mapping, resource_object))

return entities


def create_upsert_entity_json(mapping, resource_object):
def run_jq_query(jq_query):
return jq.first(jq_query, resource_object)

return {
k: v
for k, v in {
"identifier": run_jq_query(mapping.get("identifier", "null"))
or raise_missing_exception("identifier", mapping),
"title": run_jq_query(mapping.get("title", "null"))
if mapping.get("title")
else None,
"blueprint": mapping.get("blueprint", "").strip('"')
or raise_missing_exception("blueprint", mapping),
"icon": run_jq_query(mapping.get("icon", "null"))
if mapping.get("icon")
else None,
"team": run_jq_query(mapping.get("team", "null"))
if mapping.get("team")
else None,
"properties": {
prop_key: run_jq_query(prop_val)
for prop_key, prop_val in mapping.get("properties", {}).items()
},
"relations": {
rel_key: run_jq_query(rel_val)
for rel_key, rel_val in mapping.get("relations", {}).items()
}
or None,
}.items()
if v is not None
}

def raise_missing_exception(missing_field, mapping):
raise Exception(
f"Missing required field value for entity, field: {missing_field}, mapping: {mapping.get(missing_field)}"
)

0 comments on commit 7eed0e5

Please sign in to comment.