Skip to content

Commit

Permalink
import
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliidm committed Aug 4, 2023
1 parent ae4b47b commit d664868
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
32 changes: 26 additions & 6 deletions x-pack/plugins/lists/server/routes/list/import_list_item_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,33 @@ export const importListItemRoute = (router: ListsPluginRouter, config: ConfigTyp
const stream = createStreamFromBuffer(request.body);
const { deserializer, list_id: listId, serializer, type } = request.query;
const lists = await getListClient(context);
const listExists = await lists.getListDataStreamExists();
if (!listExists) {
return siemResponse.error({
body: `To import a list item, the data steam must exist first. Data stream "${lists.getListName()}" does not exist`,
statusCode: 400,
});

const listDataExists = await lists.getListDataStreamExists();
if (!listDataExists) {
const listIndexExists = await lists.getListIndexExists();
if (!listIndexExists) {
return siemResponse.error({
body: `To import a list item, the data steam must exist first. Data stream "${lists.getListName()}" does not exist`,
statusCode: 400,
});
}
// otherwise migration is needed
await lists.migrateListIndexToDataStream();
}

const listItemDataExists = await lists.getListItemDataStreamExists();
if (!listItemDataExists) {
const listItemIndexExists = await lists.getListItemIndexExists();
if (!listItemIndexExists) {
return siemResponse.error({
body: `To import a list item, the data steam must exist first. Data stream "${lists.getListItemName()}" does not exist`,
statusCode: 400,
});
}
// otherwise migration is needed
await lists.migrateListItemIndexToDataStream();
}

if (listId != null) {
const list = await lists.getList({ id: listId });
if (list == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import {
removeListServerGeneratedProperties,
removeListItemServerGeneratedProperties,
waitFor,
createListsIndices,
} from '../../utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const log = getService('log');
const es = getService('es');

describe('import_list_items', () => {
describe('importing list items without an index', () => {
Expand Down Expand Up @@ -110,6 +112,36 @@ export default ({ getService }: FtrProviderContext): void => {
};
expect(bodyToCompare).to.eql(outputtedList);
});

describe('legacy index (before migration to data streams)', () => {
beforeEach(async () => {
await deleteListsIndex(supertest, log);
});

afterEach(async () => {
await deleteListsIndex(supertest, log);
});

it('should import list to legacy index and migrate it', async () => {
// create legacy indices
await createListsIndices(es);

const { body } = await supertest
.post(`${LIST_ITEM_URL}/_import?type=ip`)
.set('kbn-xsrf', 'true')
.attach('file', getImportListItemAsBuffer(['127.0.0.1', '127.0.0.2']), 'list_items.txt')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(200);

const bodyToCompare = removeListServerGeneratedProperties(body);
const outputtedList: Partial<ListSchema> = {
...getListResponseMockWithoutAutoGeneratedValues(),
name: 'list_items.txt',
description: 'File uploaded from file system of list_items.txt',
};
expect(bodyToCompare).to.eql(outputtedList);
});
});
});
});
};

0 comments on commit d664868

Please sign in to comment.