Remove unused isNewInstance code (#116747) (#116938)

Co-authored-by: Josh Dover <1813008+joshdover@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-11-01 17:40:55 -04:00 committed by GitHub
parent 5e16bdf269
commit bbca0e1544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 195 deletions

View file

@ -1,129 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { isNewInstance } from './new_instance_status';
import { elasticsearchServiceMock, savedObjectsClientMock } from '../../../../core/server/mocks';
describe('isNewInstance', () => {
const esClient = elasticsearchServiceMock.createScopedClusterClient();
const soClient = savedObjectsClientMock.create();
beforeEach(() => jest.resetAllMocks());
it('returns true when there are no index patterns', async () => {
soClient.find.mockResolvedValue({
page: 1,
per_page: 100,
total: 0,
saved_objects: [],
});
expect(await isNewInstance({ esClient, soClient })).toEqual(true);
});
it('returns false when there are any index patterns other than metrics-* or logs-*', async () => {
soClient.find.mockResolvedValue({
page: 1,
per_page: 100,
total: 1,
saved_objects: [
{
id: '1',
references: [],
type: 'index-pattern',
score: 99,
attributes: { title: 'my-pattern-*' },
},
],
});
expect(await isNewInstance({ esClient, soClient })).toEqual(false);
});
describe('when only metrics-* and logs-* index patterns exist', () => {
beforeEach(() => {
soClient.find.mockResolvedValue({
page: 1,
per_page: 100,
total: 2,
saved_objects: [
{
id: '1',
references: [],
type: 'index-pattern',
score: 99,
attributes: { title: 'metrics-*' },
},
{
id: '2',
references: [],
type: 'index-pattern',
score: 99,
attributes: { title: 'logs-*' },
},
],
});
});
it('calls /_cat/indices for the index patterns', async () => {
await isNewInstance({ esClient, soClient });
expect(esClient.asCurrentUser.cat.indices).toHaveBeenCalledWith({
index: 'logs-*,metrics-*',
format: 'json',
});
});
it('returns true if no logs or metrics indices exist', async () => {
esClient.asCurrentUser.cat.indices.mockReturnValue(
elasticsearchServiceMock.createSuccessTransportRequestPromise([])
);
expect(await isNewInstance({ esClient, soClient })).toEqual(true);
});
it('returns true if no logs or metrics indices contain data', async () => {
esClient.asCurrentUser.cat.indices.mockReturnValue(
elasticsearchServiceMock.createSuccessTransportRequestPromise([
{ index: '.ds-metrics-foo', 'docs.count': '0' },
])
);
expect(await isNewInstance({ esClient, soClient })).toEqual(true);
});
it('returns true if only metrics-elastic_agent index contains data', async () => {
esClient.asCurrentUser.cat.indices.mockReturnValue(
elasticsearchServiceMock.createSuccessTransportRequestPromise([
{ index: '.ds-metrics-elastic_agent', 'docs.count': '100' },
])
);
expect(await isNewInstance({ esClient, soClient })).toEqual(true);
});
it('returns true if only logs-elastic_agent index contains data', async () => {
esClient.asCurrentUser.cat.indices.mockReturnValue(
elasticsearchServiceMock.createSuccessTransportRequestPromise([
{ index: '.ds-logs-elastic_agent', 'docs.count': '100' },
])
);
expect(await isNewInstance({ esClient, soClient })).toEqual(true);
});
it('returns false if any other logs or metrics indices contain data', async () => {
esClient.asCurrentUser.cat.indices.mockReturnValue(
elasticsearchServiceMock.createSuccessTransportRequestPromise([
{ index: '.ds-metrics-foo', 'docs.count': '100' },
])
);
expect(await isNewInstance({ esClient, soClient })).toEqual(false);
});
it('returns false if an authentication error is thrown', async () => {
esClient.asCurrentUser.cat.indices.mockReturnValue(
elasticsearchServiceMock.createErrorTransportRequestPromise({})
);
expect(await isNewInstance({ esClient, soClient })).toEqual(false);
});
});
});

View file

@ -1,66 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import type { IScopedClusterClient, SavedObjectsClientContract } from '../../../../core/server';
const LOGS_INDEX_PATTERN = 'logs-*';
const METRICS_INDEX_PATTERN = 'metrics-*';
const INDEX_PREFIXES_TO_IGNORE = [
'.ds-metrics-elastic_agent', // ignore index created by Fleet server itself
'.ds-logs-elastic_agent', // ignore index created by Fleet server itself
];
interface Deps {
esClient: IScopedClusterClient;
soClient: SavedObjectsClientContract;
}
export const isNewInstance = async ({ esClient, soClient }: Deps): Promise<boolean> => {
const indexPatterns = await soClient.find<{ title: string }>({
type: 'index-pattern',
fields: ['title'],
search: `*`,
searchFields: ['title'],
perPage: 100,
});
// If there are no index patterns, assume this is a new instance
if (indexPatterns.total === 0) {
return true;
}
// If there are any index patterns that are not the default metrics-* and logs-* ones created by Fleet, assume this
// is not a new instance
if (
indexPatterns.saved_objects.some(
(ip) =>
ip.attributes.title !== LOGS_INDEX_PATTERN && ip.attributes.title !== METRICS_INDEX_PATTERN
)
) {
return false;
}
try {
const logsAndMetricsIndices = await esClient.asCurrentUser.cat.indices({
index: `${LOGS_INDEX_PATTERN},${METRICS_INDEX_PATTERN}`,
format: 'json',
});
const anyIndicesContainerUserData = logsAndMetricsIndices.body
// Ignore some data that is shipped by default
.filter(({ index }) => !INDEX_PREFIXES_TO_IGNORE.some((prefix) => index?.startsWith(prefix)))
// If any other logs and metrics indices have data, return false
.some((catResult) => (catResult['docs.count'] ?? '0') !== '0');
return !anyIndicesContainerUserData;
} catch (e) {
// If any errors are encountered return false to be safe
return false;
}
};