Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
WCierkowski committed Oct 29, 2024
1 parent 8f72374 commit c166787
Showing 1 changed file with 22 additions and 47 deletions.
69 changes: 22 additions & 47 deletions tools/c7n_org/scripts/gcpprojects.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,34 @@ def get_all_subfolders(folder_id):
subfolders.update(get_all_subfolders(folder["name"].split("/")[-1])) # Recursive call
return subfolders

# Check if buid is empty; if so, assume flat structure and set organization ID
results = []
organization_id = "161588151302" # Replace with your actual organization ID
all_folders = set(buid) if buid else set()

if not buid:
# No buid provided; assuming a flat organization structure
print("No BUID specified; assuming flat organization. Listing all projects under organization.")
organization_id = "organizations/161588151302" # Replace with your actual organization ID

# Fetch projects directly under the organization
for page in client.execute_paged_query("list", {"parent": organization_id}):
for project in page.get("projects", []):
# Exclude App Script projects if the flag is set
if not appscript and "sys-" in project["projectId"]:
continue
else:
# Hierarchical structure: gather all folders and subfolders for each buid
for folder_id in buid:
all_folders.update(get_all_subfolders(folder_id))

# Exclude projects in inactive states or those in excluded folders
if project["lifecycleState"] != "ACTIVE" or project["projectNumber"] in exclude:
continue
# Retrieve all projects and apply filtering in Python
for page in client.execute_paged_query("list", {}):
for project in page.get("projects", []):
# Exclude App Script projects if the flag is set
if not appscript and "sys-" in project["projectId"]:
continue

# Exclude projects in inactive states or those in excluded folders
if project["lifecycleState"] != "ACTIVE" or project["projectNumber"] in exclude:
continue

# Filtering logic for flat and hierarchical organization structures
if (not buid and project["parent"].get("type") == "organization" and project["parent"].get("id") == organization_id) or \
(buid and project["parent"].get("type") == "folder" and project["parent"].get("id") in all_folders):

# Collect project details
project_info = {
"project_id": project["projectId"],
Expand All @@ -85,42 +96,6 @@ def get_all_subfolders(folder_id):

results.append(project_info)

else:
# Hierarchical structure: gather all folders and subfolders for each buid
all_folders = set(buid)
for folder_id in buid:
all_folders.update(get_all_subfolders(folder_id))

for page in client.execute_paged_query("list", {}):
for project in page.get("projects", []):
# Exclude App Script projects if the flag is set
if not appscript and "sys-" in project["projectId"]:
continue

# Exclude projects in inactive states or those in excluded folders
if project["lifecycleState"] != "ACTIVE" or project["projectNumber"] in exclude:
continue

# Only include projects within specified folders or subfolders
if project["parent"].get("type") == "folder" and project["parent"].get("id") in all_folders:
# Collect project details
project_info = {
"project_id": project["projectId"],
"project_number": project["projectNumber"],
"name": project["name"],
}

# Include labels if they exist
if "labels" in project:
project_info["tags"] = [
f"{k}:{v}" for k, v in project.get("labels", {}).items()
]
project_info["vars"] = {
k: v for k, v in project.get("labels", {}).items()
}

results.append(project_info)

# Output project information to YAML
output.write(yaml.safe_dump({"projects": results}, default_flow_style=False))

Expand Down

0 comments on commit c166787

Please sign in to comment.