Skip to content

Commit

Permalink
PE-6112: gcpprojects.py support subfolders (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloGiannattasio authored Aug 12, 2024
1 parent 6f70412 commit e645d96
Showing 1 changed file with 58 additions and 40 deletions.
98 changes: 58 additions & 40 deletions tools/c7n_org/scripts/gcpprojects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,77 @@


@click.command()
@click.option('-f', '--output', type=click.File('w'), default='-',
help="File to store the generated config (default stdout)")
@click.option('-e', '--exclude', required=False, multiple=True,
help="List of Project Numbers to be excluded from the Projects File")
@click.option('-b', '--buid', required=False,
help="Business Unit Folder ID")
@click.option('-ap','--appscript', default=False, is_flag=True,
help="list of app script projects to account files")
def main(output, exclude, buid, appscript):

@click.option(
"-f",
"--output",
type=click.File("w"),
default="-",
help="File to store the generated config (default stdout)",
)
@click.option(
"-e",
"--exclude",
multiple=True,
help="list of folders that won't be added to the config file",
)
@click.option(
"-b",
"--buid",
required=False,
multiple=True,
help="List folders the will be added to the config file",
)
@click.option(
"-ap",
"--appscript",
default=False,
is_flag=True,
help="list of app script projects to account files",
)
def main(output, exclude, appscript, buid):
"""
Generate a c7n-org gcp projects config file
"""
client = Session().client('cloudresourcemanager', 'v1', 'projects')
# print("Client:", client)
# print("**********************************************")
client = Session().client("cloudresourcemanager", "v1", "projects")


query_params = {'filter': f"parent.type:folder parent.id:{buid}"} if buid else {}
# print("Query Params:", query_params)
# print("**********************************************")

results = []
for page in client.execute_paged_query('list', query_params):
# print("Page:", page)
# print("**********************************************")

for project in page.get('projects', []):
# print("Project:", project)
# print("**********************************************")

for page in client.execute_paged_query("list", {}):
for project in page.get("projects", []):
# Exclude App Script GCP Projects
if appscript == False:
if 'sys-' in project['projectId']:
if "sys-" in project["projectId"]:
continue

if project['lifecycleState'] != 'ACTIVE' or project['projectNumber'] in exclude:

if (
project["lifecycleState"] != "ACTIVE"
or project["projectNumber"] in exclude
):
continue

if buid != ():
if project["parent"]["type"] != "folder":
continue
for fold in buid:
if project["parent"]["id"] != fold:
continue

project_info = {
'project_id': project['projectId'],
'project_number': project['projectNumber'],
'name': project['name'],
"project_id": project["projectId"],
"project_number": project["projectNumber"],
"name": project["name"],
}

if 'labels' in project:
project_info['tags'] = [
'%s:%s' % (k, v) for k, v in project.get('labels', {}).items()]
project_info['vars'] = {k: v for k, v in project.get('labels', {}).items()}
if "labels" in project:
project_info["tags"] = [
"%s:%s" % (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.write(
yaml.safe_dump({'projects': results}, default_flow_style=False))
output.write(yaml.safe_dump({"projects": results}, default_flow_style=False))


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()

0 comments on commit e645d96

Please sign in to comment.