Skip to content

Commit 612dc20

Browse files
avoid extra network call on configureIndex when no spec params provided, make sure we're not discarding things on unknown spec types, update unit tests
1 parent 946333f commit 612dc20

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/control/__tests__/configureIndex.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,62 @@ describe('configureIndex', () => {
274274
expect(IOA.describeIndex).toHaveBeenCalled();
275275
expect(IOA.configureIndex).not.toHaveBeenCalled();
276276
});
277+
278+
test('skips describeIndex when only non-spec params are updated', async () => {
279+
const fakeConfigure: (
280+
req: ConfigureIndexOperationRequest,
281+
) => Promise<IndexModel> = jest.fn().mockResolvedValue(podIndexModel);
282+
const IOA = {
283+
describeIndex: jest.fn(),
284+
configureIndex: fakeConfigure,
285+
} as unknown as ManageIndexesApi;
286+
287+
const returned = await configureIndex(IOA)({
288+
name: 'pod-index',
289+
deletionProtection: 'enabled',
290+
tags: { env: 'prod' },
291+
});
292+
293+
expect(returned).toBe(podIndexModel);
294+
// describeIndex must NOT be called — no spec params were supplied
295+
expect(IOA.describeIndex).not.toHaveBeenCalled();
296+
expect(IOA.configureIndex).toHaveBeenCalledWith({
297+
indexName: 'pod-index',
298+
configureIndexRequest: {
299+
spec: undefined,
300+
deletionProtection: 'enabled',
301+
tags: { env: 'prod' },
302+
embed: undefined,
303+
},
304+
xPineconeApiVersion: X_PINECONE_API_VERSION,
305+
});
306+
});
307+
308+
test('throws error when spec type cannot be determined and spec params are supplied', async () => {
309+
// Return an IndexModel whose spec has no recognisable type
310+
const unknownSpecModel = {
311+
...podIndexModel,
312+
spec: {},
313+
} as unknown as IndexModel;
314+
const fakeDescribe: (req: DescribeIndexRequest) => Promise<IndexModel> =
315+
jest.fn().mockResolvedValue(unknownSpecModel);
316+
const IOA = {
317+
describeIndex: fakeDescribe,
318+
configureIndex: jest.fn(),
319+
} as unknown as ManageIndexesApi;
320+
321+
await expect(
322+
configureIndex(IOA)({
323+
name: 'pod-index',
324+
podReplicas: 2,
325+
}),
326+
).rejects.toThrow(
327+
'Could not determine the index spec type. Verify the index exists and try again.',
328+
);
329+
330+
expect(IOA.describeIndex).toHaveBeenCalled();
331+
expect(IOA.configureIndex).not.toHaveBeenCalled();
332+
});
277333
});
278334

279335
describe('getIndexSpecType', () => {

src/control/configureIndex.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,18 @@ export const configureIndex = (api: ManageIndexesApi) => {
8787
return async (options: ConfigureIndexOptions): Promise<IndexModel> => {
8888
validator(options);
8989

90-
// Describe the index to determine its spec type
91-
const indexDescription = await describeIndex(api)(options.name);
92-
const specType = getIndexSpecType(indexDescription.spec);
90+
// Only fetch the index description when spec-type-dependent params are present,
91+
// avoiding an extra network round-trip for common updates (deletionProtection, tags, embed).
92+
const needsSpecType =
93+
options.podReplicas !== undefined ||
94+
options.podType !== undefined ||
95+
options.readCapacity !== undefined;
96+
97+
let specType: 'pod' | 'serverless' | 'byoc' | 'unknown' = 'unknown';
98+
if (needsSpecType) {
99+
const indexDescription = await describeIndex(api)(options.name);
100+
specType = getIndexSpecType(indexDescription.spec);
101+
}
93102

94103
// Validate that spec-specific parameters match the index type
95104
if (specType === 'pod' && options.readCapacity !== undefined) {
@@ -105,6 +114,12 @@ export const configureIndex = (api: ManageIndexesApi) => {
105114
`Cannot configure podReplicas or podType on a ${specType} index; these parameters are only supported for pod indexes.`,
106115
);
107116
}
117+
// Guard against silently discarding spec params when the index type could not be determined.
118+
if (needsSpecType && specType === 'unknown') {
119+
throw new PineconeArgumentError(
120+
'Could not determine the index spec type. Verify the index exists and try again.',
121+
);
122+
}
108123

109124
const spec = buildConfigureSpec(options, specType);
110125
const request: ConfigureIndexRequest = {

0 commit comments

Comments
 (0)