Skip to content

Commit b18ae18

Browse files
authored
Expose various attributes on SlurmRestApi instances (#231)
So that downstream applications can e.g. check expiry on the user token. Load user_token from external file in SlurmRestApi rather than in the zocalo.configuration Slurm plugin to allow token changes to be picked up without reloading zocalo.configuration (which may have side effects such as duplicating graylog logging handlers.
1 parent 506ee23 commit b18ae18

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ History
44

55
Unreleased
66
----------
7+
* Expose ``url``, ``version``, ``user_name`` and ``user_token`` attributes on ``SlurmRestApi`` instances
8+
* Load ``user_token`` from external file in ``SlurmRestApi`` rather than in the ``zocalo.configuration`` Slurm plugin to allow token changes to be picked up without re-loading ``zocalo.configuration``
79

810
0.28.0 (2023-03-20)
911
-------------------

src/zocalo/configuration/plugin_slurm.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import os
4-
53
from marshmallow import fields
64

75
from zocalo.configuration import PluginSchema
@@ -16,8 +14,4 @@ class Schema(PluginSchema):
1614

1715
@staticmethod
1816
def activate(configuration):
19-
user_token = configuration.get("user_token")
20-
if user_token and os.path.isfile(user_token):
21-
with open(user_token, "r") as f:
22-
configuration["user_token"] = f.read().strip()
2317
return configuration

src/zocalo/util/slurm/__init__.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3-
from typing import Any, Optional
3+
import os
4+
import pathlib
5+
from typing import Any
46

57
import requests
68

@@ -14,16 +16,23 @@ def __init__(
1416
self,
1517
url: str,
1618
version: str = "v0.0.36",
17-
user_name: Optional[str] = None,
18-
user_token: Optional[str] = None,
19+
user_name: str | None = None,
20+
user_token: str | pathlib.Path | None = None,
1921
):
20-
self._url = url
21-
self._version = version
22-
self._session = requests.Session()
23-
if user_name:
24-
self._session.headers["X-SLURM-USER-NAME"] = user_name
25-
if user_token:
26-
self._session.headers["X-SLURM-USER-TOKEN"] = user_token
22+
self.url = url
23+
self.version = version
24+
self.user_name = user_name
25+
if user_token and os.path.isfile(user_token):
26+
with open(user_token, "r") as f:
27+
self.user_token = f.read().strip()
28+
else:
29+
assert isinstance(user_token, str)
30+
self.user_token = user_token
31+
self.session = requests.Session()
32+
if self.user_name:
33+
self.session.headers["X-SLURM-USER-NAME"] = self.user_name
34+
if self.user_token:
35+
self.session.headers["X-SLURM-USER-TOKEN"] = self.user_token
2736

2837
@classmethod
2938
def from_zocalo_configuration(cls, zc: zocalo.configuration.Configuration):
@@ -37,8 +46,8 @@ def from_zocalo_configuration(cls, zc: zocalo.configuration.Configuration):
3746
def get(
3847
self, endpoint: str, params: dict[str, Any] = None, timeout: float | None = None
3948
) -> requests.Response:
40-
response = self._session.get(
41-
f"{self._url}/{endpoint}", params=params, timeout=timeout
49+
response = self.session.get(
50+
f"{self.url}/{endpoint}", params=params, timeout=timeout
4251
)
4352
response.raise_for_status()
4453
return response
@@ -50,8 +59,8 @@ def put(
5059
json: dict[str, Any] = None,
5160
timeout: float | None = None,
5261
) -> requests.Response:
53-
response = self._session.put(
54-
f"{self._url}/{endpoint}", params=params, json=json, timeout=timeout
62+
response = self.session.put(
63+
f"{self.url}/{endpoint}", params=params, json=json, timeout=timeout
5564
)
5665
response.raise_for_status()
5766
return response
@@ -63,34 +72,34 @@ def post(
6372
json: dict[str, Any] | None = None,
6473
timeout: float | None = None,
6574
) -> requests.Response:
66-
response = self._session.post(
67-
f"{self._url}/{endpoint}", data=data, json=json, timeout=timeout
75+
response = self.session.post(
76+
f"{self.url}/{endpoint}", data=data, json=json, timeout=timeout
6877
)
6978
response.raise_for_status()
7079
return response
7180

7281
def delete(
7382
self, endpoint: str, params: dict[str, Any] = None, timeout: float | None = None
7483
) -> requests.Response:
75-
response = self._session.delete(
76-
f"{self._url}/{endpoint}", params=params, timeout=timeout
84+
response = self.session.delete(
85+
f"{self.url}/{endpoint}", params=params, timeout=timeout
7786
)
7887
response.raise_for_status()
7988
return response
8089

8190
def get_jobs(self) -> models.JobsResponse:
82-
endpoint = f"slurm/{self._version}/jobs"
91+
endpoint = f"slurm/{self.version}/jobs"
8392
response = self.get(endpoint)
8493
return models.JobsResponse(**response.json())
8594

8695
def get_job_info(self, job_id: int) -> models.JobsResponse:
87-
endpoint = f"slurm/{self._version}/job/{job_id}"
96+
endpoint = f"slurm/{self.version}/job/{job_id}"
8897
response = self.get(endpoint)
8998
return models.JobsResponse(**response.json())
9099

91100
def submit_job(
92101
self, job_submission: models.JobSubmission
93102
) -> models.JobSubmissionResponse:
94-
endpoint = f"slurm/{self._version}/job/submit"
103+
endpoint = f"slurm/{self.version}/job/submit"
95104
response = self.post(endpoint, json=job_submission.dict(exclude_defaults=True))
96105
return models.JobSubmissionResponse(**response.json())

tests/configuration/test_plugin_slurm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_user_token_from_external_file(tmp_path):
8181
assert zc.slurm is None
8282
zc.activate_environment("live")
8383
assert isinstance(zc.slurm, dict)
84-
assert zc.slurm["user_token"] == user_token
84+
assert zc.slurm["user_token"] == str(user_token_file)
8585

8686

8787
def test_invalid_config():

tests/util/test_slurm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ def jobs_response():
7777
}
7878

7979

80+
def test_get_slurm_api_from_zocalo_configuration(slurm_api):
81+
assert slurm_api.url == "http://slurm.example.com:1234"
82+
assert slurm_api.version == "v0.0.36"
83+
assert slurm_api.user_name == "foo"
84+
assert slurm_api.user_token == "sometoken"
85+
86+
87+
def test_get_slurm_api_user_token_external_file(tmp_path):
88+
user_token_file = tmp_path / "slurm-user-token"
89+
user_token_file.write_text("foobar")
90+
api = slurm.SlurmRestApi(
91+
url="http://slurm.example.com:1234",
92+
version="v0.0.36",
93+
user_name="foo",
94+
user_token=user_token_file,
95+
)
96+
assert api.user_token == "foobar"
97+
98+
8099
def test_get_jobs(requests_mock, slurm_api, jobs_response):
81100
requests_mock.get(
82101
"/slurm/v0.0.36/jobs",

0 commit comments

Comments
 (0)