Skip to content

Commit

Permalink
installed pre-commit hook and fixed linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mekarajesh committed Oct 29, 2024
1 parent 1b49fd1 commit 841e45f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
62 changes: 39 additions & 23 deletions nac_collector/cisco_client_sdwan.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,21 +373,21 @@ def get_feature_profiles(self, endpoint, endpoint_dict):
profile_endpoint = endpoint["endpoint"] + "/" + str(item["profileId"])
response = self.get_request(self.base_url + profile_endpoint)
main_entry = {
"data": response.json(),
"endpoint": self.base_url + profile_endpoint
}
"data": response.json(),
"endpoint": self.base_url + profile_endpoint,
}
l1_children = []
for k, v in response.json().items():
if k == "associatedProfileParcels":
for parcel in v:
parcel_type = parcel["parcelType"]
new_endpoint = (
profile_endpoint + "/" + parcel_type
)
new_endpoint = profile_endpoint + "/" + parcel_type
response = self.get_request(self.base_url + new_endpoint)
for l1_item in response.json()['data']:
for l1_item in response.json()["data"]:
self.log_response(new_endpoint, response)
response = self.get_request(self.base_url + new_endpoint + "/" + l1_item['parcelId'])
response = self.get_request(
self.base_url + new_endpoint + "/" + l1_item["parcelId"]
)
data = response.json()
l1_children.append(
{
Expand All @@ -397,28 +397,44 @@ def get_feature_profiles(self, endpoint, endpoint_dict):
+ self.get_id_value(data),
}
)
endpoint_dict[endpoint["name"]].append(main_entry if not l1_children else {**main_entry, 'children': l1_children})

endpoint_dict[endpoint["name"]].append(
main_entry
if not l1_children
else {**main_entry, "children": l1_children}
)

for profile_parcel in endpoint_dict.get(endpoint.get("name"), []):
for associatedProfileParcel in profile_parcel.get("data", {}).get("associatedProfileParcels", []):
for associatedProfileParcel in profile_parcel.get("data", {}).get(
"associatedProfileParcels", []
):
subparcels = associatedProfileParcel.get("subparcels", [])
if isinstance(subparcels, list) and subparcels:
for subparcel in subparcels:
l2_parcel_type = subparcel.get("parcelType", "")[len(associatedProfileParcel.get('parcelType', '')):].lstrip('/')
l2_new_endpoint = (
f"{self.base_url}{endpoint.get('endpoint', '')}/{profile_parcel.get('data', {}).get('profileId', '')}/{associatedProfileParcel.get('parcelType', '')}/{associatedProfileParcel.get('parcelId', '')}/{l2_parcel_type}"
)
l2_parcel_type = subparcel.get("parcelType", "")[
len(associatedProfileParcel.get("parcelType", "")) :
].lstrip("/")
l2_new_endpoint = f"{self.base_url}{endpoint.get('endpoint', '')}/{profile_parcel.get('data', {}).get('profileId', '')}/{associatedProfileParcel.get('parcelType', '')}/{associatedProfileParcel.get('parcelId', '')}/{l2_parcel_type}"
l2_response = self.get_request(l2_new_endpoint)
self.log_response(l2_new_endpoint, l2_response)
for subparcel_item in l2_response.json().get('data', []):
for subparcel_item in l2_response.json().get("data", []):
subparcel_endpoint = f"{l2_new_endpoint}/{subparcel_item.get('parcelId', '')}"
subparcel_data = self.get_request(subparcel_endpoint).json()
for profile_parcel_item in profile_parcel.get('children', []):
if profile_parcel_item.get('data', {}).get('parcelType') == associatedProfileParcel.get('parcelType'):
profile_parcel_item.setdefault('children', []).append({
"data": subparcel_data,
"endpoint": subparcel_endpoint
})
subparcel_data = self.get_request(
subparcel_endpoint
).json()
for profile_parcel_item in profile_parcel.get(
"children", []
):
if profile_parcel_item.get("data", {}).get(
"parcelType"
) == associatedProfileParcel.get("parcelType"):
profile_parcel_item.setdefault(
"children", []
).append(
{
"data": subparcel_data,
"endpoint": subparcel_endpoint,
}
)
return endpoint_dict

@staticmethod
Expand Down
24 changes: 17 additions & 7 deletions nac_collector/github_repo_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,23 @@ def parent_children(self, endpoints_list):
}
if "%s" in endpoint:
for parent_map_key in parent_map:
children = parent_map[parent_map_key]['children']
children = parent_map[parent_map_key]["children"]
to_add = []
for l1_children in children:
if "%s" in l1_children['endpoint']:
base_endpoint, child_path = l1_children['endpoint'].split("%s", 1)
if "%s" in l1_children["endpoint"]:
base_endpoint, child_path = l1_children["endpoint"].split(
"%s", 1
)
child_entry = {"name": name, "endpoint": child_path}
# Collect all child entries to be added
for child in children:
if base_endpoint.rstrip('/') == child['endpoint'].rstrip('/'):
value_exists = any(child_entry["endpoint"] in d.values() for d in child.get("children", ''))
if base_endpoint.rstrip("/") == child[
"endpoint"
].rstrip("/"):
value_exists = any(
child_entry["endpoint"] in d.values()
for d in child.get("children", "")
)
if not value_exists:
to_add.append((child, child_entry))
# Add all collected child entries
Expand All @@ -216,14 +223,17 @@ def parent_children(self, endpoints_list):
if "%v" not in endpoint and "%s" not in endpoint:
# Standalone endpoint, add directly to modified_endpoints
modified_endpoints.append(endpoint_data)


# Add all valid parent-child structures to the modified_endpoints list
for _, parent_data in parent_map.items():
# Add to modified list only if it has children
if parent_data["children"]:
# Remove the entry of child object with %s
parent_data['children'] = [child for child in parent_data['children'] if "%s" not in child.get('endpoint', '')]
parent_data["children"] = [
child
for child in parent_data["children"]
if "%s" not in child.get("endpoint", "")
]
modified_endpoints.append(parent_data)

# Return the modified endpoints list
Expand Down

0 comments on commit 841e45f

Please sign in to comment.