diff --git a/pinecone/grpc/pinecone.py b/pinecone/grpc/pinecone.py index b03da15d7..7926afc83 100644 --- a/pinecone/grpc/pinecone.py +++ b/pinecone/grpc/pinecone.py @@ -1,5 +1,7 @@ from pinecone import Pinecone from pinecone.config import ConfigBuilder +from pinecone.utils import normalize_host +from pinecone.pinecone import check_realistic_host from .index_grpc import GRPCIndex @@ -122,8 +124,13 @@ def Index(self, name: str = "", host: str = "", **kwargs): if name == "" and host == "": raise ValueError("Either name or host must be specified") - # Use host if it is provided, otherwise get host from describe_index - index_host = host or self.db.index._get_host(name) + if host != "": + check_realistic_host(host) + # Use host url if it is provided + index_host = normalize_host(host) + else: + # Otherwise, get host url from describe_index using the index name + index_host = self.db.index._get_host(name) pt = kwargs.pop("pool_threads", None) or self._pool_threads diff --git a/tests/unit_grpc/test_grpc_index_initialization.py b/tests/unit_grpc/test_grpc_index_initialization.py index b0b642505..987728f99 100644 --- a/tests/unit_grpc/test_grpc_index_initialization.py +++ b/tests/unit_grpc/test_grpc_index_initialization.py @@ -1,3 +1,4 @@ +import pytest import re from pinecone.grpc import PineconeGRPC, GRPCClientConfig @@ -73,7 +74,7 @@ def test_config_passed_when_target_by_host(self): def test_config_passed_when_target_by_host_and_port(self): pc = PineconeGRPC(api_key="YOUR_API_KEY") config = GRPCClientConfig(timeout=5, secure=False) - index = pc.Index(host="myhost:4343", grpc_config=config) + index = pc.Index(host="myhost.example.com:4343", grpc_config=config) assert index.grpc_client_config.timeout == 5 assert index.grpc_client_config.secure == False @@ -83,7 +84,7 @@ def test_config_passed_when_target_by_host_and_port(self): assert index.grpc_client_config.conn_timeout == 1 # Endpoint calculation does not override port - assert index._endpoint() == "myhost:4343" + assert index._endpoint() == "myhost.example.com:4343" def test_config_passes_source_tag_when_set(self): pc = PineconeGRPC(api_key="YOUR_API_KEY", source_tag="my_source_tag") @@ -91,3 +92,24 @@ def test_config_passes_source_tag_when_set(self): re.search(r"source_tag=my_source_tag", pc.db._index_api.api_client.user_agent) is not None ) + + def test_invalid_host(self): + pc = PineconeGRPC(api_key="key") + + with pytest.raises(ValueError) as e: + pc.Index(host="invalid") + assert "You passed 'invalid' as the host but this does not appear to be valid" in str( + e.value + ) + + with pytest.raises(ValueError) as e: + pc.Index(host="my-index") + assert "You passed 'my-index' as the host but this does not appear to be valid" in str( + e.value + ) + + # Can instantiate with realistic host + pc.Index(host="test-bt8x3su.svc.apw5-4e34-81fa.pinecone.io") + + # Can instantiate with localhost address + pc.Index(host="localhost:8080")