Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
import subprocess
import json
from settings import ORGANIZATION_ID
FOLDER_LIST_CMD = 'gcloud resource-manager folders list --format json'
FOLDER_IAM_CMD = 'gcloud resource-manager folders get-iam-policy --format json'
def get_reverse_path(folder_id, path):
displayName = folders[folder_id]['displayName']
parent = folders[folder_id]['parent']
parts = parent.split('/')
type = parts[0]
parent_id = parts[1]
if type == 'folders':
return get_reverse_path(folder_id=parent_id,
path=(path + '/' + displayName))
else:
return path + '/' + displayName
return parent_id
def get_proper_path(reverse_path):
parts = reverse_path.split('/')
parts.reverse()
path = ''
for part in parts:
if part:
path = path + '/' + part
return path
def get_path(folder_id):
displayName = folders[folder_id]['displayName']
parent = folders[folder_id]['parent']
parts = parent.split('/')
type = parts[0]
parent_id = parts[1]
if type != 'folders':
return '/' + displayName
parent_path = folders[parent_id].get('path')
if parent_path:
return parent_path + '/' + displayName
reverse_path = get_reverse_path(folder_id=folder_id, path='')
return get_proper_path(reverse_path=reverse_path)
def get_folders(folder_id,
type_flag='--folder'):
list_cmd = FOLDER_LIST_CMD + ' ' + type_flag + '=' + str(folder_id)
folder_list = json.loads(subprocess.check_output(list_cmd,
shell=True,
stderr=subprocess.STDOUT))
for folder in folder_list:
name = folder['name']
id = name.split('/')[1]
iam_cmd = FOLDER_IAM_CMD + ' ' + id
folder['perm'] = json.loads(subprocess.check_output(iam_cmd,
shell=True,
stderr=subprocess.STDOUT))
folders[id] = folder
get_folders(folder_id=id)
return folders
def add_paths():
for folder_id in folders:
folder = folders[folder_id]
# reverse_path = get_reverse_path(folder_id=folder_id, path='')
folder['path'] = get_path(folder_id=folder_id)
folders = {}
folders = get_folders(folder_id=ORGANIZATION_ID,
type_flag='--organization')
add_paths()
with open('folders.json', 'w') as outfile:
json.dump(folders, outfile, ensure_ascii=False)