Created
December 11, 2020 18:27
-
-
Save tedwardd/c0d66c507cc6a60f25528d3d1b378f9b to your computer and use it in GitHub Desktop.
Inefficient GCP Path Crawler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This crawls through a folder hierarchy searching for a specific folder in GCP using Google's v2 folders API: | |
https://cloud.google.com/resource-manager/reference/rest/v2/folders | |
The better way to do this for real is to use the v2 folder search API: | |
https://cloud.google.com/resource-manager/reference/rest/v2/folders/search | |
This is here only as a reminder that you should always read API docs COMPLETELY before writing a solution to a problem | |
""" | |
def __get_folder_id(self, folder, parent): | |
folder_data = ( | |
self.crm_v2.folders().list(parent=parent).execute().get("folders", "") | |
) | |
folder_list = [] | |
for folder in folder_data: | |
folder_list.append(folder.get("name", "")) | |
return folder_list | |
def path_exists(self, target: str, parent: str): | |
parent_id = self.__get_org_id() # Org is always initial parent | |
path_list = parent.split("/") # Make list of paths to traverse | |
while len(path_list) > 0: # Loop until no more paths to check | |
path = path_list.pop(0) # Pop off the first path in the list | |
try: # Try to get the folder and set as the parent for the next folder if it does | |
parent_id = self.__get_folder_id(path, parent_id) | |
except errors.HttpError as e: | |
# Google returns 403 both for authz issue and missing object | |
# Either way, this means we can't continue | |
if e.resp.get('status', "") == '403': | |
raise Exception(f"Error trying to access {path}. Check your parent path and try again.") | |
raise e | |
try: # One we know the ID of the final parent folder, we can check if the new one exists or not | |
return self.__get_folder_id(target, parent_id) | |
except errors.HttpError as e: | |
if e.resp.get('status', "") == '403': | |
return False | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment