8000 Fixing issue 7277 (#7370) · sreza/python-docs-samples@eea819b · GitHub
[go: up one dir, main page]

Skip to content

Commit eea819b

Browse files
authored
Fixing issue 7277 (GoogleCloudPlatform#7370)
* WIP: Triggering a new PR to have a stage for running tests and experimenting. * fix(ssh-test): Trying to fix GoogleCloudPlatform#7277 * Import reordering
1 parent 797dc75 commit eea819b

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

compute/oslogin/service_account_ssh.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
import time
2828
import uuid
2929

30+
from google.auth.exceptions import RefreshError
3031
import googleapiclient.discovery
3132
import requests
3233

33-
34-
# Global variables
3534
SERVICE_ACCOUNT_METADATA_URL = (
3635
'http://metadata.google.internal/computeMetadata/v1/instance/'
3736
'service-accounts/default/email')
@@ -76,7 +75,19 @@ def create_ssh_key(oslogin, account, private_key_file=None, expire_time=300):
7675
'key': public_key,
7776
'expirationTimeUsec': expiration,
7877
}
79-
oslogin.users().importSshPublicKey(parent=account, body=body).execute()
78+
print(f'Creating key {account} and {body}')
79+
for attempt_no in range(1, 4):
80+
try:
81+
# This method sometimes failed to work causing issues like #7277
82+
# Maybe retrying it with some delay will make things better
83+
oslogin.users().importSshPublicKey(parent=account, body=body).execute()
84+
except RefreshError:
85+
if attempt_no == 3:
86+
break
87+
time.sleep(attempt_no)
88+
else:
89+
break
90+
8091
return private_key_file
8192
# [END create_key]
8293

0 commit comments

Comments
 (0)
0