forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAM] HTTP Versioning Aggregate Rules Endpoint (elastic#164284)
## Summary Meta issue: elastic#157883 - Adds HTTP versioning to the aggregate rules endpoint - Deletes legacy HTTP get aggregate method --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
d570545
commit 1093847
Showing
42 changed files
with
608 additions
and
1,065 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/alerting/common/routes/rule/apis/aggregate/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
export { | ||
aggregateRulesRequestBodySchema, | ||
aggregateRulesResponseBodySchema, | ||
} from './schemas/latest'; | ||
export type { AggregateRulesRequestBody, AggregateRulesResponseBody } from './types/latest'; | ||
|
||
export { | ||
aggregateRulesRequestBodySchema as aggregateRulesRequestBodySchemaV1, | ||
aggregateRulesResponseBodySchema as aggregateRulesResponseBodySchemaV1, | ||
} from './schemas/v1'; | ||
export type { | ||
AggregateRulesRequestBody as AggregateRulesRequestBodyV1, | ||
AggregateRulesResponseBody as AggregateRulesResponseBodyV1, | ||
AggregateRulesResponse as AggregateRulesResponseV1, | ||
} from './types/v1'; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/common/routes/rule/apis/aggregate/schemas/latest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './v1'; |
44 changes: 44 additions & 0 deletions
44
x-pack/plugins/alerting/common/routes/rule/apis/aggregate/schemas/v1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
|
||
export const aggregateRulesRequestBodySchema = schema.object({ | ||
search: schema.maybe(schema.string()), | ||
default_search_operator: schema.oneOf([schema.literal('OR'), schema.literal('AND')], { | ||
defaultValue: 'OR', | ||
}), | ||
search_fields: schema.maybe(schema.arrayOf(schema.string())), | ||
has_reference: schema.maybe( | ||
// use nullable as maybe is currently broken | ||
// in config-schema | ||
schema.nullable( | ||
schema.object({ | ||
type: schema.string(), | ||
id: schema.string(), | ||
}) | ||
) | ||
), | ||
filter: schema.maybe(schema.string()), | ||
}); | ||
|
||
export const aggregateRulesResponseBodySchema = schema.object({ | ||
rule_execution_status: schema.recordOf(schema.string(), schema.number()), | ||
rule_last_run_outcome: schema.recordOf(schema.string(), schema.number()), | ||
rule_enabled_status: schema.object({ | ||
enabled: schema.number(), | ||
disabled: schema.number(), | ||
}), | ||
rule_muted_status: schema.object({ | ||
muted: schema.number(), | ||
unmuted: schema.number(), | ||
}), | ||
rule_snoozed_status: schema.object({ | ||
snoozed: schema.number(), | ||
}), | ||
rule_tags: schema.arrayOf(schema.string()), | ||
}); |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/common/routes/rule/apis/aggregate/types/latest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './v1'; |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/alerting/common/routes/rule/apis/aggregate/types/v1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { TypeOf } from '@kbn/config-schema'; | ||
import { aggregateRulesRequestBodySchemaV1, aggregateRulesResponseBodySchemaV1 } from '..'; | ||
|
||
export type AggregateRulesRequestBody = TypeOf<typeof aggregateRulesRequestBodySchemaV1>; | ||
export type AggregateRulesResponseBody = TypeOf<typeof aggregateRulesResponseBodySchemaV1>; | ||
|
||
export interface AggregateRulesResponse { | ||
body: AggregateRulesResponseBody; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
x-pack/plugins/alerting/server/application/rule/methods/aggregate/aggregate_rules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { KueryNode, nodeBuilder } from '@kbn/es-query'; | ||
import { findRulesSo } from '../../../../data/rule'; | ||
import { AlertingAuthorizationEntity } from '../../../../authorization'; | ||
import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events'; | ||
import { buildKueryNodeFilter } from '../../../../rules_client/common'; | ||
import { alertingAuthorizationFilterOpts } from '../../../../rules_client/common/constants'; | ||
import { RulesClientContext } from '../../../../rules_client/types'; | ||
import { aggregateOptionsSchema } from './schemas'; | ||
import type { AggregateParams } from './types'; | ||
import { validateRuleAggregationFields } from './validation'; | ||
|
||
export async function aggregateRules<T = Record<string, unknown>>( | ||
context: RulesClientContext, | ||
params: AggregateParams<T> | ||
): Promise<T> { | ||
const { options = {}, aggs } = params; | ||
const { filter, page = 1, perPage = 0, ...restOptions } = options; | ||
|
||
let authorizationTuple; | ||
try { | ||
authorizationTuple = await context.authorization.getFindAuthorizationFilter( | ||
AlertingAuthorizationEntity.Rule, | ||
alertingAuthorizationFilterOpts | ||
); | ||
validateRuleAggregationFields(aggs); | ||
aggregateOptionsSchema.validate(options); | ||
} catch (error) { | ||
context.auditLogger?.log( | ||
ruleAuditEvent({ | ||
action: RuleAuditAction.AGGREGATE, | ||
error, | ||
}) | ||
); | ||
throw error; | ||
} | ||
|
||
const { filter: authorizationFilter } = authorizationTuple; | ||
const filterKueryNode = buildKueryNodeFilter(filter); | ||
|
||
const { aggregations } = await findRulesSo<T>({ | ||
savedObjectsClient: context.unsecuredSavedObjectsClient, | ||
savedObjectsFindOptions: { | ||
...restOptions, | ||
filter: | ||
authorizationFilter && filterKueryNode | ||
? nodeBuilder.and([filterKueryNode, authorizationFilter as KueryNode]) | ||
: authorizationFilter, | ||
page, | ||
perPage, | ||
aggs, | ||
}, | ||
}); | ||
|
||
return aggregations!; | ||
} |
7 changes: 7 additions & 0 deletions
7
...r/application/rule/methods/aggregate/factories/default_rule_aggregation_factory/latest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
export { defaultRuleAggregationFactory } from './v1'; |
24 changes: 24 additions & 0 deletions
24
.../application/rule/methods/aggregate/factories/default_rule_aggregation_factory/v1.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { defaultRuleAggregationFactory } from './v1'; | ||
|
||
describe('getDefaultRuleAggregation', () => { | ||
it('should return aggregation with default maxTags', () => { | ||
const result = defaultRuleAggregationFactory(); | ||
expect(result.tags).toEqual({ | ||
terms: { field: 'alert.attributes.tags', order: { _key: 'asc' }, size: 50 }, | ||
}); | ||
}); | ||
|
||
it('should return aggregation with custom maxTags', () => { | ||
const result = defaultRuleAggregationFactory({ maxTags: 100 }); | ||
expect(result.tags).toEqual({ | ||
terms: { field: 'alert.attributes.tags', order: { _key: 'asc' }, size: 100 }, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.