Skip to content

Commit

Permalink
fixes from comments on pr
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley14 committed Sep 17, 2020
1 parent fb3d939 commit 377b543
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,16 @@ export const createRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) =>
message: `To create a rule, the index must exist first. Index ${finalIndex} does not exist`,
});
}
if (ruleId != null) {
if (
foundRules != null &&
foundRules.length > 0 &&
foundRules.find((rule) => rule.params.ruleId === ruleId)
) {
return createBulkErrorObject({
ruleId,
statusCode: 409,
message: `rule_id: "${ruleId}" already exists`,
});
}
if (
ruleId != null &&
foundRules != null &&
foundRules.some((rule) => rule.params.ruleId === ruleId)
) {
return createBulkErrorObject({
ruleId,
statusCode: 409,
message: `rule_id: "${ruleId}" already exists`,
});
}
const createdRule = await createRules({
alertsClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ export const importRulesRoute = (router: IRouter, config: ConfigType, ml: SetupP

throwHttpError(await mlAuthz.validateRuleType(type));

let rule;
if (rules != null) {
rule = rules.find((aRule) => aRule.params.ruleId === ruleId);
}
const rule = rules?.find((aRule) => aRule.params.ruleId === ruleId);

if (rule == null) {
await createRules({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { updateRulesNotifications } from '../../rules/update_rules_notifications
import { ruleStatusSavedObjectsClientFactory } from '../../signals/rule_status_saved_objects_client';
import { readRules } from '../../rules/read_rules';
import { PartialFilter } from '../../types';
import { RuleAlertType } from '../../rules/types';

export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) => {
router.patch(
Expand Down Expand Up @@ -58,7 +59,7 @@ export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) =>
}
return acc;
}, []);
const foundRules = await readRules({
const foundRules: RuleAlertType[] | null = await readRules({
alertsClient,
ruleIds,
id: undefined,
Expand Down Expand Up @@ -118,14 +119,12 @@ export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) =>
throwHttpError(await mlAuthz.validateRuleType(type));
}

const foundRule = foundRules?.find((rule) => rule.params.ruleId === ruleId);
let existingRule = foundRule != null ? [foundRule] : null;
if (foundRule == null) {
// try the id
existingRule = await readRules({ alertsClient, ruleIds: undefined, id });
}
const searchedRule = foundRules?.find((rule) => rule.params.ruleId === ruleId);
const existingRule = searchedRule
? [searchedRule]
: await readRules({ alertsClient, ruleIds: undefined, id });

if (existingRule != null && existingRule.length > 0 && existingRule[0].params.type) {
if (existingRule != null && existingRule[0].params.type) {
// reject an unauthorized modification of an ML rule
throwHttpError(await mlAuthz.validateRuleType(existingRule[0].params.type));
}
Expand Down

0 comments on commit 377b543

Please sign in to comment.