From 31fd18d43741e988b3451ffcae1a883a1df6a644 Mon Sep 17 00:00:00 2001 From: Shahzad Date: Mon, 23 Jun 2025 15:17:29 +0200 Subject: [PATCH] update to use internal client --- .../plugins/slo/server/routes/slo/find_groups.ts | 7 +------ .../plugins/slo/server/routes/slo/find_slo.ts | 2 +- .../slo/server/routes/slo/get_slo_stats_overview.ts | 2 +- .../plugins/slo/server/services/find_slo_groups.ts | 8 ++++---- .../slo/server/services/get_slo_stats_overview.ts | 8 ++++---- .../summary_search_client.test.ts | 9 ++++++--- .../summary_search_client/summary_search_client.ts | 10 +++++----- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_groups.ts b/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_groups.ts index 3563cca7fcc0e..e567c432a3aab 100644 --- a/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_groups.ts +++ b/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_groups.ts @@ -23,12 +23,7 @@ export const findSLOGroupsRoute = createSloServerRoute({ await assertPlatinumLicense(plugins); const { scopedClusterClient, soClient, spaceId } = await getScopedClients({ request, logger }); - const findSLOGroups = new FindSLOGroups( - scopedClusterClient.asCurrentUser, - soClient, - logger, - spaceId - ); + const findSLOGroups = new FindSLOGroups(scopedClusterClient, soClient, logger, spaceId); return await findSLOGroups.execute(params?.query ?? {}); }, }); diff --git a/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_slo.ts b/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_slo.ts index ad07bf8dbbff2..80ea6ec8aa7fa 100644 --- a/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_slo.ts +++ b/x-pack/solutions/observability/plugins/slo/server/routes/slo/find_slo.ts @@ -28,7 +28,7 @@ export const findSLORoute = createSloServerRoute({ }); const summarySearchClient = new DefaultSummarySearchClient( - scopedClusterClient.asCurrentUser, + scopedClusterClient, soClient, logger, spaceId diff --git a/x-pack/solutions/observability/plugins/slo/server/routes/slo/get_slo_stats_overview.ts b/x-pack/solutions/observability/plugins/slo/server/routes/slo/get_slo_stats_overview.ts index 99d22a22da9bf..68d1d673d26a9 100644 --- a/x-pack/solutions/observability/plugins/slo/server/routes/slo/get_slo_stats_overview.ts +++ b/x-pack/solutions/observability/plugins/slo/server/routes/slo/get_slo_stats_overview.ts @@ -29,7 +29,7 @@ export const getSLOStatsOverview = createSloServerRoute({ const slosOverview = new GetSLOStatsOverview( soClient, - scopedClusterClient.asCurrentUser, + scopedClusterClient, spaceId, logger, rulesClient, diff --git a/x-pack/solutions/observability/plugins/slo/server/services/find_slo_groups.ts b/x-pack/solutions/observability/plugins/slo/server/services/find_slo_groups.ts index e9f6b8533650e..298b254e24443 100644 --- a/x-pack/solutions/observability/plugins/slo/server/services/find_slo_groups.ts +++ b/x-pack/solutions/observability/plugins/slo/server/services/find_slo_groups.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { ElasticsearchClient, Logger, SavedObjectsClientContract } from '@kbn/core/server'; +import { IScopedClusterClient, Logger, SavedObjectsClientContract } from '@kbn/core/server'; import { FindSLOGroupsParams, FindSLOGroupsResponse, @@ -38,7 +38,7 @@ function toPagination(params: FindSLOGroupsParams): Pagination { export class FindSLOGroups { constructor( - private esClient: ElasticsearchClient, + private scopedClusterClient: IScopedClusterClient, private soClient: SavedObjectsClientContract, private logger: Logger, private spaceId: string @@ -53,11 +53,11 @@ export class FindSLOGroups { const parsedFilters = parseStringFilters(filters, this.logger); const settings = await getSloSettings(this.soClient); - const { indices } = await getSummaryIndices(this.esClient, settings); + const { indices } = await getSummaryIndices(this.scopedClusterClient.asInternalUser, settings); const hasSelectedTags = groupBy === 'slo.tags' && groupsFilter.length > 0; - const response = await typedSearch(this.esClient, { + const response = await typedSearch(this.scopedClusterClient.asCurrentUser, { index: indices, size: 0, query: { diff --git a/x-pack/solutions/observability/plugins/slo/server/services/get_slo_stats_overview.ts b/x-pack/solutions/observability/plugins/slo/server/services/get_slo_stats_overview.ts index 9df83dbdef239..2c2a237d94353 100644 --- a/x-pack/solutions/observability/plugins/slo/server/services/get_slo_stats_overview.ts +++ b/x-pack/solutions/observability/plugins/slo/server/services/get_slo_stats_overview.ts @@ -6,7 +6,7 @@ */ import { RulesClientApi } from '@kbn/alerting-plugin/server/types'; -import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; +import { IScopedClusterClient } from '@kbn/core-elasticsearch-server'; import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; import { Logger } from '@kbn/logging'; import { AlertConsumers, SLO_RULE_TYPE_IDS } from '@kbn/rule-data-utils'; @@ -20,7 +20,7 @@ import { getElasticsearchQueryOrThrow, parseStringFilters } from './transform_ge export class GetSLOStatsOverview { constructor( private soClient: SavedObjectsClientContract, - private esClient: ElasticsearchClient, + private scopedClusterClient: IScopedClusterClient, private spaceId: string, private logger: Logger, private rulesClient: RulesClientApi, @@ -29,13 +29,13 @@ export class GetSLOStatsOverview { public async execute(params: GetSLOStatsOverviewParams): Promise { const settings = await getSloSettings(this.soClient); - const { indices } = await getSummaryIndices(this.esClient, settings); + const { indices } = await getSummaryIndices(this.scopedClusterClient.asInternalUser, settings); const kqlQuery = params.kqlQuery ?? ''; const filters = params.filters ?? ''; const parsedFilters = parseStringFilters(filters, this.logger); - const response = await typedSearch(this.esClient, { + const response = await typedSearch(this.scopedClusterClient.asCurrentUser, { index: indices, size: 0, query: { diff --git a/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.test.ts b/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.test.ts index 1fbe8d7ba0d17..84e13ce233602 100644 --- a/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.test.ts +++ b/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.test.ts @@ -16,6 +16,7 @@ import { } from '../fixtures/summary_search_document'; import { DefaultSummarySearchClient } from './summary_search_client'; import type { Sort, SummarySearchClient } from './types'; +import { IScopedClusterClient } from '@kbn/core-elasticsearch-server'; const defaultSort: Sort = { field: 'sli_value', @@ -27,11 +28,13 @@ const defaultPagination: Pagination = { }; describe('Summary Search Client', () => { - let esClientMock: ElasticsearchClientMock; + let scopedClusterClient: IScopedClusterClient; let service: SummarySearchClient; + let esClientMock: ElasticsearchClientMock; beforeEach(() => { - esClientMock = elasticsearchServiceMock.createElasticsearchClient(); + scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + esClientMock = scopedClusterClient.asCurrentUser as ElasticsearchClientMock; const soClientMock = { getCurrentNamespace: jest.fn().mockReturnValue('default'), get: jest.fn().mockResolvedValue({ @@ -42,7 +45,7 @@ describe('Summary Search Client', () => { }), } as any; service = new DefaultSummarySearchClient( - esClientMock, + scopedClusterClient, soClientMock, loggerMock.create(), 'default' diff --git a/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.ts b/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.ts index 64472b31babb8..a46006f6b359e 100644 --- a/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.ts +++ b/x-pack/solutions/observability/plugins/slo/server/services/summary_search_client/summary_search_client.ts @@ -6,7 +6,7 @@ */ import type { estypes } from '@elastic/elasticsearch'; -import { ElasticsearchClient, Logger, SavedObjectsClientContract } from '@kbn/core/server'; +import { IScopedClusterClient, Logger, SavedObjectsClientContract } from '@kbn/core/server'; import { isCCSRemoteIndexName } from '@kbn/es-query'; import { ALL_VALUE } from '@kbn/slo-schema'; import { assertNever } from '@kbn/std'; @@ -32,7 +32,7 @@ import { isCursorPagination } from './types'; export class DefaultSummarySearchClient implements SummarySearchClient { constructor( - private esClient: ElasticsearchClient, + private scopedClusterClient: IScopedClusterClient, private soClient: SavedObjectsClientContract, private logger: Logger, private spaceId: string @@ -47,7 +47,7 @@ export class DefaultSummarySearchClient implements SummarySearchClient { ): Promise> { const parsedFilters = parseStringFilters(filters, this.logger); const settings = await getSloSettings(this.soClient); - const { indices } = await getSummaryIndices(this.esClient, settings); + const { indices } = await getSummaryIndices(this.scopedClusterClient.asInternalUser, settings); const esParams = createEsParams({ index: indices, @@ -83,7 +83,7 @@ export class DefaultSummarySearchClient implements SummarySearchClient { try { const summarySearch = await typedSearch( - this.esClient, + this.scopedClusterClient.asCurrentUser, esParams ); @@ -173,7 +173,7 @@ export class DefaultSummarySearchClient implements SummarySearchClient { // Always attempt to delete temporary summary documents with an existing non-temp summary document // The temp summary documents are _eventually_ removed as we get through the real summary documents - await this.esClient.deleteByQuery({ + await this.scopedClusterClient.asCurrentUser.deleteByQuery({ index: SUMMARY_DESTINATION_INDEX_PATTERN, wait_for_completion: false, conflicts: 'proceed',