Clean up create/update project (#526)

Make function eo t=enable project APIs
This commit is contained in:
Ross Scroggs
2017-07-07 08:37:39 -07:00
committed by Jay Lee
parent f8750fe0b6
commit 4fbabd9f35

View File

@ -6710,34 +6710,27 @@ def doDelProjects(login_hint=None):
except googleapiclient.errors.HttpError: except googleapiclient.errors.HttpError:
pass pass
def doUpdateProject(login_hint=None): def enableProjectAPIs(simplehttp, httpObj, project_name, checkEnabled):
login_hint = getValidateLoginHint(login_hint)
_, http = getCRMService(login_hint)
cs_data = readFile(GC_Values[GC_CLIENT_SECRETS_JSON], mode=u'rb', continueOnError=True, displayError=True, encoding=None)
if not cs_data:
systemErrorExit(14, u'Your client secrets file:\n\n%s\n\nis missing. Please recreate the file.' % GC_Values[GC_CLIENT_SECRETS_JSON])
try:
cs_json = json.loads(cs_data)
project_id = 'project:%s' % cs_json[u'installed'][u'project_id']
except (ValueError, IndexError, KeyError):
systemErrorExit(3, u'The format of your client secrets file:\n\n%s\n\nis incorrect. Please recreate the file.' % GC_Values[GC_CLIENT_SECRETS_JSON])
simplehttp = httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL])
_, c = simplehttp.request(GAM_PROJECT_APIS, u'GET') _, c = simplehttp.request(GAM_PROJECT_APIS, u'GET')
apis_needed = c.splitlines() apis = c.splitlines()
serveman = googleapiclient.discovery.build(u'servicemanagement', u'v1', http=http, cache_discovery=False) serveman = googleapiclient.discovery.build(u'servicemanagement', u'v1', http=httpObj, cache_discovery=False)
enabled_services_results = callGAPIpages(serveman.services(), u'list', items=u'services', consumerId=project_id, fields=u'nextPageToken,services(serviceName)') if checkEnabled:
for enabled in enabled_services_results: enabledServices = callGAPIpages(serveman.services(), u'list', items=u'services',
if u'serviceName' in enabled and enabled[u'serviceName'] in apis_needed: consumerId=project_name, fields=u'nextPageToken,services(serviceName)')
print u' API %s already enabled...' % enabled[u'serviceName'] for enabled in enabledServices:
apis_needed.remove(enabled[u'serviceName']) if u'serviceName' in enabled:
elif u'serviceName' in enabled: if enabled[u'serviceName'] in apis:
print u' non-GAM API %s is enabled (which is fine)' % enabled[u'serviceName'] print u' API %s already enabled...' % enabled[u'serviceName']
for api in apis_needed: apis.remove(enabled[u'serviceName'])
else:
print u' API %s (non-GAM) is enabled (which is fine)' % enabled[u'serviceName']
for api in apis:
while True: while True:
print u' enabling API %s...' % api print u' enabling API %s...' % api
try: try:
callGAPI(serveman.services(), u'enable', throw_reasons=[u'failedPrecondition'], callGAPI(serveman.services(), u'enable',
serviceName=api, body={u'consumerId': project_id}) throw_reasons=[u'failedPrecondition'],
serviceName=api, body={u'consumerId': project_name})
break break
except googleapiclient.errors.HttpError, e: except googleapiclient.errors.HttpError, e:
print u'\nThere was an error enabling %s. Please resolve error as described below:' % api print u'\nThere was an error enabling %s. Please resolve error as described below:' % api
@ -6746,6 +6739,20 @@ def doUpdateProject(login_hint=None):
print print
raw_input(u'Press enter once resolved and we will try enabling the API again.') raw_input(u'Press enter once resolved and we will try enabling the API again.')
def doUpdateProject(login_hint=None):
login_hint = getValidateLoginHint(login_hint)
_, httpObj = getCRMService(login_hint)
cs_data = readFile(GC_Values[GC_CLIENT_SECRETS_JSON], mode=u'rb', continueOnError=True, displayError=True, encoding=None)
if not cs_data:
systemErrorExit(14, u'Your client secrets file:\n\n%s\n\nis missing. Please recreate the file.' % GC_Values[GC_CLIENT_SECRETS_JSON])
try:
cs_json = json.loads(cs_data)
project_name = 'project:%s' % cs_json[u'installed'][u'project_id']
except (ValueError, IndexError, KeyError):
systemErrorExit(3, u'The format of your client secrets file:\n\n%s\n\nis incorrect. Please recreate the file.' % GC_Values[GC_CLIENT_SECRETS_JSON])
simplehttp = httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL])
enableProjectAPIs(simplehttp, httpObj, project_name, True)
def doCreateProject(login_hint=None): def doCreateProject(login_hint=None):
def _checkClientAndSecret(simplehttp, client_id, client_secret): def _checkClientAndSecret(simplehttp, client_id, client_secret):
@ -6783,7 +6790,7 @@ def doCreateProject(login_hint=None):
sys.exit(5) sys.exit(5)
login_hint = getValidateLoginHint(login_hint) login_hint = getValidateLoginHint(login_hint)
login_domain = login_hint[login_hint.find(u'@')+1:] login_domain = login_hint[login_hint.find(u'@')+1:]
crm, http = getCRMService(login_hint) crm, httpObj = getCRMService(login_hint)
project_id = u'gam-project' project_id = u'gam-project'
for i in range(3): for i in range(3):
project_id += u'-%s' % ''.join(random.choice(string.digits + string.ascii_lowercase) for i in range(3)) project_id += u'-%s' % ''.join(random.choice(string.digits + string.ascii_lowercase) for i in range(3))
@ -6792,26 +6799,28 @@ def doCreateProject(login_hint=None):
while True: while True:
create_again = False create_again = False
print u'Creating project "%s"...' % body[u'name'] print u'Creating project "%s"...' % body[u'name']
create_operation = callGAPI(crm.projects(), u'create', body=body) create_operation = callGAPI(crm.projects(), u'create',
body=body)
operation_name = create_operation[u'name'] operation_name = create_operation[u'name']
time.sleep(5) # Google recommends always waiting at least 5 seconds time.sleep(5) # Google recommends always waiting at least 5 seconds
for i in range(1, 5): for i in range(1, 5):
print u'Checking project status...' print u'Checking project status...'
status = callGAPI(crm.operations(), u'get', name=operation_name) status = callGAPI(crm.operations(), u'get',
name=operation_name)
if u'error' in status: if u'error' in status:
if (u'message' in status[u'error'] and if status[u'error'].get(u'message', u'') == u'No permission to create project in organization':
status[u'error'][u'message'] == u'No permission to create project in organization'):
print u'Hmm... Looks like you have no rights to your Google Cloud Organization.' print u'Hmm... Looks like you have no rights to your Google Cloud Organization.'
print u'Attempting to fix that...' print u'Attempting to fix that...'
search_body = {u'filter': u'domain:%s' % login_domain} getorg = callGAPI(crm.organizations(), u'search',
getorg = callGAPI(crm.organizations(), u'search', body=search_body) body={u'filter': u'domain:%s' % login_domain})
try: try:
organization = getorg[u'organizations'][0][u'name'] organization = getorg[u'organizations'][0][u'name']
print u'Your organization name is %s' % organization print u'Your organization name is %s' % organization
except (KeyError, IndexError): except (KeyError, IndexError):
print u'ERROR: 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.' print u'ERROR: 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.'
sys.exit(3) sys.exit(3)
org_policy = callGAPI(crm.organizations(), u'getIamPolicy', resource=organization, body={}) org_policy = callGAPI(crm.organizations(), u'getIamPolicy',
resource=organization, body={})
if u'bindings' not in org_policy: if u'bindings' not in org_policy:
org_policy[u'bindings'] = [] org_policy[u'bindings'] = []
print u'Looks like no one has rights to your Google Cloud Organization. Attempting to give you create rights...' print u'Looks like no one has rights to your Google Cloud Organization. Attempting to give you create rights...'
@ -6827,10 +6836,9 @@ def doCreateProject(login_hint=None):
print print
my_role = u'roles/resourcemanager.projectCreator' my_role = u'roles/resourcemanager.projectCreator'
print u'Giving %s the role of %s...' % (login_hint, my_role) print u'Giving %s the role of %s...' % (login_hint, my_role)
my_rights = {u'role': my_role, u'members': [u'user:%s' % login_hint]} org_policy[u'bindings'].append({u'role': my_role, u'members': [u'user:%s' % login_hint]})
org_policy[u'bindings'].append(my_rights) callGAPI(crm.organizations(), u'setIamPolicy',
callGAPI(crm.organizations(), u'setIamPolicy', resource=organization, resource=organization, body={u'policy': org_policy})
body={u'policy': org_policy})
create_again = True create_again = True
break break
try: try:
@ -6847,14 +6855,14 @@ and accept the Terms of Service (ToS). As soon as you've accepted the ToS popup,
pass pass
print status print status
sys.exit(1) sys.exit(1)
if u'done' in status and status[u'done']: if status.get(u'done', False):
break break
sleep_time = i ** 2 sleep_time = i ** 2
print u'Project still being created. Sleeping %s seconds' % sleep_time print u'Project still being created. Sleeping %s seconds' % sleep_time
time.sleep(sleep_time) time.sleep(sleep_time)
if create_again: if create_again:
continue continue
if not u'done' in status or not status[u'done']: if not status.get(u'done', False):
print u'Failed to create project: %s' % status print u'Failed to create project: %s' % status
sys.exit(1) sys.exit(1)
elif u'error' in status: elif u'error' in status:
@ -6862,28 +6870,14 @@ and accept the Terms of Service (ToS). As soon as you've accepted the ToS popup,
sys.exit(2) sys.exit(2)
break break
simplehttp = httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL]) simplehttp = httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL])
_, c = simplehttp.request(GAM_PROJECT_APIS, u'GET') enableProjectAPIs(simplehttp, httpObj, project_name, False)
apis = c.splitlines() iam = googleapiclient.discovery.build(u'iam', u'v1', http=httpObj, cache_discovery=False)
serveman = googleapiclient.discovery.build(u'servicemanagement', u'v1', http=http, cache_discovery=False)
for api in apis:
while True:
print u' enabling API %s...' % api
try:
callGAPI(serveman.services(), u'enable', throw_reasons=[u'failedPrecondition'],
serviceName=api, body={u'consumerId': project_name})
break
except googleapiclient.errors.HttpError, e:
print u'\nThere was an error enabling %s. Please resolve error as described below:' % api
print
print u'\n%s\n' % e
print
raw_input(u'Press enter once resolved and we will try enabling the API again.')
iam = googleapiclient.discovery.build(u'iam', u'v1', http=http, cache_discovery=False)
print u'Creating Service Account' print u'Creating Service Account'
service_account = callGAPI(iam.projects().serviceAccounts(), u'create', name=u'projects/%s' % project_id, service_account = callGAPI(iam.projects().serviceAccounts(), u'create',
name=u'projects/%s' % project_id,
body={u'accountId': project_id, u'serviceAccount': {u'displayName': u'GAM Project'}}) body={u'accountId': project_id, u'serviceAccount': {u'displayName': u'GAM Project'}})
body = {u'privateKeyType': u'TYPE_GOOGLE_CREDENTIALS_FILE', u'keyAlgorithm': u'KEY_ALG_RSA_2048'} key = callGAPI(iam.projects().serviceAccounts().keys(), u'create',
key = callGAPI(iam.projects().serviceAccounts().keys(), u'create', name=service_account[u'name'], body=body) name=service_account[u'name'], body={u'privateKeyType': u'TYPE_GOOGLE_CREDENTIALS_FILE', u'keyAlgorithm': u'KEY_ALG_RSA_2048'})
oauth2service_data = base64.b64decode(key[u'privateKeyData']) oauth2service_data = base64.b64decode(key[u'privateKeyData'])
writeFile(service_account_file, oauth2service_data, continueOnError=False) writeFile(service_account_file, oauth2service_data, continueOnError=False)
console_credentials_url = u'https://console.developers.google.com/apis/credentials?project=%s' % project_id console_credentials_url = u'https://console.developers.google.com/apis/credentials?project=%s' % project_id