Skip to content

Commit 001ae9b

Browse files
Merge pull request #35 from Shubha-accenture/sprint10-bug-fixes
Sprint10 bug fixes
2 parents 65faf8d + fa741cd commit 001ae9b

19 files changed

+611
-306
lines changed

dataproc_jupyter_plugin/handlers.py

+38-33
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
from jupyter_server.utils import url_path_join
1818
import tornado
1919
import subprocess
20-
from datetime import datetime
21-
from datetime import datetime
2220
from cachetools import TTLCache
21+
import datetime
2322

2423
from google.cloud.jupyter_config.config import gcp_kernel_gateway_url
2524

@@ -46,14 +45,20 @@ def get_cached_credentials():
4645
if process.returncode == 0:
4746
config_data = json.loads(output)
4847
credentials = {
49-
'project_id': config_data['configuration']['properties']['core']['project'],
50-
'region_id': config_data['configuration']['properties']['compute']['region'],
51-
'access_token': config_data['credential']['access_token']
52-
}
48+
'project_id': config_data['configuration']['properties']['core']['project'],
49+
'region_id': config_data['configuration']['properties']['compute']['region'],
50+
'access_token': config_data['credential']['access_token']
51+
}
5352

5453
token_expiry = config_data['credential']['token_expiry']
55-
utc_datetime = datetime.strptime(token_expiry, '%Y-%m-%dT%H:%M:%SZ')
56-
ttl_seconds = max(0, (utc_datetime - datetime.utcnow()).total_seconds())
54+
utc_datetime = datetime.datetime.strptime(token_expiry, '%Y-%m-%dT%H:%M:%SZ')
55+
current_utc_datetime = datetime.datetime.utcnow()
56+
expiry_timedelta = utc_datetime - current_utc_datetime
57+
expiry_seconds = expiry_timedelta.total_seconds()
58+
if expiry_seconds > 1000:
59+
ttl_seconds = 1000
60+
else:
61+
ttl_seconds = expiry_seconds
5762
credentials_cache = TTLCache(maxsize=1, ttl=ttl_seconds)
5863
credentials_cache['credentials'] = credentials
5964
return credentials
@@ -66,41 +71,41 @@ def get_cached_credentials():
6671
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
6772
project, error = process.communicate()
6873
if project == '':
69-
credentials = {
70-
'project_id': '',
71-
'region_id': '',
72-
'access_token': '',
73-
'config_error': 1,
74-
'login_error': 0
75-
}
74+
credentials = {
75+
'project_id': '',
76+
'region_id': '',
77+
'access_token': '',
78+
'config_error': 1,
79+
'login_error': 0
80+
}
7681
else:
7782
cmd = "gcloud config get-value compute/region"
7883
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
7984
region, error = process.communicate()
8085
if region == '':
8186
credentials = {
82-
'project_id': '',
83-
'region_id': '',
84-
'access_token': '',
85-
'config_error': 1,
86-
'login_error': 0
87-
}
87+
'project_id': '',
88+
'region_id': '',
89+
'access_token': '',
90+
'config_error': 1,
91+
'login_error': 0
92+
}
8893
else:
8994
credentials = {
90-
'project_id': '',
91-
'region_id': '',
92-
'access_token': '',
93-
'config_error': 0,
94-
'login_error': 1
95-
}
95+
'project_id': '',
96+
'region_id': '',
97+
'access_token': '',
98+
'config_error': 0,
99+
'login_error': 1
100+
}
96101
else:
97102
credentials = {
98-
'project_id': '',
99-
'region_id': '',
100-
'access_token': '',
101-
'config_error': 1,
102-
'login_error': 0
103-
}
103+
'project_id': '',
104+
'region_id': '',
105+
'access_token': '',
106+
'config_error': 1,
107+
'login_error': 0
108+
}
104109
credentials_cache = TTLCache(maxsize=1, ttl=5)
105110
credentials_cache['credentials'] = credentials
106111
return credentials

0 commit comments

Comments
 (0)