Skip to content

Commit

Permalink
[Synthetics] remove public SO usage from delete params flow (#158046)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiqueclarke authored May 22, 2023
1 parent 0975eba commit 8c1624b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import { toMountPoint, useKibana } from '@kbn/kibana-react-plugin/public';
import { i18n } from '@kbn/i18n';

import { useDispatch } from 'react-redux';
import { getGlobalParamAction } from '../../../state/global_params';
import { getGlobalParamAction, deleteGlobalParams } from '../../../state/global_params';
import { syncGlobalParamsAction } from '../../../state/settings';
import { kibanaService } from '../../../../../utils/kibana_service';
import { syntheticsParamType } from '../../../../../../common/types/saved_objects';
import { NO_LABEL, YES_LABEL } from '../../monitors_page/management/monitor_list_table/labels';
import { ListParamItem } from './params_list';

Expand All @@ -38,10 +37,7 @@ export const DeleteParam = ({

const { status } = useFetcher(() => {
if (isDeleting && savedObjects) {
return savedObjects.client.bulkDelete(
items.map(({ id }) => ({ type: syntheticsParamType, id })),
{ force: true }
);
return deleteGlobalParams({ ids: items.map(({ id }) => id) });
}
}, [items, isDeleting]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ export const editGlobalParam = async ({
...paramRequest,
});
};

export const deleteGlobalParams = async ({
ids,
}: {
ids: string[];
}): Promise<SyntheticsParamSO> => {
return apiService.delete(SYNTHETICS_API_URLS.PARAMS, {
ids: JSON.stringify(ids),
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ export const globalParamsReducer = createReducer(initialState, (builder) => {
export * from './actions';
export * from './effects';
export * from './selectors';
export * from './api';
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ class ApiService {
return response;
}

public async delete<T>(apiUrl: string) {
const response = await this._http!.delete<T>(apiUrl);
public async delete<T>(apiUrl: string, params?: HttpFetchQuery) {
const response = await this._http!.delete<T>({ path: apiUrl, query: params });

if (response instanceof Error) {
throw response;
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/synthetics/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
} from '../legacy_uptime/routes';
import { getHasIntegrationMonitorsRoute } from './fleet/get_has_integration_monitors';
import { addSyntheticsParamsRoute } from './settings/add_param';
import { deleteSyntheticsParamsRoute } from './settings/delete_param';
import { enableDefaultAlertingRoute } from './default_alerts/enable_default_alert';
import { getDefaultAlertingRoute } from './default_alerts/get_default_alert';
import { createNetworkEventsRoute } from './network_events';
Expand Down Expand Up @@ -78,6 +79,7 @@ export const syntheticsAppRestApiRoutes: SyntheticsRestApiRouteFactory[] = [
getSyntheticsParamsRoute,
editSyntheticsParamsRoute,
addSyntheticsParamsRoute,
deleteSyntheticsParamsRoute,
syncParamsSyntheticsParamsRoute,
enableDefaultAlertingRoute,
getDefaultAlertingRoute,
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/synthetics/server/routes/settings/delete_param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ export const deleteSyntheticsParamsRoute: SyntheticsRestApiRouteFactory = () =>
method: 'DELETE',
path: SYNTHETICS_API_URLS.PARAMS,
validate: {
body: schema.object({
ids: schema.arrayOf(schema.string()),
query: schema.object({
ids: schema.string(),
}),
},
writeAccess: true,
handler: async ({ savedObjectsClient, request, server }): Promise<any> => {
const { ids } = request.body as { ids: string[] };
handler: async ({ savedObjectsClient, request }): Promise<any> => {
const { ids } = request.query as { ids: string };
const parsedIds = JSON.parse(ids) as string[];

const result = await savedObjectsClient.bulkDelete(
ids.map((id) => ({ type: syntheticsParamType, id })),
parsedIds.map((id) => ({ type: syntheticsParamType, id })),
{ force: true }
);

Expand Down
34 changes: 29 additions & 5 deletions x-pack/test/api_integration/apis/synthetics/sync_global_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,37 @@ export default function ({ getService }: FtrProviderContext) {
});

it('delete all params and sync again', async () => {
await kServer.savedObjects.clean({ types: [syntheticsParamType] });
const apiResponseK = await supertestAPI
.get(SYNTHETICS_API_URLS.SYNC_GLOBAL_PARAMS)
await supertestAPI
.post(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.send({ key: 'test', value: 'test' });
.send({ key: 'get', value: 'test' });
const getResponse = await supertestAPI
.get(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.expect(200);

expect(getResponse.body.data.length).eql(2);

expect(apiResponseK.status).eql(200);
const paramsResponse = getResponse.body.data || [];
const ids = paramsResponse.map((param: any) => param.id);

await supertestAPI
.delete(SYNTHETICS_API_URLS.PARAMS)
.query({ ids: JSON.stringify(ids) })
.set('kbn-xsrf', 'true')
.expect(200);

const getResponseAfterDelete = await supertestAPI
.get(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.expect(200);

expect(getResponseAfterDelete.body.data.length).eql(0);

await supertestAPI
.get(SYNTHETICS_API_URLS.SYNC_GLOBAL_PARAMS)
.set('kbn-xsrf', 'true')
.expect(200);

const apiResponse = await supertestAPI.get(
'/api/fleet/package_policies?page=1&perPage=2000&kuery=ingest-package-policies.package.name%3A%20synthetics'
Expand Down

0 comments on commit 8c1624b

Please sign in to comment.