Skip to content

Commit 4199108

Browse files
committed
Wrap url mapping logic in a function
1 parent a1f5c23 commit 4199108

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

api/PclusterApiHandler.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
USER_POOL_ID = os.getenv("USER_POOL_ID")
3333
AUTH_PATH = os.getenv("AUTH_PATH")
3434
API_BASE_URL = os.getenv("API_BASE_URL")
35-
API_VERSION = sorted(os.getenv("API_VERSION", "3.1.0").split(","), key=lambda x: [-int(n) for n in x.split('.')])
35+
API_VERSION = sorted(os.getenv("API_VERSION", "3.1.0").strip().split(","), key=lambda x: [-int(n) for n in x.split('.')])
3636
DEFAULT_API_VERSION = API_VERSION[0]
3737
API_USER_ROLE = os.getenv("API_USER_ROLE")
3838
OIDC_PROVIDER = os.getenv("OIDC_PROVIDER")
3939
CLIENT_ID = os.getenv("CLIENT_ID")
4040
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
4141
SECRET_ID = os.getenv("SECRET_ID")
42-
SITE_URL = os.getenv("SITE_URL")
4342
SCOPES_LIST = os.getenv("SCOPES_LIST")
4443
REGION = os.getenv("AWS_DEFAULT_REGION")
4544
TOKEN_URL = os.getenv("TOKEN_URL", f"{AUTH_PATH}/oauth2/token")
@@ -49,6 +48,7 @@
4948
AUDIENCE = os.getenv("AUDIENCE")
5049
USER_ROLES_CLAIM = os.getenv("USER_ROLES_CLAIM", "cognito:groups")
5150
SSM_LOG_GROUP_NAME = os.getenv("SSM_LOG_GROUP_NAME")
51+
ARG_VERSION="version"
5252

5353
try:
5454
if (not USER_POOL_ID or USER_POOL_ID == "") and SECRET_ID:
@@ -63,13 +63,18 @@
6363
if not JWKS_URL:
6464
JWKS_URL = os.getenv("JWKS_URL",
6565
f"https://cognito-idp.{REGION}.amazonaws.com/{USER_POOL_ID}/" ".well-known/jwks.json")
66-
API_BASE_URL_MAPPING = {}
6766

68-
if API_BASE_URL:
69-
for url in API_BASE_URL.split(","):
70-
if url:
71-
pair=url.split("=")
72-
API_BASE_URL_MAPPING[pair[0]] = pair[1]
67+
def create_url_map(url_list):
68+
url_map = {}
69+
if url_list:
70+
for url in url_list.split(","):
71+
if url:
72+
pair=url.split("=")
73+
url_map[pair[0]] = pair[1]
74+
return url_map
75+
76+
API_BASE_URL_MAPPING = create_url_map(API_BASE_URL)
77+
SITE_URL = os.getenv("SITE_URL", API_BASE_URL_MAPPING.get(DEFAULT_API_VERSION))
7378

7479

7580

@@ -242,9 +247,9 @@ def ec2_action():
242247
def get_cluster_config_text(cluster_name, region=None):
243248
url = f"/v3/clusters/{cluster_name}"
244249
if region:
245-
info_resp = sigv4_request("GET", get_base_url(request.args.get("version")), url, params={"region": region})
250+
info_resp = sigv4_request("GET", get_base_url(request), url, params={"region": region})
246251
else:
247-
info_resp = sigv4_request("GET", get_base_url(request.args.get("version")), url)
252+
info_resp = sigv4_request("GET", get_base_url(request), url)
248253
if info_resp.status_code != 200:
249254
abort(info_resp.status_code)
250255

@@ -493,7 +498,7 @@ def get_dcv_session():
493498

494499

495500
def get_custom_image_config():
496-
image_info = sigv4_request("GET", get_base_url(request.args.get("version")), f"/v3/images/custom/{request.args.get('image_id')}").json()
501+
image_info = sigv4_request("GET", get_base_url(request), f"/v3/images/custom/{request.args.get('image_id')}").json()
497502
configuration = requests.get(image_info["imageConfiguration"]["url"])
498503
return configuration.text
499504

@@ -744,9 +749,10 @@ def _get_params(_request):
744749
params.pop("path")
745750
return params
746751

747-
def get_base_url(v):
748-
if v and str(v) in API_VERSION:
749-
return API_BASE_URL_MAPPING[str(v)]
752+
def get_base_url(request):
753+
version = request.args.get(ARG_VERSION)
754+
if version and str(version) in API_VERSION:
755+
return API_BASE_URL_MAPPING[str(version)]
750756
return API_BASE_URL_MAPPING[DEFAULT_API_VERSION]
751757

752758

@@ -756,7 +762,7 @@ def get_base_url(v):
756762
@authenticated({'admin'})
757763
@validated(params=PCProxyArgs)
758764
def pc_proxy_get():
759-
response = sigv4_request(request.method, get_base_url(request.args.get("version")), request.args.get("path"), _get_params(request))
765+
response = sigv4_request(request.method, get_base_url(request), request.args.get("path"), _get_params(request))
760766
return response.json(), response.status_code
761767

762768
@pc.route('/', methods=['POST','PUT','PATCH','DELETE'], strict_slashes=False)
@@ -770,5 +776,5 @@ def pc_proxy():
770776
except:
771777
pass
772778

773-
response = sigv4_request(request.method, get_base_url(request.args.get("version")), request.args.get("path"), _get_params(request), body=body)
779+
response = sigv4_request(request.method, get_base_url(request), request.args.get("path"), _get_params(request), body=body)
774780
return response.json(), response.status_code

api/tests/test_pcluster_api_handler.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
from unittest import mock
2-
from api.PclusterApiHandler import login, get_base_url
2+
from api.PclusterApiHandler import login, get_base_url, create_url_map
3+
4+
class MockRequest:
5+
cookies = {'int_value': 100}
6+
args = {'version': '3.12.0'}
7+
json = {'username': '[email protected]'}
38

49

510
@mock.patch("api.PclusterApiHandler.requests.post")
@@ -33,5 +38,8 @@ def test_get_base_url(monkeypatch):
3338
monkeypatch.setattr('api.PclusterApiHandler.API_BASE_URL', '3.12.0=https://example.com,3.11.0=https://example1.com,')
3439
monkeypatch.setattr('api.PclusterApiHandler.API_BASE_URL_MAPPING', {'3.12.0': 'https://example.com', '3.11.0': 'https://example1.com'})
3540

36-
assert 'https://example.com' == get_base_url('3.12.0')
41+
assert 'https://example.com' == get_base_url(MockRequest())
42+
43+
def test_create_url_map():
44+
assert {'3.12.0': 'https://example.com', '3.11.0': 'https://example1.com'} == create_url_map('3.12.0=https://example.com,3.11.0=https://example1.com,')
3745

frontend/locales/en/strings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496
"version": {
497497
"label": "Cluster Version",
498498
"title": "Version",
499-
"placeholder": "Select your cluster version",
499+
"placeholder": "Cluster version",
500500
"description": "Select the AWS ParallelCluster version to use for this cluster.",
501501
"help": {
502502
"main": "Choose the version of AWS ParallelCluster to use for creating and managing your cluster."

0 commit comments

Comments
 (0)