Skip to content

Commit

Permalink
[Transform] Refactor new IndexService
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jun 5, 2020
1 parent 8e7b21a commit def2ccf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
29 changes: 4 additions & 25 deletions x-pack/plugins/transform/public/app/hooks/use_delete_transform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import React, { useCallback, useEffect, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { CoreSetup } from 'kibana/public';
import { toMountPoint } from '../../../../../../src/plugins/kibana_react/public';
import {
TransformEndpointRequest,
Expand All @@ -18,16 +17,7 @@ import { useAppDependencies, useToastNotifications } from '../app_dependencies';
import { TransformListRow, refreshTransformList$, REFRESH_TRANSFORM_LIST_STATE } from '../common';
import { ToastNotificationText } from '../components';
import { useApi } from './use_api';
import { API_BASE_PATH } from '../../../common/constants';
import { IIndexPattern } from '../../../../../../src/plugins/data/common/index_patterns';

export const canDeleteIndex = async (http: CoreSetup['http']) => {
const privilege = await http.get(`${API_BASE_PATH}privileges`);
if (!privilege) {
return false;
}
return privilege.hasAllPrivileges;
};
import { indexService } from '../services/es_index_service';

export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
const { http, savedObjects } = useAppDependencies();
Expand All @@ -43,22 +33,11 @@ export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
const toggleDeleteIndexPattern = useCallback(() => setDeleteIndexPattern(!deleteIndexPattern), [
deleteIndexPattern,
]);
const savedObjectsClient = savedObjects.client;

const checkIndexPatternExists = useCallback(
async (indexName: string) => {
try {
const response = await savedObjectsClient.find<IIndexPattern>({
type: 'index-pattern',
perPage: 10,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
});
const ip = response.savedObjects.find(
(obj) => obj.attributes.title.toLowerCase() === indexName.toLowerCase()
);
if (ip !== undefined) {
if (await indexService.indexPatternExists(savedObjects.client, indexName)) {
setIndexPatternExists(true);
}
} catch (e) {
Expand All @@ -76,12 +55,12 @@ export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
);
}
},
[savedObjectsClient, toastNotifications]
[savedObjects.client, toastNotifications]
);

const checkUserIndexPermission = useCallback(async () => {
try {
const userCanDelete = await canDeleteIndex(http);
const userCanDelete = await indexService.canDeleteIndex(http);
if (userCanDelete) {
setUserCanDeleteIndex(true);
}
Expand Down
33 changes: 33 additions & 0 deletions x-pack/plugins/transform/public/app/services/es_index_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { HttpSetup, SavedObjectsClientContract } from 'kibana/public';
import { API_BASE_PATH } from '../../../common/constants';
import { IIndexPattern } from '../../../../../../src/plugins/data/common/index_patterns';

export class IndexService {
async canDeleteIndex(http: HttpSetup) {
const privilege = await http.get(`${API_BASE_PATH}privileges`);
if (!privilege) {
return false;
}
return privilege.hasAllPrivileges;
}

async indexPatternExists(savedObjectsClient: SavedObjectsClientContract, indexName: string) {
const response = await savedObjectsClient.find<IIndexPattern>({
type: 'index-pattern',
perPage: 1,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
});
const ip = response.savedObjects.find((obj) => obj.attributes.title === indexName);
return ip !== undefined;
}
}

export const indexService = new IndexService();
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/server/routes/api/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async function getIndexPatternId(
) {
const response = await savedObjectsClient.find<IIndexPattern>({
type: 'index-pattern',
perPage: 10,
perPage: 1,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
Expand Down

0 comments on commit def2ccf

Please sign in to comment.