Skip to content

Commit

Permalink
Merge branch 'main' into overviewDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
TackAdam authored Nov 4, 2024
2 parents 5190f1d + 10e6636 commit 9206f59
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export async function addIntegrationRequest({
indexPattern,
workflows,
skipRedirect,
dataSourceInfo,
dataSourceInfo, // s3 datasource
dataSourceMDSId,
dataSourceMDSLabel,
}: AddIntegrationRequestParams): Promise<boolean> {
Expand Down Expand Up @@ -378,6 +378,7 @@ export async function addIntegrationRequest({
query: {
path: `${indexPattern}/_bulk?refresh=wait_for`,
method: 'POST',
dataSourceId: dataSourceMDSId,
},
})
.then((_) => {
Expand Down
89 changes: 89 additions & 0 deletions server/adaptors/integrations/__test__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,95 @@ describe('IntegrationInstanceBuilder', () => {
expect(instance).toEqual(expectedInstance);
});

it('should build an integration instance with MDS reference', async () => {
const options = {
indexPattern: 'instance-datasource',
name: 'instance-name',
dataSourceMDSId: 'sampleMDSId',
dataSourceMDSLabel: 'sample MDS',
};

const remappedAssets = [
{
id: 'remapped-asset1',
references: [{ id: 'remapped-ref1' }],
},
{
id: 'remapped-asset2',
references: [{ id: 'remapped-ref2' }],
},
];
const postAssetsResponse = {
saved_objects: [
{ id: 'created-asset1', type: 'dashboard', attributes: { title: 'Dashboard 1' } },
{ id: 'created-asset2', type: 'visualization', attributes: { title: 'Visualization 1' } },
],
};
const expectedInstance = {
name: 'instance-name',
templateName: 'sample',
dataSource: 'instance-datasource',
creationDate: expect.any(String),
assets: [
{
assetType: 'dashboard',
assetId: 'created-asset1',
status: 'available',
isDefaultAsset: true,
description: 'Dashboard 1',
},
{
assetType: 'visualization',
assetId: 'created-asset2',
status: 'available',
isDefaultAsset: false,
description: 'Visualization 1',
},
],
references: [
{
id: 'sampleMDSId',
name: 'sample MDS',
type: 'data-source',
},
],
};

const mockTemplate: Partial<IntegrationConfig> = TEST_INTEGRATION_CONFIG;

jest
.spyOn(mockUtils, 'deepCheck')
.mockResolvedValue({ ok: true, value: mockTemplate as IntegrationConfig });

// Mock the implementation of the methods in the Integration class
// sampleIntegration.deepCheck = jest.fn().mockResolvedValue({ ok: true, value: mockTemplate });
sampleIntegration.getAssets = jest.fn().mockResolvedValue({
ok: true,
value: [{ type: 'savedObjectBundle', data: remappedAssets }],
});
sampleIntegration.getConfig = jest.fn().mockResolvedValue({ ok: true, value: mockTemplate });

// Mock builder sub-methods
const remapIDsSpy = jest.spyOn(builder, 'remapIDs');
const addMDSReferenceSpy = jest.spyOn(builder, 'addMDSReference');
const postAssetsSpy = jest.spyOn(builder, 'postAssets');

(mockSavedObjectsClient.bulkCreate as jest.Mock).mockResolvedValue(postAssetsResponse);

const instance = await builder.build(sampleIntegration, options);

expect(sampleIntegration.getAssets).toHaveBeenCalled();
expect(remapIDsSpy).toHaveBeenCalledWith(remappedAssets);
expect(addMDSReferenceSpy).toHaveBeenCalledWith(
remappedAssets,
undefined,
'sampleMDSId',
'sample MDS'
);
expect(postAssetsSpy).toHaveBeenCalledWith(remappedAssets);
expect(instance).toEqual(expectedInstance);
});

it('should reject with an error if integration is not valid', async () => {
const options = {
indexPattern: 'instance-datasource',
Expand Down
2 changes: 1 addition & 1 deletion server/adaptors/integrations/integrations_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class IntegrationInstanceBuilder {
dataSourceMDSId?: string,
dataSourceMDSLabel?: string
): SavedObject[] {
if (!dataSource) {
if (!dataSourceMDSId) {
return assets;
}
return assets.map((asset) => {
Expand Down
7 changes: 6 additions & 1 deletion server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ export class ObservabilityPlugin
core.savedObjects.registerType(integrationTemplateType);

// Register server side APIs
setupRoutes({ router, client: openSearchObservabilityClient, dataSourceEnabled });
setupRoutes({
router,
client: openSearchObservabilityClient,
dataSourceEnabled,
logger: this.logger,
});

core.savedObjects.registerType(getVisualizationSavedObject(dataSourceEnabled));
core.savedObjects.registerType(getSearchSavedObject(dataSourceEnabled));
Expand Down
18 changes: 9 additions & 9 deletions server/routes/dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down Expand Up @@ -71,7 +71,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand All @@ -95,7 +95,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand All @@ -119,7 +119,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down Expand Up @@ -158,7 +158,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down Expand Up @@ -194,7 +194,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down Expand Up @@ -230,7 +230,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down Expand Up @@ -263,7 +263,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down Expand Up @@ -298,7 +298,7 @@ export function registerDslRoute(
} catch (error) {
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode === 500 ? 503 : error.statusCode || 503,
body: error.message,
});
}
Expand Down
6 changes: 4 additions & 2 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { ILegacyClusterClient, IRouter } from '../../../../src/core/server';
import { ILegacyClusterClient, IRouter, Logger } from '../../../../src/core/server';
import { DSLFacet } from '../services/facets/dsl_facet';
import { PPLFacet } from '../services/facets/ppl_facet';
import SavedObjectFacet from '../services/facets/saved_objects';
Expand All @@ -30,10 +30,12 @@ export function setupRoutes({
router,
client,
dataSourceEnabled,
logger,
}: {
router: IRouter;
client: ILegacyClusterClient;
dataSourceEnabled: boolean;
logger: Logger;
}) {
PanelsRouter(router);
VisualizationsRouter(router);
Expand All @@ -49,7 +51,7 @@ export function setupRoutes({
registerParaRoute(router);
registerNoteRoute(router);
registerVizRoute(router, dataSourceEnabled);
const queryService = new QueryService(client);
const queryService = new QueryService(client, logger);
registerSqlRoute(router, queryService, dataSourceEnabled);

registerMetricsRoute(router, dataSourceEnabled);
Expand Down
3 changes: 2 additions & 1 deletion server/services/queryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import 'core-js/stable';
import _ from 'lodash';
import 'regenerator-runtime/runtime';
import { Logger } from '../../../../src/core/server';

export class QueryService {
private client: any;
constructor(client: any) {
constructor(client: any, private readonly logger: Logger) {
this.client = client;
}

Expand Down

0 comments on commit 9206f59

Please sign in to comment.