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_native.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (30 sloc)
1.37 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 get_folders_native | |
import google.auth | |
from google.cloud import resourcemanager_v3 | |
credentials, project_id = google.auth.default() | |
client = resourcemanager_v3.ProjectsClient(credentials=credentials) | |
from settings import APPS_SCRIPT_FOLDER_ID, EXCLUDED_PROJECTS, ORGANIZATION_ID | |
def init_projects(): | |
projects = {} | |
for id, folder in get_folders_native.folders.items(): | |
proj_list = client.list_projects(parent=folder['folder'].name).projects | |
for proj in proj_list: | |
project_id = proj.project_id | |
if proj.parent != APPS_SCRIPT_FOLDER_ID: | |
projects[project_id] = { | |
'project': proj | |
} | |
return projects | |
def get_iam_policy(project_id): | |
if project_id not in EXCLUDED_PROJECTS: | |
policy = client.get_iam_policy(resource=project_id) | |
return policy | |
#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 = init_projects() | |
for project_id in projects: | |
if 'iam_policy' not in projects[project_id].keys(): | |
projects[project_id]['iam_policy'] = get_iam_policy(project_id=projects[project_id]['project'].name) | |
#write_projects(projects=projects) |