Skip to content

Commit

Permalink
Add tags to create and configure index
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Dec 19, 2024
1 parent d9c275f commit b67293b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
19 changes: 10 additions & 9 deletions pinecone/control/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def create_index(
timeout: Optional[int] = None,
deletion_protection: Optional[Literal["enabled", "disabled"]] = "disabled",
vector_type: Optional[Literal["dense", "sparse"]] = "dense",
tags: Optional[Dict[str, str]] = None,
):
api_instance = self.index_api

Expand All @@ -185,6 +186,11 @@ def create_index(
else:
raise ValueError("deletion_protection must be either 'enabled' or 'disabled'")

if tags is None:
tags_obj = None
else:
tags_obj = IndexTags(**tags)

index_spec = self._parse_index_spec(spec)

api_instance.create_index(
Expand All @@ -197,6 +203,7 @@ def create_index(
("spec", index_spec),
("deletion_protection", dp),
("vector_type", vector_type),
("tags", tags_obj),
]
)
)
Expand Down Expand Up @@ -285,22 +292,22 @@ def configure_index(
):
api_instance = self.index_api
description = self.describe_index(name=name)

if deletion_protection is None:
dp = DeletionProtection(description.deletion_protection)
elif deletion_protection in ["enabled", "disabled"]:
dp = DeletionProtection(deletion_protection)
else:
raise ValueError("deletion_protection must be either 'enabled' or 'disabled'")

fetched_tags = description.tags
if fetched_tags is None:
starting_tags = {}
else:
starting_tags = fetched_tags.to_dict()

if tags is None:
# Do not modify tags, if none are provided
# Do not modify tags if none are provided
tags = starting_tags
else:
# Merge existing tags with new tags
Expand All @@ -319,12 +326,6 @@ def configure_index(
req = ConfigureIndexRequest(deletion_protection=dp, tags=IndexTags(**tags))

api_instance.configure_index(name, configure_index_request=req)

def add_index_tags(self, name: str, tags: Dict[str, str]):
self.configure_index(name=name, tags=tags)

def remove_index_tag(self, name: str, tag_key: str):
self.configure_index(name=name, tags={tag_key: ""})

def create_collection(self, name: str, source: str):
api_instance = self.index_api
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/control/pod/test_create_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class TestCreateIndexPods:
def test_create_with_optional_tags(self, client, create_index_params):
index_name = create_index_params["name"]
tags = {"foo": "FOO", "bar": "BAR"}
create_index_params["tags"] = create_index_params

client.create_index(**create_index_params)

desc = client.describe_index(name=index_name)
assert desc.tags.to_dict() == tags
40 changes: 40 additions & 0 deletions tests/integration/control/serverless/test_configure_index_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest


class TestIndexTags:
def test_index_tags_none_by_default(self, client, ready_sl_index):
client.describe_index(name=ready_sl_index)
assert client.describe_index(name=ready_sl_index).tags is None

def test_add_index_tags(self, client, ready_sl_index):
client.configure_index(name=ready_sl_index, tags={"foo": "FOO", "bar": "BAR"})
assert client.describe_index(name=ready_sl_index).tags.to_dict() == {
"foo": "FOO",
"bar": "BAR",
}

def test_remove_tags_by_setting_empty_value_for_key(self, client, ready_sl_index):
client.configure_index(name=ready_sl_index, tags={"foo": "FOO", "bar": "BAR"})
client.configure_index(name=ready_sl_index, tags={})
assert client.describe_index(name=ready_sl_index).tags.to_dict() == {
"foo": "FOO",
"bar": "BAR",
}

client.configure_index(name=ready_sl_index, tags={"foo": ""})
assert client.describe_index(name=ready_sl_index).tags.to_dict() == {"bar": "BAR"}

def test_merge_new_tags_with_existing_tags(self, client, ready_sl_index):
client.configure_index(name=ready_sl_index, tags={"foo": "FOO", "bar": "BAR"})
client.configure_index(name=ready_sl_index, tags={"baz": "BAZ"})
assert client.describe_index(name=ready_sl_index).tags.to_dict() == {
"foo": "FOO",
"bar": "BAR",
"baz": "BAZ",
}

@pytest.mark.skip(reason="Backend bug filed")
def test_remove_all_tags(self, client, ready_sl_index):
client.configure_index(name=ready_sl_index, tags={"foo": "FOO", "bar": "BAR"})
client.configure_index(name=ready_sl_index, tags={"foo": "", "bar": ""})
assert client.describe_index(name=ready_sl_index).tags is None
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ def test_create_dense_index_with_metric(self, client, create_sl_index_params, me
desc = client.describe_index(create_sl_index_params["name"])
assert desc.metric == metric
assert desc.vector_type == "dense"

def test_create_with_optional_tags(self, client, create_sl_index_params):
tags = {"foo": "FOO", "bar": "BAR"}
create_sl_index_params["tags"] = tags
client.create_index(**create_sl_index_params)
desc = client.describe_index(create_sl_index_params["name"])
assert desc.tags.to_dict() == tags
8 changes: 0 additions & 8 deletions tests/integration/control/serverless/test_index_tags.py

This file was deleted.

0 comments on commit b67293b

Please sign in to comment.