diff --git a/tools/c7n_org/scripts/gcpprojects.py b/tools/c7n_org/scripts/gcpprojects.py index 4f9b42b28de..fbd106955dd 100644 --- a/tools/c7n_org/scripts/gcpprojects.py +++ b/tools/c7n_org/scripts/gcpprojects.py @@ -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() \ No newline at end of file