Skip to content

Instantly share code, notes, and snippets.

@bobbae
Last active February 12, 2021 19:05
Show Gist options
  • Select an option

  • Save bobbae/fc095ff95d45df43c355960b6c800be7 to your computer and use it in GitHub Desktop.

Select an option

Save bobbae/fc095ff95d45df43c355960b6c800be7 to your computer and use it in GitHub Desktop.
GCP: get a list of projects with roles/owner permission
import subprocess
import json
projects = subprocess.Popen(['gcloud', 'projects', 'list', '--format=json'], stdout=subprocess.PIPE).communicate()[0]
projects2 = json.loads(projects.decode('utf-8').replace('\n', ' '))
for proj in projects2:
projid = proj['projectId']
try:
iamp = subprocess.Popen(['gcloud', 'projects', 'get-iam-policy', projid, '--format=json'], stdout=subprocess.PIPE).communicate()[0]
iamp2 = json.loads(iamp.decode('utf-8').replace('\n', ' '))
for b in iamp2['bindings']:
role=b['role']
if role=='roles/owner':
print(projid,'owner')
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment