Skip to content

Commit b3bb58a

Browse files
committed
[FSTORE-1454] Unify variable_api and project_api (logicalclocks#219)
* Merge variable_api of hsfs * Merge project_api of hsfs * Ruff project_api * Fix client/external so that Project API get_client works That is, set _project_id. * Fix Project API docs * Add docs to variable_api * Ruff client/external.py * Refactor variable_api
1 parent 19c2075 commit b3bb58a

File tree

3 files changed

+97
-13
lines changed

3 files changed

+97
-13
lines changed

python/hopsworks/client/external.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
# limitations under the License.
1515
#
1616

17-
import os
1817
import base64
19-
import requests
18+
import os
2019

21-
from hopsworks.client import base, auth, exceptions
20+
import requests
21+
from hopsworks.client import auth, base, exceptions
2222

2323

2424
class Client(base.Client):
@@ -41,6 +41,11 @@ def __init__(
4141
self._port = port
4242
self._base_url = "https://" + self._host + ":" + str(self._port)
4343
self._project_name = project
44+
if project is not None:
45+
project_info = self._get_project_info(project)
46+
self._project_id = str(project_info["projectId"])
47+
else:
48+
self._project_id = None
4449

4550
if api_key_value is not None:
4651
api_key = api_key_value

python/hopsworks/core/project_api.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# limitations under the License.
1515
#
1616

17-
from hopsworks import client, project, constants
1817
import json
18+
19+
from hopsworks import client, constants, project
1920
from hopsworks.client.exceptions import RestAPIError
2021

2122

@@ -27,8 +28,6 @@ def _exists(self, name: str):
2728
name: Name of the project.
2829
# Returns
2930
`bool`: True if project exists, otherwise False
30-
# Raises
31-
`RestAPIError`: If unable to check the existence of the project
3231
"""
3332
try:
3433
self._get_project(name)
@@ -111,3 +110,12 @@ def _create_project(
111110
project = self._get_project(name)
112111
print("Project created successfully, explore it at " + project.get_url())
113112
return project
113+
114+
def get_client(self):
115+
_client = client.get_instance()
116+
path_params = [
117+
"project",
118+
_client._project_id,
119+
"client",
120+
]
121+
return _client._send_request("GET", path_params, stream=True)

python/hopsworks/core/variable_api.py

+78-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2022 Logical Clocks AB
2+
# Copyright 2022 Hopsworks AB
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -13,16 +13,29 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
from __future__ import annotations
17+
18+
import re
19+
from typing import Optional, Tuple
1620

1721
from hopsworks import client
22+
from hopsworks.client.exceptions import RestAPIError
1823

1924

2025
class VariableApi:
2126
def __init__(self):
2227
pass
2328

2429
def get_variable(self, variable: str):
25-
"""Get the configured value for a variable"""
30+
"""Get the configured value of a variable.
31+
32+
# Arguments
33+
vairable: Name of the variable.
34+
# Returns
35+
The vairable's value
36+
# Raises
37+
`RestAPIError`: If unable to get the variable
38+
"""
2639

2740
_client = client.get_instance()
2841

@@ -31,15 +44,73 @@ def get_variable(self, variable: str):
3144

3245
return domain["successMessage"]
3346

34-
def get_version(self, software: str):
47+
def get_version(self, software: str) -> Optional[str]:
48+
"""Get version of a software component.
49+
50+
# Arguments
51+
software: Name of the software.
52+
# Returns
53+
The software's version, if the software is available, otherwise `None`.
54+
# Raises
55+
`RestAPIError`: If unable to get the version
56+
"""
57+
3558
_client = client.get_instance()
36-
path_params = [
37-
"variables",
38-
"versions",
39-
]
4059

60+
path_params = ["variables", "versions"]
4161
resp = _client._send_request("GET", path_params)
62+
4263
for entry in resp:
4364
if entry["software"] == software:
4465
return entry["version"]
4566
return None
67+
68+
def parse_major_and_minor(
69+
self, backend_version: str
70+
) -> Tuple[Optional[str], Optional[str]]:
71+
"""Extract major and minor version from full version.
72+
73+
# Arguments
74+
backend_version: The full version.
75+
# Returns
76+
(major, minor): The pair of major and minor parts of the version, or (None, None) if the version format is incorrect.
77+
"""
78+
79+
version_pattern = r"(\d+)\.(\d+)"
80+
matches = re.match(version_pattern, backend_version)
81+
82+
if matches is None:
83+
return (None, None)
84+
return matches.group(1), matches.group(2)
85+
86+
def get_flyingduck_enabled(self) -> bool:
87+
"""Check if Flying Duck is enabled on the backend.
88+
89+
# Returns
90+
`True`: If flying duck is availalbe, `False` otherwise.
91+
# Raises
92+
`RestAPIError`: If unable to obtain the flag's value.
93+
"""
94+
return self.get_variable("enable_flyingduck") == "true"
95+
96+
def get_loadbalancer_external_domain(self) -> str:
97+
"""Get domain of external loadbalancer.
98+
99+
# Returns
100+
`str`: The domain of external loadbalancer, if it is set up, otherwise empty string `""`.
101+
"""
102+
try:
103+
return self.get_variable("loadbalancer_external_domain")
104+
except RestAPIError:
105+
return ""
106+
107+
def get_service_discovery_domain(self) -> str:
108+
"""Get domain of service discovery server.
109+
110+
# Returns
111+
`str`: The domain of service discovery server, if it is set up, otherwise empty string `""`.
112+
"""
113+
try:
114+
return self.get_variable("service_discovery_domain")
115+
except RestAPIError:
116+
return ""

0 commit comments

Comments
 (0)