Skip to content

Commit

Permalink
Revert changes to find_rules_route.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkambic committed Nov 26, 2024
1 parent 921b854 commit 2773205
Showing 1 changed file with 11 additions and 105 deletions.
116 changes: 11 additions & 105 deletions x-pack/plugins/alerting/server/routes/rule/apis/find/find_rules_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,26 @@

import { IRouter } from '@kbn/core/server';
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { ILicenseState } from '../../../../lib';
import { verifyAccessAndContext } from '../../../lib';
import { findRulesRequestQuerySchemaV1 } from '../../../../../common/routes/rule/apis/find';
import type {
FindRulesRequestQueryV1,
FindRulesResponseV1,
} from '../../../../../common/routes/rule/apis/find';
import { findRulesRequestQuerySchemaV1 } from '../../../../../common/routes/rule/apis/find';
import { RuleParamsV1, ruleResponseSchemaV1 } from '../../../../../common/routes/rule/response';
import {
AlertingRequestHandlerContext,
BASE_ALERTING_API_PATH,
INTERNAL_ALERTING_API_FIND_RULES_PATH,
} from '../../../../types';
import { ILicenseState } from '../../../../lib';
import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../../../../types';
import { verifyAccessAndContext } from '../../../lib';
import { trackLegacyTerminology } from '../../../lib/track_legacy_terminology';
import { transformFindRulesBodyV1, transformFindRulesResponseV1 } from './transforms';

interface BuildFindRulesRouteParams {
licenseState: ILicenseState;
path: string;
router: IRouter<AlertingRequestHandlerContext>;
excludeFromPublicApi?: boolean;
usageCounter?: UsageCounter;
}

const buildFindRulesRoute = ({
licenseState,
path,
router,
excludeFromPublicApi = false,
usageCounter,
}: BuildFindRulesRouteParams) => {
export const findRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
router.get(
{
path,
path: `${BASE_ALERTING_API_PATH}/rules/_find`,
options: {
access: 'public',
summary: 'Get information about rules',
Expand Down Expand Up @@ -91,7 +77,7 @@ const buildFindRulesRoute = ({

const findResult = await rulesClient.find({
options,
excludeFromPublicApi,
excludeFromPublicApi: true,
includeSnoozeData: true,
});

Expand All @@ -104,86 +90,6 @@ const buildFindRulesRoute = ({
})
)
);
if (path === INTERNAL_ALERTING_API_FIND_RULES_PATH) {
router.post(
{
path,
options: { access: 'internal' },
validate: {
body: findRulesRequestQuerySchemaV1,
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = (await context.alerting).getRulesClient();

const body: FindRulesRequestQueryV1 = req.body;

trackLegacyTerminology(
[req.body.search, req.body.search_fields, req.body.sort_field].filter(
Boolean
) as string[],
usageCounter
);

const options = transformFindRulesBodyV1({
...body,
has_reference: body.has_reference || undefined,
search_fields: searchFieldsAsArray(body.search_fields),
});

if (req.body.fields) {
usageCounter?.incrementCounter({
counterName: `alertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
}

const findResult = await rulesClient.find({
options,
excludeFromPublicApi,
includeSnoozeData: true,
});

const responseBody: FindRulesResponseV1<RuleParamsV1>['body'] =
transformFindRulesResponseV1<RuleParamsV1>(findResult, options.fields);

return res.ok({
body: responseBody,
});
})
)
);
}
};

export const findRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
buildFindRulesRoute({
excludeFromPublicApi: true,
licenseState,
path: `${BASE_ALERTING_API_PATH}/api/alerting/rules/_find`,
router,
usageCounter,
});
};

export const findInternalRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
buildFindRulesRoute({
excludeFromPublicApi: false,
licenseState,
path: INTERNAL_ALERTING_API_FIND_RULES_PATH,
router,
usageCounter,
});
};

function searchFieldsAsArray(searchFields: string | string[] | undefined): string[] | undefined {
Expand Down

0 comments on commit 2773205

Please sign in to comment.