Migrate to Resource Manager API v3

This commit is contained in:
Jay Lee
2021-07-16 10:14:58 -04:00
parent cee7eb970a
commit 00e764b118
2 changed files with 27 additions and 29 deletions

View File

@@ -7158,12 +7158,7 @@ def getCRMService(login_hint):
login_hint=login_hint, login_hint=login_hint,
use_console_flow=not GC_Values[GC_OAUTH_BROWSER]) use_console_flow=not GC_Values[GC_OAUTH_BROWSER])
httpc = transport.AuthorizedHttp(creds, transport.create_http()) httpc = transport.AuthorizedHttp(creds, transport.create_http())
return getService('cloudresourcemanagerv1', httpc), httpc return getService('cloudresourcemanager', httpc), httpc
# Ugh, v2 doesn't contain all the operations of v1 so we need to use both here.
def getCRM2Service(httpc):
return getService('cloudresourcemanager', httpc)
def getGAMProjectFile(filepath): def getGAMProjectFile(filepath):
@@ -7457,10 +7452,10 @@ def _getProjects(crm, pfilter):
try: try:
return gapi.get_all_pages( return gapi.get_all_pages(
crm.projects(), crm.projects(),
'list', 'search',
'projects', 'projects',
throw_reasons=[gapi_errors.ErrorReason.BAD_REQUEST], throw_reasons=[gapi_errors.ErrorReason.BAD_REQUEST],
filter=pfilter) query=pfilter)
except gapi_errors.GapiBadRequestError as e: except gapi_errors.GapiBadRequestError as e:
controlflow.system_error_exit(2, f'Project: {pfilter}, {str(e)}') controlflow.system_error_exit(2, f'Project: {pfilter}, {str(e)}')
@@ -7524,7 +7519,6 @@ def _getLoginHintProjectId(createCmd):
crm, httpObj = getCRMService(login_hint) crm, httpObj = getCRMService(login_hint)
if parent and not parent.startswith( if parent and not parent.startswith(
'organizations/') and not parent.startswith('folders/'): 'organizations/') and not parent.startswith('folders/'):
crm2 = getCRM2Service(httpObj)
parent = convertGCPFolderNameToID(parent, crm2) parent = convertGCPFolderNameToID(parent, crm2)
if parent: if parent:
parent_type, parent_id = parent.split('/') parent_type, parent_id = parent.split('/')
@@ -7576,11 +7570,13 @@ def convertGCPFolderNameToID(parent, crm2):
def createGCPFolder(): def createGCPFolder():
login_hint = _getValidateLoginHint() login_hint = _getValidateLoginHint()
_, httpObj = getCRMService(login_hint) login_domain = login_hint.split('@')[-1]
crm2 = getCRM2Service(httpObj) crm, _ = getCRMService(login_hint)
gapi.call(crm2.folders(), organization = getGCPOrg(crm, login_domain)
gapi.call(crm.folders(),
'create', 'create',
body={ body={
'parent': organization,
'name': sys.argv[3], 'name': sys.argv[3],
'displayName': sys.argv[3] 'displayName': sys.argv[3]
}) })
@@ -7637,16 +7633,31 @@ def _checkForExistingProjectFiles():
) )
def getGCPOrg(crm, domain):
resp = gapi.call(crm.organizations(),
'search',
query=f'domain:{domain}')
try:
organization = resp['organizations'][0]['name']
print(f'Your organization name is {organization}')
return organization
except (KeyError, IndexError):
controlflow.system_error_exit(
3,
'you have no rights to create projects for your organization and you don\'t seem to be a super admin! Sorry, there\'s nothing more I can do.'
)
def doCreateProject(): def doCreateProject():
_checkForExistingProjectFiles() _checkForExistingProjectFiles()
crm, httpObj, login_hint, projectId, parent = _getLoginHintProjectId(True) crm, httpObj, login_hint, projectId, parent = _getLoginHintProjectId(True)
login_domain = login_hint[login_hint.find('@') + 1:] login_domain = login_hint[login_hint.find('@') + 1:]
body = {'projectId': projectId, 'name': 'GAM Project'} body = {'projectId': projectId, 'displayName': 'GAM Project'}
if parent: if parent:
body['parent'] = parent body['parent'] = parent
while True: while True:
create_again = False create_again = False
print(f'Creating project "{body["name"]}"...') print(f'Creating project "{body["displayName"]}"...')
create_operation = gapi.call(crm.projects(), 'create', body=body) create_operation = gapi.call(crm.projects(), 'create', body=body)
operation_name = create_operation['name'] operation_name = create_operation['name']
time.sleep(8) # Google recommends always waiting at least 5 seconds time.sleep(8) # Google recommends always waiting at least 5 seconds
@@ -7661,18 +7672,7 @@ def doCreateProject():
'Hmm... Looks like you have no rights to your Google Cloud Organization.' 'Hmm... Looks like you have no rights to your Google Cloud Organization.'
) )
print('Attempting to fix that...') print('Attempting to fix that...')
getorg = gapi.call( organization = getGCPOrg(crm, login_domain)
crm.organizations(),
'search',
body={'filter': f'domain:{login_domain}'})
try:
organization = getorg['organizations'][0]['name']
print(f'Your organization name is {organization}')
except (KeyError, IndexError):
controlflow.system_error_exit(
3,
'you have no rights to create projects for your organization and you don\'t seem to be a super admin! Sorry, there\'s nothing more I can do.'
)
org_policy = gapi.call(crm.organizations(), org_policy = gapi.call(crm.organizations(),
'getIamPolicy', 'getIamPolicy',
resource=organization) resource=organization)

View File

@@ -296,7 +296,6 @@ API_NAME_MAPPING = {
'datatransfer': 'admin', 'datatransfer': 'admin',
'drive3': 'drive', 'drive3': 'drive',
'calendar': 'calendar-json', 'calendar': 'calendar-json',
'cloudresourcemanagerv1': 'cloudresourcemanager',
'cloudidentity_beta': 'cloudidentity', 'cloudidentity_beta': 'cloudidentity',
} }
@@ -310,8 +309,7 @@ API_VER_MAPPING = {
'classroom': 'v1', 'classroom': 'v1',
'cloudidentity': 'v1', 'cloudidentity': 'v1',
'cloudidentity_beta': 'v1beta1', 'cloudidentity_beta': 'v1beta1',
'cloudresourcemanager': 'v2', 'cloudresourcemanager': 'v3',
'cloudresourcemanagerv1': 'v1',
'contactdelegation': 'v1', 'contactdelegation': 'v1',
'datatransfer': 'datatransfer_v1', 'datatransfer': 'datatransfer_v1',
'directory': 'directory_v1', 'directory': 'directory_v1',