diff --git a/x-pack/plugins/rule_registry/server/index.ts b/x-pack/plugins/rule_registry/server/index.ts index 7b43ae675faf70..7c467173008196 100644 --- a/x-pack/plugins/rule_registry/server/index.ts +++ b/x-pack/plugins/rule_registry/server/index.ts @@ -13,6 +13,8 @@ export { RuleRegistryPluginSetupContract } from './plugin'; export { createLifecycleRuleTypeFactory } from './rule_registry/rule_type_helpers/create_lifecycle_rule_type_factory'; export { ecsFieldMap } from './generated/ecs_field_map'; export { pickWithPatterns } from './rule_registry/field_map/pick_with_patterns'; +export { FieldMapOf } from './types'; +export { ScopedRuleRegistryClient } from './rule_registry/create_scoped_rule_registry_client/types'; export const config = { schema: schema.object({ diff --git a/x-pack/plugins/rule_registry/server/rule_registry/create_scoped_rule_registry_client/index.ts b/x-pack/plugins/rule_registry/server/rule_registry/create_scoped_rule_registry_client/index.ts index 2a9eecc77ba589..9a3d4a38d2ad65 100644 --- a/x-pack/plugins/rule_registry/server/rule_registry/create_scoped_rule_registry_client/index.ts +++ b/x-pack/plugins/rule_registry/server/rule_registry/create_scoped_rule_registry_client/index.ts @@ -28,7 +28,9 @@ const getRuleUuids = async ({ ...(namespace ? { namespace } : {}), }; - const pitFinder = savedObjectsClient.createPointInTimeFinder(options); + const pitFinder = savedObjectsClient.createPointInTimeFinder({ + ...options, + }); const ruleUuids: string[] = []; @@ -53,7 +55,8 @@ export function createScopedRuleRegistryClient; index: string; }>; - index: string; + indexAliasName: string; + indexTarget: string; logger: Logger; ruleData?: { rule: { @@ -100,7 +104,7 @@ export function createScopedRuleRegistryClient { const validations = docs.map((doc) => { @@ -161,7 +165,7 @@ export function createScopedRuleRegistryClient (isRight(validation) ? validation.right : null)) - ).map((doc) => ({ body: doc, index })); + ).map((doc) => ({ body: doc, index: indexAliasName })); return clusterClientAdapter.indexDocuments(operations); }, diff --git a/x-pack/plugins/rule_registry/server/rule_registry/field_map/runtime_type_from_fieldmap.ts b/x-pack/plugins/rule_registry/server/rule_registry/field_map/runtime_type_from_fieldmap.ts index 422747282beaa0..6dc557c016d1a1 100644 --- a/x-pack/plugins/rule_registry/server/rule_registry/field_map/runtime_type_from_fieldmap.ts +++ b/x-pack/plugins/rule_registry/server/rule_registry/field_map/runtime_type_from_fieldmap.ts @@ -89,7 +89,7 @@ export function runtimeTypeFromFieldMap( fieldMap: TFieldMap ): FieldMapType { function mapToType(fields: FieldMap) { - return mapValues(fields, (field) => { + return mapValues(fields, (field, key) => { const type = field.type in esFieldTypeMap ? esFieldTypeMap[field.type as keyof EsFieldTypeMap] diff --git a/x-pack/plugins/rule_registry/server/rule_registry/index.ts b/x-pack/plugins/rule_registry/server/rule_registry/index.ts index fc3a2ab0228150..f1d24550ade0a5 100644 --- a/x-pack/plugins/rule_registry/server/rule_registry/index.ts +++ b/x-pack/plugins/rule_registry/server/rule_registry/index.ts @@ -78,11 +78,13 @@ export class RuleRegistry { private getEsNames() { const base = [this.options.kibanaIndex, this.options.name]; + const indexTarget = `${base.join('-')}*`; const indexAliasName = [...base, this.options.kibanaVersion.toLowerCase()].join('-'); const policyName = [...base, 'policy'].join('-'); return { indexAliasName, + indexTarget, policyName, }; } @@ -137,13 +139,15 @@ export class RuleRegistry { if (!this.options.writeEnabled) { return undefined; } + const { indexAliasName, indexTarget } = this.getEsNames(); return createScopedRuleRegistryClient({ - savedObjectsClient: context.core.savedObjects.client, + savedObjectsClient: context.core.savedObjects.getClient({ includedHiddenTypes: ['alert'] }), scopedClusterClient: context.core.elasticsearch.client, clusterClientAdapter: this.esAdapter, fieldMap: this.options.fieldMap, - index: this.getEsNames().indexAliasName, + indexAliasName, + indexTarget, logger: this.options.logger, }); } @@ -153,6 +157,8 @@ export class RuleRegistry { ) { const logger = this.options.logger.get(type.id); + const { indexAliasName, indexTarget } = this.getEsNames(); + this.options.alertingPluginSetupContract.registerType< AlertTypeParams, AlertTypeState, @@ -187,7 +193,8 @@ export class RuleRegistry { scopedClusterClient: services.scopedClusterClient, clusterClientAdapter: this.esAdapter, fieldMap: this.options.fieldMap, - index: this.getEsNames().indexAliasName, + indexAliasName, + indexTarget, namespace, ruleData: { producer, diff --git a/x-pack/plugins/rule_registry/server/types.ts b/x-pack/plugins/rule_registry/server/types.ts index da355a412207cf..e6b53a8558964e 100644 --- a/x-pack/plugins/rule_registry/server/types.ts +++ b/x-pack/plugins/rule_registry/server/types.ts @@ -14,6 +14,7 @@ import { AlertTypeState, } from '../../alerting/common'; import { ActionGroup, AlertExecutorOptions } from '../../alerting/server'; +import { RuleRegistry } from './rule_registry'; import { ScopedRuleRegistryClient } from './rule_registry/create_scoped_rule_registry_client/types'; import { DefaultFieldMap } from './rule_registry/defaults/field_map'; @@ -93,3 +94,7 @@ export type RuleType< TAdditionalRuleExecutorServices >; }; + +export type FieldMapOf< + TRuleRegistry extends RuleRegistry +> = TRuleRegistry extends RuleRegistry ? TFieldMap : never; diff --git a/x-pack/test/apm_api_integration/tests/alerts/rule_registry.ts b/x-pack/test/apm_api_integration/tests/alerts/rule_registry.ts index d87058ec28d73d..ff4d36d9af72fd 100644 --- a/x-pack/test/apm_api_integration/tests/alerts/rule_registry.ts +++ b/x-pack/test/apm_api_integration/tests/alerts/rule_registry.ts @@ -134,9 +134,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(apmIndexName.split('-').includes('observability')).to.be(true); expect(apmIndexName.split('-').includes('apm')).to.be(true); - expectSnapshot(indexNames[0]).toMatchInline( - `".kibana-alerts-observability-apm-8.0.0-000001"` - ); + expect(indexNames[0].startsWith('.kibana-alerts-observability-apm')).to.be(true); expect(get(apmIndex, 'index.mappings.properties.service.properties.environment.type')).to.be( 'keyword'