Last active
February 12, 2021 19:05
-
-
Save bobbae/fc095ff95d45df43c355960b6c800be7 to your computer and use it in GitHub Desktop.
GCP: get a list of projects with roles/owner permission
This file contains hidden or 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 | |
| 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