diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c823709af27..06eccf2d30ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -118,6 +118,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BUG][Multiple Datasource]Read hideLocalCluster setting from yml and set in data source selector and data source menu ([#6361](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6361)) - [BUG][Multiple Datasource] Refactor read-only component to cover more edge cases ([#6416](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6416)) - [BUG] Fix for checkForFunctionProperty so that order does not matter ([#6248](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6248)) +- [Dynamic Configurations] Fix dynamic config API calls to pass correct input ([#6474](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6474)) - [BUG][Multiple Datasource] Validation succeed as long as status code in response is 200 ([#6399](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6399)) - [BUG][Multiple Datasource] Add validation for title length to be no longer than 32 characters [#6452](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6452)) diff --git a/src/plugins/application_config/server/routes/index.test.ts b/src/plugins/application_config/server/routes/index.test.ts index 0aa161bf560f..37fc7981ea16 100644 --- a/src/plugins/application_config/server/routes/index.test.ts +++ b/src/plugins/application_config/server/routes/index.test.ts @@ -79,6 +79,8 @@ describe('application config routes', () => { getConfig: jest.fn().mockReturnValue(configurations), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const request = {}; const okResponse = { @@ -91,7 +93,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleGetConfig(client, request, response, logger); + const returnedResponse = await handleGetConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(okResponse); @@ -100,6 +107,8 @@ describe('application config routes', () => { value: configurations, }, }); + + expect(getConfigurationClient).toBeCalledWith(request); }); it('return error response when client throws error', async () => { @@ -111,6 +120,8 @@ describe('application config routes', () => { }), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const request = {}; const response = { @@ -119,7 +130,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleGetConfig(client, request, response, logger); + const returnedResponse = await handleGetConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(ERROR_RESPONSE); @@ -131,6 +147,7 @@ describe('application config routes', () => { }); expect(logger.error).toBeCalledWith(error); + expect(getConfigurationClient).toBeCalledWith(request); }); }); @@ -140,6 +157,8 @@ describe('application config routes', () => { getEntityConfig: jest.fn().mockReturnValue(ENTITY_VALUE), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const okResponse = { statusCode: 200, }; @@ -156,7 +175,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleGetEntityConfig(client, request, response, logger); + const returnedResponse = await handleGetEntityConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(okResponse); @@ -165,6 +189,8 @@ describe('application config routes', () => { value: ENTITY_VALUE, }, }); + + expect(getConfigurationClient).toBeCalledWith(request); }); it('return error response when client throws error', async () => { @@ -176,6 +202,8 @@ describe('application config routes', () => { }), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const request = { params: { entity: ENTITY_NAME, @@ -188,7 +216,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleGetEntityConfig(client, request, response, logger); + const returnedResponse = await handleGetEntityConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(ERROR_RESPONSE); @@ -200,6 +233,8 @@ describe('application config routes', () => { }); expect(logger.error).toBeCalledWith(error); + + expect(getConfigurationClient).toBeCalledWith(request); }); }); @@ -209,6 +244,8 @@ describe('application config routes', () => { updateEntityConfig: jest.fn().mockReturnValue(ENTITY_NEW_VALUE), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const okResponse = { statusCode: 200, }; @@ -228,7 +265,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleUpdateEntityConfig(client, request, response, logger); + const returnedResponse = await handleUpdateEntityConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(okResponse); @@ -241,6 +283,8 @@ describe('application config routes', () => { }); expect(logger.error).not.toBeCalled(); + + expect(getConfigurationClient).toBeCalledWith(request); }); it('return error response when client fails', async () => { @@ -252,6 +296,8 @@ describe('application config routes', () => { }), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const request = { params: { entity: ENTITY_NAME, @@ -267,7 +313,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleUpdateEntityConfig(client, request, response, logger); + const returnedResponse = await handleUpdateEntityConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(ERROR_RESPONSE); @@ -279,6 +330,8 @@ describe('application config routes', () => { }); expect(logger.error).toBeCalledWith(error); + + expect(getConfigurationClient).toBeCalledWith(request); }); }); @@ -288,6 +341,8 @@ describe('application config routes', () => { deleteEntityConfig: jest.fn().mockReturnValue(ENTITY_NAME), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const okResponse = { statusCode: 200, }; @@ -304,7 +359,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleDeleteEntityConfig(client, request, response, logger); + const returnedResponse = await handleDeleteEntityConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(okResponse); @@ -317,6 +377,7 @@ describe('application config routes', () => { }); expect(logger.error).not.toBeCalled(); + expect(getConfigurationClient).toBeCalledWith(request); }); it('return error response when client fails', async () => { @@ -328,6 +389,8 @@ describe('application config routes', () => { }), }; + const getConfigurationClient = jest.fn().mockReturnValue(client); + const request = { params: { entity: ENTITY_NAME, @@ -340,7 +403,12 @@ describe('application config routes', () => { const logger = loggerMock.create(); - const returnedResponse = await handleDeleteEntityConfig(client, request, response, logger); + const returnedResponse = await handleDeleteEntityConfig( + getConfigurationClient, + request, + response, + logger + ); expect(returnedResponse).toBe(ERROR_RESPONSE); @@ -352,6 +420,8 @@ describe('application config routes', () => { }); expect(logger.error).toBeCalledWith(error); + + expect(getConfigurationClient).toBeCalledWith(request); }); }); }); diff --git a/src/plugins/application_config/server/routes/index.ts b/src/plugins/application_config/server/routes/index.ts index b6ec638e1aa9..82c9a98bc445 100644 --- a/src/plugins/application_config/server/routes/index.ts +++ b/src/plugins/application_config/server/routes/index.ts @@ -6,7 +6,6 @@ import { schema } from '@osd/config-schema'; import { IRouter, - IScopedClusterClient, Logger, OpenSearchDashboardsRequest, OpenSearchDashboardsResponseFactory, @@ -15,7 +14,7 @@ import { ConfigurationClient } from '../types'; export function defineRoutes( router: IRouter, - getConfigurationClient: (configurationClient: IScopedClusterClient) => ConfigurationClient, + getConfigurationClient: (request?: OpenSearchDashboardsRequest) => ConfigurationClient, logger: Logger ) { router.get( @@ -24,9 +23,7 @@ export function defineRoutes( validate: false, }, async (context, request, response) => { - const client = getConfigurationClient(context.core.opensearch.client); - - return await handleGetConfig(client, request, response, logger); + return await handleGetConfig(getConfigurationClient, request, response, logger); } ); router.get( @@ -39,9 +36,7 @@ export function defineRoutes( }, }, async (context, request, response) => { - const client = getConfigurationClient(context.core.opensearch.client); - - return await handleGetEntityConfig(client, request, response, logger); + return await handleGetEntityConfig(getConfigurationClient, request, response, logger); } ); router.post( @@ -57,9 +52,7 @@ export function defineRoutes( }, }, async (context, request, response) => { - const client = getConfigurationClient(context.core.opensearch.client); - - return await handleUpdateEntityConfig(client, request, response, logger); + return await handleUpdateEntityConfig(getConfigurationClient, request, response, logger); } ); router.delete( @@ -72,21 +65,21 @@ export function defineRoutes( }, }, async (context, request, response) => { - const client = getConfigurationClient(context.core.opensearch.client); - - return await handleDeleteEntityConfig(client, request, response, logger); + return await handleDeleteEntityConfig(getConfigurationClient, request, response, logger); } ); } export async function handleGetEntityConfig( - client: ConfigurationClient, + getConfigurationClient: (request?: OpenSearchDashboardsRequest) => ConfigurationClient, request: OpenSearchDashboardsRequest, response: OpenSearchDashboardsResponseFactory, logger: Logger ) { logger.info(`Received a request to get entity config for ${request.params.entity}.`); + const client = getConfigurationClient(request); + try { const result = await client.getEntityConfig(request.params.entity, { headers: request.headers, @@ -103,7 +96,7 @@ export async function handleGetEntityConfig( } export async function handleUpdateEntityConfig( - client: ConfigurationClient, + getConfigurationClient: (request?: OpenSearchDashboardsRequest) => ConfigurationClient, request: OpenSearchDashboardsRequest, response: OpenSearchDashboardsResponseFactory, logger: Logger @@ -112,6 +105,8 @@ export async function handleUpdateEntityConfig( `Received a request to update entity ${request.params.entity} with new value ${request.body.newValue}.` ); + const client = getConfigurationClient(request); + try { const result = await client.updateEntityConfig(request.params.entity, request.body.newValue, { headers: request.headers, @@ -128,13 +123,15 @@ export async function handleUpdateEntityConfig( } export async function handleDeleteEntityConfig( - client: ConfigurationClient, + getConfigurationClient: (request?: OpenSearchDashboardsRequest) => ConfigurationClient, request: OpenSearchDashboardsRequest, response: OpenSearchDashboardsResponseFactory, logger: Logger ) { logger.info(`Received a request to delete entity ${request.params.entity}.`); + const client = getConfigurationClient(request); + try { const result = await client.deleteEntityConfig(request.params.entity, { headers: request.headers, @@ -151,13 +148,15 @@ export async function handleDeleteEntityConfig( } export async function handleGetConfig( - client: ConfigurationClient, + getConfigurationClient: (request?: OpenSearchDashboardsRequest) => ConfigurationClient, request: OpenSearchDashboardsRequest, response: OpenSearchDashboardsResponseFactory, logger: Logger ) { logger.info('Received a request to get all configurations.'); + const client = getConfigurationClient(request); + try { const result = await client.getConfig({ headers: request.headers }); return response.ok({