Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/ansys/hps/client/jms/api/jms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import json
import logging
import os
from typing import List, Union
from typing import Dict, List, Union
import uuid

import backoff
Expand Down Expand Up @@ -78,7 +78,7 @@ def url(self) -> str:
def fs_url(self) -> str:
"""URL of the file storage gateway."""
if self._fs_url is None:
self._fs_url = get_fs_url(self.client, self.url)
self._fs_url = _find_available_fs_url(self.get_storage())
return self._fs_url

def get_api_info(self):
Expand Down Expand Up @@ -136,7 +136,7 @@ def restore_project(self, path: str) -> Project:
Path of the archive file.

"""
return restore_project(self, path)
return _restore_project(self, path)

################################################################
# Task Definition Templates
Expand Down Expand Up @@ -275,7 +275,7 @@ def monitor_operation(self, operation_id: str, max_value: float = 5.0, max_time:
# Storages
def get_storage(self):
"""Get a list of storages."""
return get_storages(self.client, self.url)
return _get_storages(self.client, self.url)


def get_projects(client, api_url, as_objects=True, **query_params) -> List[Project]:
Expand Down Expand Up @@ -409,7 +409,7 @@ def _copy_objects(
return op.result["destination_ids"]


def restore_project(jms_api, archive_path):
def _restore_project(jms_api, archive_path):
"""Restore an archived project."""
if not os.path.exists(archive_path):
raise HPSError(f"Project archive: path does not exist {archive_path}")
Expand Down Expand Up @@ -454,7 +454,7 @@ def restore_project(jms_api, archive_path):
return get_project(jms_api.client, jms_api.url, project_id)


def get_storages(client, api_url):
def _get_storages(client: Client, api_url: str) -> List[Dict]:
"""
Get a list of storages.
"""
Expand All @@ -463,26 +463,30 @@ def get_storages(client, api_url):
return r.json()["backends"]


def get_fs_url(client, api_url):
"""Get the file storage URL."""
file_storages = get_storages(client, api_url)
def _find_available_fs_url(file_storages: Dict) -> str:
"""Find first available file storage URL."""

if not file_storages:
raise HPSError(f"There is no file storage information.")
raise HPSError("There is no file storage information.")

rest_gateways = [fs for fs in file_storages if fs["obj_type"] == "RestGateway"]
rest_gateways.sort(key=lambda fs: fs["priority"])

if not rest_gateways:
raise HPSError(f"There is no REST gateway defined.")
raise HPSError("There is no file storage gateway defined.")

for d in rest_gateways:
url = d["url"]
try:
r = requests.get(url, verify=False, timeout=2)
is_ansft = r.json()["ansft"]
except Exception as ex:
log.debug(ex)
continue
if r.status_code == 200:
if r.status_code == 200 and is_ansft:
return url
return None

raise HPSError(
f"All defined file storage gateways are unavailable"
f" ({', '.join([d['url'] for d in rest_gateways])})."
)
17 changes: 16 additions & 1 deletion tests/jms/test_jms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
from marshmallow.utils import missing
import pytest

from ansys.hps.client import Client, ClientError
from ansys.hps.client import Client, ClientError, HPSError
from ansys.hps.client.jms import JmsApi, ProjectApi
from ansys.hps.client.jms.api.jms_api import _find_available_fs_url
from ansys.hps.client.jms.resource import (
FloatParameterDefinition,
IntParameterDefinition,
Expand All @@ -53,6 +54,20 @@ def test_jms_api_info(client):
assert "time" in info


def test_unavailable_fs_url(client):

storage_config = JmsApi(client).get_storage()

for config in storage_config:
if config["obj_type"] == "RestGateway":
config["url"] = config["url"].replace("v1", "v234")

with pytest.raises(HPSError) as ex_info:
_find_available_fs_url(storage_config)

assert "unavailable" in str(ex_info.value)


def test_jms_api(client):

log.debug("=== Client ===")
Expand Down