Skip to content

Commit cbcccc5

Browse files
authored
Merge pull request #122 from weaviate/fix-connect-to-custom-json-config
- Bump weaviate-client version to 4.14.3 and allow IPs as hostname in config file
2 parents c130a3c + f8335ed commit cbcccc5

4 files changed

Lines changed: 174 additions & 14 deletions

File tree

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
weaviate-client>=4.12.0
1+
weaviate-client>=4.14.3
22
click==8.1.7
33
twine
44
pytest

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ classifiers =
3737
include_package_data = True
3838
python_requires = >=3.9
3939
install_requires =
40-
weaviate-client>=4.12.0
40+
weaviate-client>=4.14.3
4141
#weaviate-client @ git+https://github.com/weaviate/weaviate-python-client.git@dev/1.30
4242
click==8.1.7
4343
semver>=3.0.2

test/unittests/test_managers/test_config_manager.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,126 @@ def test_get_client_with_api_key_auth():
139139
call_kwargs["auth_credentials"], weaviate.auth.AuthApiKey
140140
)
141141
assert call_kwargs["auth_credentials"].api_key == "test-key"
142+
143+
144+
def test_get_client_with_ip_address():
145+
config_data = {
146+
"host": "127.0.0.1",
147+
"grpc_host": "127.0.0.1",
148+
"http_port": 8080,
149+
"grpc_port": 50051,
150+
}
151+
152+
with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
153+
with patch("os.path.isfile") as mock_isfile:
154+
mock_isfile.return_value = True
155+
with patch("weaviate.connect_to_custom") as mock_connect:
156+
config = ConfigManager(config_file="test_config.json")
157+
config.get_client()
158+
159+
mock_connect.assert_called_once()
160+
call_kwargs = mock_connect.call_args.kwargs
161+
assert call_kwargs["http_host"] == "127.0.0.1"
162+
assert call_kwargs["grpc_host"] == "127.0.0.1"
163+
assert call_kwargs["http_port"] == 8080
164+
assert call_kwargs["grpc_port"] == 50051
165+
assert call_kwargs["http_secure"] == False
166+
assert call_kwargs["grpc_secure"] == False
167+
168+
169+
def test_get_client_with_ip_address_and_secure_ports():
170+
config_data = {
171+
"host": "127.0.0.1",
172+
"grpc_host": "127.0.0.1",
173+
"http_port": 443,
174+
"grpc_port": 443,
175+
}
176+
177+
with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
178+
with patch("os.path.isfile") as mock_isfile:
179+
mock_isfile.return_value = True
180+
with patch("weaviate.connect_to_custom") as mock_connect:
181+
config = ConfigManager(config_file="test_config.json")
182+
config.get_client()
183+
184+
mock_connect.assert_called_once()
185+
call_kwargs = mock_connect.call_args.kwargs
186+
assert call_kwargs["http_host"] == "127.0.0.1"
187+
assert call_kwargs["grpc_host"] == "127.0.0.1"
188+
assert call_kwargs["http_port"] == 443
189+
assert call_kwargs["grpc_port"] == 443
190+
assert call_kwargs["http_secure"] == True
191+
assert call_kwargs["grpc_secure"] == True
192+
193+
194+
def test_get_client_with_ip_address_with_https():
195+
config_data = {
196+
"host": "https://85.10.20.30",
197+
"grpc_host": "https://85.10.20.31",
198+
"http_port": 443,
199+
"grpc_port": 443,
200+
}
201+
202+
with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
203+
with patch("os.path.isfile") as mock_isfile:
204+
mock_isfile.return_value = True
205+
with patch("weaviate.connect_to_custom") as mock_connect:
206+
config = ConfigManager(config_file="test_config.json")
207+
config.get_client()
208+
209+
mock_connect.assert_called_once()
210+
call_kwargs = mock_connect.call_args.kwargs
211+
assert call_kwargs["http_host"] == "85.10.20.30"
212+
assert call_kwargs["grpc_host"] == "85.10.20.31"
213+
assert call_kwargs["http_port"] == 443
214+
assert call_kwargs["grpc_port"] == 443
215+
assert call_kwargs["http_secure"] == True
216+
assert call_kwargs["grpc_secure"] == True
217+
218+
219+
def test_get_client_with_host_and_custom():
220+
config_data = {
221+
"host": "https://weaviate.google.my-cloud.com",
222+
"grpc_host": "https://grpc.weaviate.google.my-cloud.com",
223+
}
224+
225+
with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
226+
with patch("os.path.isfile") as mock_isfile:
227+
mock_isfile.return_value = True
228+
with patch("weaviate.connect_to_custom") as mock_connect:
229+
config = ConfigManager(config_file="test_config.json")
230+
config.get_client()
231+
232+
mock_connect.assert_called_once()
233+
call_kwargs = mock_connect.call_args.kwargs
234+
assert call_kwargs["http_host"] == "weaviate.google.my-cloud.com"
235+
assert call_kwargs["grpc_host"] == "grpc.weaviate.google.my-cloud.com"
236+
assert call_kwargs["http_port"] == 443
237+
assert call_kwargs["grpc_port"] == 443
238+
assert call_kwargs["http_secure"] == True
239+
assert call_kwargs["grpc_secure"] == True
240+
241+
242+
def test_get_client_with_host_and_custom_with_ports():
243+
config_data = {
244+
"host": "https://weaviate.google.my-cloud.com",
245+
"grpc_host": "https://grpc.weaviate.google.my-cloud.com",
246+
"http_port": 8080,
247+
"grpc_port": 8080,
248+
}
249+
250+
with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
251+
with patch("os.path.isfile") as mock_isfile:
252+
mock_isfile.return_value = True
253+
with patch("weaviate.connect_to_custom") as mock_connect:
254+
config = ConfigManager(config_file="test_config.json")
255+
config.get_client()
256+
257+
mock_connect.assert_called_once()
258+
call_kwargs = mock_connect.call_args.kwargs
259+
assert call_kwargs["http_host"] == "weaviate.google.my-cloud.com"
260+
assert call_kwargs["grpc_host"] == "grpc.weaviate.google.my-cloud.com"
261+
assert call_kwargs["http_port"] == 8080
262+
assert call_kwargs["grpc_port"] == 8080
263+
assert call_kwargs["http_secure"] == True # URL contains https
264+
assert call_kwargs["grpc_secure"] == True # URL contains https

weaviate_cli/managers/config_manager.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ def __get_port(self, url_parsed):
8585
else:
8686
return 80
8787

88+
def __is_url(self, host: str) -> bool:
89+
"""Check if the host is a URL (has a scheme and a network location)."""
90+
try:
91+
result = urlparse(host)
92+
return all([result.scheme, result.netloc])
93+
except ValueError:
94+
return False
95+
8896
def get_client(self) -> weaviate.WeaviateClient:
8997
"""Get weaviate client from config"""
9098
auth_config: Optional[weaviate.auth.AuthCredentials] = None
@@ -124,15 +132,44 @@ def get_client(self) -> weaviate.WeaviateClient:
124132
headers=self.config["headers"] if "headers" in self.config else None,
125133
)
126134
else:
127-
host_parsed = urlparse(self.config["host"])
128-
grpc_parsed = urlparse(self.config["grpc_host"])
129-
return weaviate.connect_to_custom(
130-
http_host=host_parsed.hostname,
131-
grpc_host=grpc_parsed.hostname,
132-
grpc_secure=grpc_parsed.scheme == "https",
133-
http_secure=host_parsed.scheme == "https",
134-
http_port=self.__get_port(host_parsed),
135-
grpc_port=self.__get_port(grpc_parsed),
136-
auth_credentials=auth_config,
137-
headers=self.config["headers"] if "headers" in self.config else None,
138-
)
135+
# Check if host is a URL or IP/hostname
136+
if self.__is_url(self.config["host"]):
137+
# Handle URL format
138+
host_parsed = urlparse(self.config["host"])
139+
grpc_parsed = urlparse(self.config["grpc_host"])
140+
return weaviate.connect_to_custom(
141+
http_host=host_parsed.hostname,
142+
grpc_host=grpc_parsed.hostname,
143+
grpc_secure=grpc_parsed.scheme == "https",
144+
http_secure=host_parsed.scheme == "https",
145+
http_port=(
146+
self.__get_port(host_parsed)
147+
if "http_port" not in self.config
148+
or self.config["http_port"] == ""
149+
else self.config["http_port"]
150+
),
151+
grpc_port=(
152+
self.__get_port(grpc_parsed)
153+
if "grpc_port" not in self.config
154+
or self.config["grpc_port"] == ""
155+
else self.config["grpc_port"]
156+
),
157+
auth_credentials=auth_config,
158+
headers=(
159+
self.config["headers"] if "headers" in self.config else None
160+
),
161+
)
162+
else:
163+
# Handle IP/hostname format
164+
return weaviate.connect_to_custom(
165+
http_host=self.config["host"],
166+
grpc_host=self.config["grpc_host"],
167+
grpc_secure=self.config["grpc_port"] == 443, # Secure if port 443
168+
http_secure=self.config["http_port"] == 443, # Secure if port 443
169+
http_port=self.config["http_port"],
170+
grpc_port=self.config["grpc_port"],
171+
auth_credentials=auth_config,
172+
headers=(
173+
self.config["headers"] if "headers" in self.config else None
174+
),
175+
)

0 commit comments

Comments
 (0)