Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove checks for storage_server.json in storage #8939

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
57 changes: 1 addition & 56 deletions src/ert/dark_storage/client/_session.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,8 @@
import json
import os
from pathlib import Path
from typing import Optional

from pydantic import BaseModel, ValidationError
from pydantic import BaseModel


class ConnInfo(BaseModel):
base_url: str
auth_token: Optional[str] = None


ENV_VAR = "ERT_STORAGE_CONNECTION_STRING"

# Avoid searching for the connection information on every request. We assume
# that a single client process will only ever want to connect to a single ERT
# Storage server during its lifetime, so we don't provide an API for managing
# this cache.
_CACHED_CONN_INFO: Optional[ConnInfo] = None


def find_conn_info() -> ConnInfo:
"""
The base url and auth token are read from either:
The file `storage_server.json`, starting from the current working directory
or the environment variable `ERT_STORAGE_CONNECTION_STRING`

In both cases the configuration is represented by JSON representation of the
`ConnInfo` pydantic model.

In the event that nothing is found, a RuntimeError is raised.
"""
global _CACHED_CONN_INFO # noqa: PLW0603
if _CACHED_CONN_INFO is not None:
return _CACHED_CONN_INFO

conn_str = os.environ.get(ENV_VAR)

# This could be an empty string rather than None, as by the shell
# invocation: env ERT_STORAGE_CONNECTION_STRING= python
if not conn_str:
# Look for `storage_server.json` from cwd up to root.
root = Path("/")
path = Path.cwd()
while path != root:
try:
conn_str = (path / "storage_server.json").read_text()
break
except FileNotFoundError:
path = path.parent

if not conn_str:
raise RuntimeError("No Storage connection configuration found")

try:
conn_info = ConnInfo.parse_obj(json.loads(conn_str))
_CACHED_CONN_INFO = conn_info
return conn_info
except json.JSONDecodeError as e:
raise RuntimeError("Invalid storage conneciton configuration") from e
except ValidationError as e:
raise RuntimeError("Invalid storage conneciton configuration") from e
9 changes: 2 additions & 7 deletions src/ert/dark_storage/client/async_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Optional

import httpx

from ._session import ConnInfo, find_conn_info
from ._session import ConnInfo


class AsyncClient(httpx.AsyncClient):
Expand All @@ -11,10 +9,7 @@ class AsyncClient(httpx.AsyncClient):
interact with ERT Storage's API
"""

def __init__(self, conn_info: Optional[ConnInfo] = None) -> None:
if conn_info is None:
conn_info = find_conn_info()

def __init__(self, conn_info: ConnInfo) -> None:
headers = {}
if conn_info.auth_token is not None:
headers = {"Token": conn_info.auth_token}
Expand Down
9 changes: 2 additions & 7 deletions src/ert/dark_storage/client/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Optional

import httpx

from ._session import ConnInfo, find_conn_info
from ._session import ConnInfo


class Client(httpx.Client):
Expand All @@ -11,10 +9,7 @@ class Client(httpx.Client):
interact with ERT Storage's API
"""

def __init__(self, conn_info: Optional[ConnInfo] = None) -> None:
if conn_info is None:
conn_info = find_conn_info()

def __init__(self, conn_info: ConnInfo) -> None:
headers = {}
if conn_info.auth_token is not None:
headers = {"Token": conn_info.auth_token}
Expand Down