Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
gcp-gce-project-audit-bq/get_projects.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (42 sloc)
1.7 KB
This file contains 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
import subprocess | |
import json | |
from settings import APPS_SCRIPT_FOLDER_ID, EXCLUDED_PROJECTS | |
IAM_POLICY_CMD = 'gcloud projects get-iam-policy --format json' | |
def init_projects(): | |
projects = {} | |
project_list_cmd = 'gcloud projects list --format json' | |
print('initializing projects') | |
proj_list = json.loads(subprocess.check_output(project_list_cmd, | |
shell=True, | |
stderr=subprocess.STDOUT)) | |
for proj in proj_list: | |
project_id = proj['projectId'] | |
if proj['parent']['id'] != APPS_SCRIPT_FOLDER_ID: | |
projects[project_id] = { | |
'project': proj | |
} | |
return projects | |
def write_projects(projects): | |
with open('projects.json', 'w') as outfile: | |
json.dump(projects, outfile, ensure_ascii=False) | |
def read_projects(): | |
try: | |
with open('projects.json', 'r') as jsonfile: | |
return json.load(jsonfile) | |
except Exception: | |
projects = init_projects() | |
write_projects(projects=projects) | |
return projects | |
def get_iam_policy(project_id): | |
if project_id not in EXCLUDED_PROJECTS: | |
get_iam_policy_cmd = IAM_POLICY_CMD + ' ' + project_id | |
return json.loads(subprocess.check_output(get_iam_policy_cmd, | |
shell=True, | |
stderr=subprocess.STDOUT)) | |
projects = read_projects() | |
for project_id in projects: | |
proj = projects[project_id] | |
if not proj.get('iam_policy'): | |
print('reading iam_policy for ' + project_id) | |
proj['iam_policy'] = get_iam_policy(project_id=project_id) | |
write_projects(projects=projects) |