Skip to content

Commit

Permalink
clean up createServerlessIndex() and address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanshah18 committed Jan 10, 2025
1 parent 5bde9dd commit d45381b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.openapitools.db_control.client.model.IndexModel;

import java.util.HashMap;
import java.util.Map;

public class DeletionProtectionTest {
private static final Pinecone controlPlaneClient = new Pinecone
Expand All @@ -18,33 +19,37 @@ public class DeletionProtectionTest {
@Test
public void createIndexWithDeletionProtectionEnabled() {
String indexName = RandomStringBuilder.build("create-serv", 8);
HashMap<String, String> tags = new HashMap<>();
tags.put("test", "deletion-protection-enabled");
HashMap<String, String> expectedTags = new HashMap<>();
expectedTags.put("test", "deletion-protection-enabled");
// Create serverless index with deletion protection enabled
controlPlaneClient.createServerlessIndex(indexName, "cosine", 3, "aws", "us-west-2", DeletionProtection.ENABLED, tags);
controlPlaneClient.createServerlessIndex(indexName, "cosine", 3, "aws", "us-west-2", DeletionProtection.ENABLED, expectedTags);
// Describe index to verify deletion protection is enabled
IndexModel indexModel = controlPlaneClient.describeIndex(indexName);
DeletionProtection deletionProtection = indexModel.getDeletionProtection();
Assertions.assertEquals(deletionProtection, DeletionProtection.ENABLED);
Map<String, String> actualTags = indexModel.getTags();
Assertions.assertEquals(expectedTags, actualTags);
}

@Test
public void createPodIndexWithDeletionProtectionDisabled() {
String indexName = RandomStringBuilder.build("create-pod", 8);
HashMap<String, String> tags = new HashMap<>();
tags.put("test", "deletion-protection-disabled");
HashMap<String, String> expectedTags = new HashMap<>();
expectedTags.put("test", "deletion-protection-disabled");
// Create serverless index with deletion protection disabled
controlPlaneClient.createServerlessIndex(indexName, "cosine", 3, "aws", "us-west-2", DeletionProtection.DISABLED, tags);
controlPlaneClient.createServerlessIndex(indexName, "cosine", 3, "aws", "us-west-2", DeletionProtection.DISABLED, expectedTags);
IndexModel indexModel = controlPlaneClient.describeIndex(indexName);
DeletionProtection deletionProtection = indexModel.getDeletionProtection();
Assertions.assertEquals(deletionProtection, DeletionProtection.DISABLED);
Map<String, String> actualTags = indexModel.getTags();
Assertions.assertEquals(expectedTags, actualTags);
// Configure index to enable deletionProtection
controlPlaneClient.configureServerlessIndex(indexName, DeletionProtection.ENABLED, tags);
controlPlaneClient.configureServerlessIndex(indexName, DeletionProtection.ENABLED, expectedTags);
indexModel = controlPlaneClient.describeIndex(indexName);
deletionProtection = indexModel.getDeletionProtection();
Assertions.assertEquals(deletionProtection, DeletionProtection.ENABLED);
// Configure index to disable deletionProtection
controlPlaneClient.configureServerlessIndex(indexName, DeletionProtection.DISABLED, tags);
controlPlaneClient.configureServerlessIndex(indexName, DeletionProtection.DISABLED, expectedTags);
// Delete index
controlPlaneClient.deleteIndex(indexName);
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/io/pinecone/clients/Pinecone.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ public IndexModel createServerlessIndex(String indexName,
IndexModel indexModel = null;

try {
indexModel = manageIndexesApi.createIndex(new CreateIndexRequest()
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
.name(indexName)
.metric(userMetric)
.dimension(dimension)
.spec(createServerlessIndexRequestSpec)
.deletionProtection(deletionProtection))
.tags(tags);
.deletionProtection(deletionProtection);

if(tags != null && !tags.isEmpty()) {
createIndexRequest.tags(tags);
}

indexModel = manageIndexesApi.createIndex(createIndexRequest);
} catch (ApiException apiException) {
handleApiException(apiException);
}
Expand Down

0 comments on commit d45381b

Please sign in to comment.