Skip to content

Commit

Permalink
Merge pull request opensearch-project#6 from derek-ho/hookup-data-source
Browse files Browse the repository at this point in the history
hook up datasource creation from flyout to backend
  • Loading branch information
Swiddis authored Jun 13, 2023
2 parents 4d3fd7e + dec5e7d commit a59c403
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { create } from '../../../../../../src/plugins/data/common/search/aggs/me

interface IntegrationFlyoutProps {
onClose: () => void;
onCreate: (name: string, namespace: string, tags: string) => void;
onCreate: (name: string, dataSource: string) => void;
integrationName: string;
}

Expand Down Expand Up @@ -343,7 +343,7 @@ export function AddIntegrationFlyout(props: IntegrationFlyoutProps) {
<EuiFlexItem>
<EuiButton
onClick={() => {
onCreate(name, namespace, tags);
onCreate(name, dataSource);
onClose();
}}
fill
Expand Down
8 changes: 4 additions & 4 deletions public/components/integrations/components/integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export function Integration(props: AvailableIntegrationProps) {
setToasts([...toasts, { id: new Date().toISOString(), title, text, color } as Toast]);
};

async function addIntegrationRequest(templateName: string, name: string) {
async function addIntegrationRequest(templateName: string, name: string, dataSource: string) {
console.log(name);
http
.post(`${INTEGRATIONS_BASE}/store/${templateName}`, {
body: JSON.stringify({ name }),
body: JSON.stringify({ name, dataSource }),
})
.then((res) => {
setToast(
Expand Down Expand Up @@ -145,8 +145,8 @@ export function Integration(props: AvailableIntegrationProps) {
onClose={() => {
setIsFlyoutVisible(false);
}}
onCreate={(name) => {
addIntegrationRequest(integrationTemplateId, name);
onCreate={(name, dataSource) => {
addIntegrationRequest(integrationTemplateId, name, dataSource);
}}
integrationName={integrationTemplateId}
/>
Expand Down

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion server/adaptors/integrations/integrations_adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export interface IntegrationsAdaptor {

getIntegrationInstance: (query?: IntegrationInstanceQuery) => Promise<IntegrationInstanceResult>;

loadIntegrationInstance: (templateName: string, name: string) => Promise<IntegrationInstance>;
loadIntegrationInstance: (
templateName: string,
name: string,
dataSource: string
) => Promise<IntegrationInstance>;

deleteIntegrationInstance: (id: string) => Promise<any>;

Expand Down
18 changes: 18 additions & 0 deletions server/adaptors/integrations/integrations_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface BuilderOptions {
name: string;
dataset: string;
namespace: string;
dataSource: string;
}

export const assetsMap: { [key: string]: string[] } = {
Expand All @@ -29,6 +30,10 @@ export const assetsMap: { [key: string]: string[] } = {
],
};

export const indexPatternsMap: { [key: string]: string[] } = {
nginx: ['ss4o_logs-*-*'],
};

export class IntegrationInstanceBuilder {
client: SavedObjectsClientContract;

Expand All @@ -46,11 +51,24 @@ export class IntegrationInstanceBuilder {
})
.then(() => integration.getAssets())
.then((assets) => this.remapIDs(assets.savedObjects!))
.then((assets) => this.remapDataSource(assets, options.name, options.dataSource))
.then((assets) => this.postAssets(assets))
.then((refs) => this.buildInstance(integration, refs, options));
return instance;
}

remapDataSource(assets: any[], templateName: string, dataSource: string | undefined): any[] {
if (!dataSource) return assets;
indexPatternsMap[templateName].forEach((element) => {
assets.forEach((asset) => {
if (asset.attributes.title === element) {
asset.attributes.title = dataSource;
}
});
});
return assets;
}

remapIDs(assets: any[]): any[] {
const toRemap = assets.filter((asset) => asset.id);
const idMap = new Map<string, string>();
Expand Down
4 changes: 3 additions & 1 deletion server/adaptors/integrations/integrations_kibana_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export class IntegrationsKibanaBackend implements IntegrationsAdaptor {

loadIntegrationInstance = async (
templateName: string,
name: string
name: string,
dataSource: string
): Promise<IntegrationInstance> => {
const template = await this.repository.getIntegration(templateName);
if (template === null) {
Expand All @@ -82,6 +83,7 @@ export class IntegrationsKibanaBackend implements IntegrationsAdaptor {
name,
dataset: 'nginx',
namespace: 'prod',
dataSource,
});
await this.client.create('integration-instance', result);
return Promise.resolve(result);
Expand Down
7 changes: 6 additions & 1 deletion server/routes/integrations/integrations_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@ export function registerIntegrationsRoute(router: IRouter) {
}),
body: schema.object({
name: schema.string(),
dataSource: schema.string(),
}),
},
},
async (context, request, response): Promise<any> => {
const adaptor = getAdaptor(context, request);
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => {
return a.loadIntegrationInstance(request.params.templateName, request.body.name);
return a.loadIntegrationInstance(
request.params.templateName,
request.body.name,
request.body.dataSource
);
});
}
);
Expand Down

0 comments on commit a59c403

Please sign in to comment.