1
1
#
2
- # Copyright 2022 Logical Clocks AB
2
+ # Copyright 2022 Hopsworks AB
3
3
#
4
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
5
# you may not use this file except in compliance with the License.
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
#
16
+ from __future__ import annotations
17
+
18
+ import re
19
+ from typing import Optional , Tuple
16
20
17
21
from hopsworks import client
22
+ from hopsworks .client .exceptions import RestAPIError
18
23
19
24
20
25
class VariableApi :
21
26
def __init__ (self ):
22
27
pass
23
28
24
29
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
+ """
26
39
27
40
_client = client .get_instance ()
28
41
@@ -31,15 +44,73 @@ def get_variable(self, variable: str):
31
44
32
45
return domain ["successMessage" ]
33
46
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
+
35
58
_client = client .get_instance ()
36
- path_params = [
37
- "variables" ,
38
- "versions" ,
39
- ]
40
59
60
+ path_params = ["variables" , "versions" ]
41
61
resp = _client ._send_request ("GET" , path_params )
62
+
42
63
for entry in resp :
43
64
if entry ["software" ] == software :
44
65
return entry ["version" ]
45
66
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