servicemanagement API to serviceusage API

This commit is contained in:
Jay Lee
2020-06-17 13:30:08 -04:00
parent 186381426a
commit 7c46d8548e
2 changed files with 17 additions and 14 deletions

View File

@ -7210,34 +7210,36 @@ def enableGAMProjectAPIs(GAMProjectAPIs,
i=0, i=0,
count=0): count=0):
apis = GAMProjectAPIs[:] apis = GAMProjectAPIs[:]
project_name = f'project:{projectId}' project_name = f'projects/{projectId}'
serveman = getService('servicemanagement', httpObj) serveu = getService('serviceusage', httpObj)
status = True status = True
if checkEnabled: if checkEnabled:
try: try:
services = gapi.get_all_pages( services = gapi.get_all_pages(
serveman.services(), serveu.services(),
'list', 'list',
'services', 'services',
throw_reasons=[gapi_errors.ErrorReason.NOT_FOUND], throw_reasons=[gapi_errors.ErrorReason.NOT_FOUND],
consumerId=project_name, parent=project_name,
fields='nextPageToken,services(serviceName)') filter='state:ENABLED',
fields='nextPageToken,services(name)')
jcount = len(services) jcount = len(services)
print( print(
f' Project: {projectId}, Check {jcount} APIs{currentCount(i, count)}' f' Project: {projectId}, Check {jcount} APIs{currentCount(i, count)}'
) )
j = 0 j = 0
for service in sorted(services, key=lambda k: k['serviceName']): for service in sorted(services, key=lambda k: k['name']):
j += 1 j += 1
if 'serviceName' in service: if 'name' in service:
if service['serviceName'] in apis: service_name = service['name'].split('/')[-1]
if service_name in apis:
print( print(
f' API: {service["serviceName"]}, Already enabled{currentCount(j, jcount)}' f' API: {service_name}, Already enabled{currentCount(j, jcount)}'
) )
apis.remove(service['serviceName']) apis.remove(service_name)
else: else:
print( print(
f' API: {service["serviceName"]}, Already enabled (non-GAM which is fine){currentCount(j, jcount)}' f' API: {service_name}, Already enabled (non-GAM which is fine){currentCount(j, jcount)}'
) )
except gapi_errors.GapiNotFoundError as e: except gapi_errors.GapiNotFoundError as e:
print( print(
@ -7251,18 +7253,18 @@ def enableGAMProjectAPIs(GAMProjectAPIs,
) )
j = 0 j = 0
for api in apis: for api in apis:
service_name = f'projects/{projectId}/services/{api}'
j += 1 j += 1
while True: while True:
try: try:
gapi.call(serveman.services(), gapi.call(serveu.services(),
'enable', 'enable',
throw_reasons=[ throw_reasons=[
gapi_errors.ErrorReason.FAILED_PRECONDITION, gapi_errors.ErrorReason.FAILED_PRECONDITION,
gapi_errors.ErrorReason.FORBIDDEN, gapi_errors.ErrorReason.FORBIDDEN,
gapi_errors.ErrorReason.PERMISSION_DENIED gapi_errors.ErrorReason.PERMISSION_DENIED
], ],
serviceName=api, name=service_name)
body={'consumerId': project_name})
print(f' API: {api}, Enabled{currentCount(j, jcount)}') print(f' API: {api}, Enabled{currentCount(j, jcount)}')
break break
except gapi_errors.GapiFailedPreconditionError as e: except gapi_errors.GapiFailedPreconditionError as e:

View File

@ -252,6 +252,7 @@ API_VER_MAPPING = {
'reports': 'reports_v1', 'reports': 'reports_v1',
'reseller': 'v1', 'reseller': 'v1',
'servicemanagement': 'v1', 'servicemanagement': 'v1',
'serviceusage': 'v1',
'sheets': 'v4', 'sheets': 'v4',
'siteVerification': 'v1', 'siteVerification': 'v1',
'storage': 'v1', 'storage': 'v1',