Skip to content

Commit

Permalink
[ES body removal] @elastic/kibana-core (elastic#204851)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2cd882c)
  • Loading branch information
afharo committed Jan 10, 2025
1 parent 1899657 commit ba25202
Show file tree
Hide file tree
Showing 84 changed files with 473 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { isRetryableEsClientErrorMock } from './is_scripting_enabled.test.mocks';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as estypes from '@elastic/elasticsearch/lib/api/types';
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';
import { isInlineScriptingEnabled } from './is_scripting_enabled';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ describe('#bulkCreate', () => {
getId = () => expect.any(String),
}: { method: string; _index?: string; getId?: (type: string, id?: string) => string }
) => {
const body = [];
const operations = [];
for (const { type, id, if_primary_term: ifPrimaryTerm, if_seq_no: ifSeqNo } of objects) {
body.push({
operations.push({
[method]: {
_index,
_id: getId(type, id),
Expand All @@ -179,10 +179,10 @@ describe('#bulkCreate', () => {
: {}),
},
});
body.push(expect.any(Object));
operations.push(expect.any(Object));
}
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
};
Expand Down Expand Up @@ -290,9 +290,9 @@ describe('#bulkCreate', () => {

it(`formats the ES request`, async () => {
await bulkCreateSuccess(client, repository, [obj1, obj2]);
const body = [...expectObjArgs(obj1), ...expectObjArgs(obj2)];
const operations = [...expectObjArgs(obj1), ...expectObjArgs(obj2)];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -301,9 +301,12 @@ describe('#bulkCreate', () => {
const obj1WithManagedTrue = { ...obj1, managed: true };
const obj2WithManagedTrue = { ...obj2, managed: true };
await bulkCreateSuccess(client, repository, [obj1WithManagedTrue, obj2WithManagedTrue]);
const body = [...expectObjArgs(obj1WithManagedTrue), ...expectObjArgs(obj2WithManagedTrue)];
const operations = [
...expectObjArgs(obj1WithManagedTrue),
...expectObjArgs(obj2WithManagedTrue),
];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand Down Expand Up @@ -333,9 +336,9 @@ describe('#bulkCreate', () => {

await bulkCreateSuccess(client, repository, objects);
const expected = expect.not.objectContaining({ originId: expect.anything() });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -360,9 +363,9 @@ describe('#bulkCreate', () => {
];
await bulkCreateSuccess(client, repository, objects);
const expected = expect.objectContaining({ originId: 'some-originId' });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -375,9 +378,9 @@ describe('#bulkCreate', () => {
];
await bulkCreateSuccess(client, repository, objects);
const expected = expect.not.objectContaining({ originId: expect.anything() });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -389,9 +392,9 @@ describe('#bulkCreate', () => {
];
await bulkCreateSuccess(client, repository, objects);
const expected = expect.objectContaining({ originId: 'existing-originId' });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -401,9 +404,9 @@ describe('#bulkCreate', () => {
it(`adds namespace to request body for any types that are single-namespace`, async () => {
await bulkCreateSuccess(client, repository, [obj1, obj2], { namespace });
const expected = expect.objectContaining({ namespace });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -412,29 +415,29 @@ describe('#bulkCreate', () => {
it(`adds managed=false to request body if declared for any types that are single-namespace`, async () => {
await bulkCreateSuccess(client, repository, [obj1, obj2], { namespace, managed: false });
const expected = expect.objectContaining({ namespace, managed: false });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
// this only ensures we don't override any other options
it(`adds managed=true to request body if declared for any types that are single-namespace`, async () => {
await bulkCreateSuccess(client, repository, [obj1, obj2], { namespace, managed: true });
const expected = expect.objectContaining({ namespace, managed: true });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});

it(`normalizes options.namespace from 'default' to undefined`, async () => {
await bulkCreateSuccess(client, repository, [obj1, obj2], { namespace: 'default' });
const expected = expect.not.objectContaining({ namespace: 'default' });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -446,9 +449,9 @@ describe('#bulkCreate', () => {
];
await bulkCreateSuccess(client, repository, objects, { namespace });
const expected = expect.not.objectContaining({ namespace: expect.anything() });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
});
Expand All @@ -468,9 +471,9 @@ describe('#bulkCreate', () => {
await bulkCreateSuccess(client, repository, objects, { namespace, overwrite: true });
const expected1 = expect.objectContaining({ namespaces: [namespace ?? 'default'] });
const expected2 = expect.objectContaining({ namespaces: ['*'] });
const body = [expect.any(Object), expected1, expect.any(Object), expected2];
const operations = [expect.any(Object), expected1, expect.any(Object), expected2];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
client.bulk.mockClear();
Expand Down Expand Up @@ -503,7 +506,7 @@ describe('#bulkCreate', () => {
},
]);
await bulkCreateSuccess(client, repository, objects, { namespace, overwrite: true });
const body = [
const operations = [
{ index: expect.objectContaining({ _id: `${ns2}:dashboard:${o1.id}` }) },
expect.objectContaining({ namespace: ns2 }),
{
Expand All @@ -525,7 +528,7 @@ describe('#bulkCreate', () => {
})
);
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
client.bulk.mockClear();
Expand All @@ -539,12 +542,12 @@ describe('#bulkCreate', () => {
const test = async (namespace?: string) => {
const objects = [{ ...obj1, type: 'dashboard', initialNamespaces: ['default'] }];
await bulkCreateSuccess(client, repository, objects, { namespace, overwrite: true });
const body = [
const operations = [
{ index: expect.objectContaining({ _id: `dashboard:${obj1.id}` }) },
expect.not.objectContaining({ namespace: 'default' }),
];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
client.bulk.mockClear();
Expand All @@ -558,9 +561,9 @@ describe('#bulkCreate', () => {
const objects = [obj1, { ...obj2, type: NAMESPACE_AGNOSTIC_TYPE }];
await bulkCreateSuccess(client, repository, objects, { namespace, overwrite: true });
const expected = expect.not.objectContaining({ namespaces: expect.anything() });
const body = [expect.any(Object), expected, expect.any(Object), expected];
const operations = [expect.any(Object), expected, expect.any(Object), expected];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
client.bulk.mockClear();
Expand Down Expand Up @@ -652,9 +655,9 @@ describe('#bulkCreate', () => {
const result = await repository.bulkCreate(objects);
expect(client.bulk).toHaveBeenCalled();
const objCall = isBulkError ? expectObjArgs(obj) : [];
const body = [...expectObjArgs(obj1), ...objCall, ...expectObjArgs(obj2)];
const operations = [...expectObjArgs(obj1), ...objCall, ...expectObjArgs(obj2)];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
expect(result).toEqual({
Expand Down Expand Up @@ -765,7 +768,7 @@ describe('#bulkCreate', () => {
);
expect(client.bulk).toHaveBeenCalled();
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body: [...expectObjArgs(o1), ...expectObjArgs(o5)] }),
expect.objectContaining({ operations: [...expectObjArgs(o1), ...expectObjArgs(o5)] }),
expect.anything()
);
expect(result).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export const performBulkCreate = async <T>(
? await client.bulk({
refresh,
require_alias: true,
body: bulkCreateParams,
operations: bulkCreateParams,
})
: undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../repository.test.mock';

import type { Payload } from '@hapi/boom';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as estypes from '@elastic/elasticsearch/lib/api/types';

import type {
SavedObjectsBulkDeleteObject,
Expand Down Expand Up @@ -131,9 +131,9 @@ describe('#bulkDelete', () => {
overrides?: Record<string, unknown>;
}
) => {
const body = [];
const operations = [];
for (const { type, id } of objects) {
body.push({
operations.push({
[method]: {
_index,
_id: getId(type, id),
Expand All @@ -143,7 +143,7 @@ describe('#bulkDelete', () => {
}

expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
};
Expand Down Expand Up @@ -202,9 +202,9 @@ describe('#bulkDelete', () => {
overrides?: Record<string, unknown>;
}
) => {
const body = [];
const operations = [];
for (const { type, id } of objects) {
body.push({
operations.push({
[method]: {
_index,
_id: getId(type, id),
Expand All @@ -213,7 +213,7 @@ describe('#bulkDelete', () => {
});
}
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.objectContaining({ operations }),
expect.anything()
);
};
Expand Down Expand Up @@ -266,7 +266,7 @@ describe('#bulkDelete', () => {
expect.objectContaining({ _id: `${MULTI_NAMESPACE_ISOLATED_TYPE}:${obj2.id}` }),
];
expect(client.mget).toHaveBeenCalledWith(
expect.objectContaining({ body: { docs } }),
expect.objectContaining({ docs }),
expect.anything()
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const performBulkDelete = async <T>(
const bulkDeleteResponse = bulkDeleteParams.length
? await client.bulk({
refresh,
body: bulkDeleteParams,
operations: bulkDeleteParams,
require_alias: true,
})
: undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
getSavedObjectFromSourceMock,
rawDocExistsInNamespaceMock,
} from './bulk_get.isolated.test.mocks';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as estypes from '@elastic/elasticsearch/lib/api/types';
import { SavedObject, CheckAuthorizationResult } from '@kbn/core-saved-objects-server';
import { apiContextMock, ApiExecutionContextMock } from '../../mocks';
import { performBulkGet } from './bulk_get';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../repository.test.mock';

import type { Payload } from '@hapi/boom';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as estypes from '@elastic/elasticsearch/lib/api/types';

import type { SavedObjectsBulkGetObject } from '@kbn/core-saved-objects-api-server';
import { type SavedObjectsRawDocSource, type SavedObject } from '@kbn/core-saved-objects-server';
Expand Down Expand Up @@ -149,14 +149,12 @@ describe('#bulkGet', () => {
) => {
expect(client.mget).toHaveBeenCalledWith(
expect.objectContaining({
body: {
docs: objects.map(({ type, id }) =>
expect.objectContaining({
_index,
_id: getId(type, id),
})
),
},
docs: objects.map(({ type, id }) =>
expect.objectContaining({
_index,
_id: getId(type, id),
})
),
}),
expect.anything()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ export const performBulkGet = async <T>(
}));
const bulkGetResponse = bulkGetDocs.length
? await client.mget<SavedObjectsRawDocSource>(
{
body: {
docs: bulkGetDocs,
},
},
{ docs: bulkGetDocs },
{ ignore: [404], meta: true }
)
: undefined;
Expand Down
Loading

0 comments on commit ba25202

Please sign in to comment.