Skip to content

Commit

Permalink
Retry key creation after SA create
Browse files Browse the repository at this point in the history
  • Loading branch information
jay0lee committed May 15, 2020
1 parent 6b4a22d commit 9d95cdb
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions gyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def buildGAPIServiceObject(api, soft_errors=False):
e = e.args[0]
systemErrorExit(5, e)

def callGAPI(service, function, soft_errors=False, throw_reasons=[], **kwargs):
def callGAPI(service, function, soft_errors=False, throw_reasons=[], retry_reasons=[], **kwargs):
retries = 10
parameters = kwargs.copy()
parameters.update(extra_args)
Expand All @@ -589,12 +589,13 @@ def callGAPI(service, function, soft_errors=False, throw_reasons=[], **kwargs):
message = error['error']['errors'][0]['message']
except (KeyError, json.decoder.JSONDecodeError):
http_status = int(e.resp['status'])
reason = e.content
reason = http_status
message = e.content
if reason in throw_reasons:
raise
if n != retries and (http_status >= 500 or
reason in ['rateLimitExceeded', 'userRateLimitExceeded', 'backendError']):
reason in ['rateLimitExceeded', 'userRateLimitExceeded', 'backendError'] or
reason in retry_reasons):
wait_on_fail = (2 ** n) if (2 ** n) < 60 else 60
randomness = float(random.randint(1,1000)) / 1000
wait_on_fail += randomness
Expand Down Expand Up @@ -1005,11 +1006,21 @@ def doCreateProject():
cache_discovery=False,
discoveryServiceUrl=googleapiclient.discovery.V2_DISCOVERY_URI)
print('Creating Service Account')
sa_body = {
'accountId': project_id,
'serviceAccount': {
'displayName': 'GYB Project Service Account'
}
}
service_account = callGAPI(iam.projects().serviceAccounts(), 'create',
name='projects/%s' % project_id,
body={'accountId': project_id, 'serviceAccount': {'displayName': 'GYB Project Service Account'}})
body=sa_body)
key_body = {
'privateKeyType': 'TYPE_GOOGLE_CREDENTIALS_FILE',
'keyAlgorithm': 'KEY_ALG_RSA_2048'
}
key = callGAPI(iam.projects().serviceAccounts().keys(), 'create',
name=service_account['name'], body={'privateKeyType': 'TYPE_GOOGLE_CREDENTIALS_FILE', 'keyAlgorithm': 'KEY_ALG_RSA_2048'})
name=service_account['name'], body=key_body, retry_reasons=[404])
oauth2service_data = base64.b64decode(key['privateKeyData'])
writeFile(service_account_file, oauth2service_data, continueOnError=False)
_createClientSecretsOauth2service(project_id)
Expand Down

0 comments on commit 9d95cdb

Please sign in to comment.