Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AUTOMATED] Changes from Sandbox #288

Merged
merged 7 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions tools/c7n_org/scripts/gcpprojects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +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')
client = Session().client("cloudresourcemanager", "v1", "projects")

query_params = {'filter': f"parent.type:folder parent.id:{buid}"} if buid else {}
results = []
for page in client.execute_paged_query('list', query_params):

for project in page.get('projects', []):

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__':
if __name__ == "__main__":
main()