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

[FSTORE-1454] Unify variable_api and project_api #219

Merged
merged 8 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 8 additions & 3 deletions python/hopsworks/client/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
# limitations under the License.
#

import os
import base64
import requests
import os

from hopsworks.client import base, auth, exceptions
import requests
from hopsworks.client import auth, base, exceptions


class Client(base.Client):
Expand All @@ -41,6 +41,11 @@ def __init__(
self._port = port
self._base_url = "https://" + self._host + ":" + str(self._port)
self._project_name = project
if project is not None:
project_info = self._get_project_info(project)
self._project_id = str(project_info["projectId"])
else:
self._project_id = None

if api_key_value is not None:
api_key = api_key_value
Expand Down
14 changes: 11 additions & 3 deletions python/hopsworks/core/project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# limitations under the License.
#

from hopsworks import client, project, constants
import json

from hopsworks import client, constants, project
from hopsworks.client.exceptions import RestAPIError


Expand All @@ -27,8 +28,6 @@ def _exists(self, name: str):
name: Name of the project.
# Returns
`bool`: True if project exists, otherwise False
# Raises
`RestAPIError`: If unable to check the existence of the project
"""
try:
self._get_project(name)
Expand Down Expand Up @@ -111,3 +110,12 @@ def _create_project(
project = self._get_project(name)
print("Project created successfully, explore it at " + project.get_url())
return project

def get_client(self):
_client = client.get_instance()
path_params = [
"project",
_client._project_id,
"client",
]
return _client._send_request("GET", path_params, stream=True)
101 changes: 98 additions & 3 deletions python/hopsworks/core/variable_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2022 Logical Clocks AB
# Copyright 2022 Hopsworks AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,16 +13,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

import re
from typing import Optional, Tuple

from hopsworks import client
from hopsworks.client.exceptions import RestAPIError


class VariableApi:
def __init__(self):
pass

def get_variable(self, variable: str):
"""Get the configured value for a variable"""
"""Get the configured value of a variable.

# Arguments
vairable: Name of the variable.
# Returns
The vairable's value
# Raises
`RestAPIError`: If unable to get the variable
"""

_client = client.get_instance()

Expand All @@ -31,7 +44,17 @@ def get_variable(self, variable: str):

return domain["successMessage"]

def get_version(self, software: str):
def get_version(self, software: str) -> Optional[str]:
"""Get version of a software component.

# Arguments
software: Name of the software.
# Returns
The software's version, if the software is available, otherwise `None`.
# Raises
`RestAPIError`: If unable to get the version
"""

_client = client.get_instance()
path_params = [
"variables",
Expand All @@ -43,3 +66,75 @@ def get_version(self, software: str):
if entry["software"] == software:
return entry["version"]
return None

def parse_major_and_minor(self, backend_version: str) -> Tuple[Optional[str], Optional[str]]:
"""Extract major and minor version from full version.

# Arguments
backend_version: The full version.
# Returns
(major, minor): The pair of major and minor parts of the version, or (None, None) if the version format is incorrect.
"""

version_pattern = r"(\d+)\.(\d+)"
matches = re.match(version_pattern, backend_version)

if matches is None:
return (None, None)
return matches.group(1), matches.group(2)

def get_flyingduck_enabled(self) -> bool:
"""Check if Flying Duck is enabled on the backend.

# Returns
`True`: If flying duck is availalbe, `False` otherwise.
# Raises
`RestAPIError`: If unable to obtain the flag's value.
"""

_client = client.get_instance()
path_params = [
"variables",
"enable_flyingduck",
]

resp = _client._send_request("GET", path_params)
return resp["successMessage"] == "true"
aversey marked this conversation as resolved.
Show resolved Hide resolved

def get_loadbalancer_external_domain(self) -> str:
"""Get domain of external loadbalancer.

# Returns
`str`: The domain of external loadbalancer, if it is set up, otherwise empty string `""`.
"""

_client = client.get_instance()
path_params = [
"variables",
"loadbalancer_external_domain",
]

try:
resp = _client._send_request("GET", path_params)
return resp["successMessage"]
except RestAPIError:
return ""

def get_service_discovery_domain(self) -> str:
"""Get domain of service discovery server.

# Returns
`str`: The domain of service discovery server, if it is set up, otherwise empty string `""`.
"""

_client = client.get_instance()
path_params = [
"variables",
"service_discovery_domain",
]

try:
resp = _client._send_request("GET", path_params)
return resp["successMessage"]
except RestAPIError:
return ""