Skip to content

Commit

Permalink
Merge pull request opensearch-project#7 from derek-ho/createDataSource
Browse files Browse the repository at this point in the history
create dataSource UI option
  • Loading branch information
Swiddis authored Jun 15, 2023
2 parents b2ad24b + 72d4bd3 commit 5817a93
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import {
EuiTitle,
} from '@elastic/eui';
import React, { Fragment, useState } from 'react';
import { HttpStart } from '../../../../../../src/core/public';
import { INTEGRATIONS_BASE } from '../../../../common/constants/shared';

interface IntegrationFlyoutProps {
onClose: () => void;
onCreate: (name: string, dataSource: string) => void;
integrationName: string;
integrationType: string;
http: HttpStart;
}

export const doTypeValidation = (toCheck: any, required: any): boolean => {
Expand Down Expand Up @@ -156,6 +159,17 @@ export function AddIntegrationFlyout(props: IntegrationFlyoutProps) {
},
];

const createDataSourceMappings = async (targetDataSource: string): Promise<any> => {
const data = await fetch(`${INTEGRATIONS_BASE}/repository/${integrationName}/schema`).then(
(response) => {
return response.json();
}
);
Object.keys(data.data.mappings).forEach(function (k) {
createMappings(k, data.data.mappings[k], targetDataSource);
});
};

const onCreateSelectChange = (value: any) => {
setCreateDatasourceOption(value);
};
Expand Down Expand Up @@ -210,6 +224,43 @@ export function AddIntegrationFlyout(props: IntegrationFlyoutProps) {
});
};

const createMappings = async (
componentName: string,
payload: any,
dataSourceName: string
): Promise<{ [key: string]: { properties: any } } | null> => {
if (componentName !== integrationType) {
return fetch(`/api/console/proxy?path=_component_template/${componentName}&method=POST`, {
method: 'POST',
headers: [
['osd-xsrf', 'true'],
['Content-Type', 'application/json'],
],
body: JSON.stringify(payload),
})
.then((response) => response.json())
.catch((err: any) => {
console.error(err);
return null;
});
} else {
payload.index_patterns = [dataSourceName];
return fetch(`/api/console/proxy?path=_index_template/${componentName}&method=POST`, {
method: 'POST',
headers: [
['osd-xsrf', 'true'],
['Content-Type', 'application/json'],
],
body: JSON.stringify(payload),
})
.then((response) => response.json())
.catch((err: any) => {
console.error(err);
return null;
});
}
};

const fetchIntegrationMappings = async (
targetName: string
): Promise<{ [key: string]: { template: { mappings: { properties?: any } } } } | null> => {
Expand Down Expand Up @@ -336,7 +387,9 @@ export function AddIntegrationFlyout(props: IntegrationFlyoutProps) {
append={
<EuiButton
data-test-subj="resetCustomEmbeddablePanelTitle"
onClick={() => {}}
onClick={() => {
createDataSourceMappings(createDataSource);
}}
disabled={createDataSource.length === 0}
>
Create
Expand Down
2 changes: 1 addition & 1 deletion public/components/integrations/components/integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export function Integration(props: AvailableIntegrationProps) {
};

async function addIntegrationRequest(templateName: string, name: string, dataSource: string) {
console.log(name);
http
.post(`${INTEGRATIONS_BASE}/store/${templateName}`, {
body: JSON.stringify({ name, dataSource }),
Expand Down Expand Up @@ -163,6 +162,7 @@ export function Integration(props: AvailableIntegrationProps) {
}}
integrationName={integrationTemplateId}
integrationType={integration.type}
http={http}
/>
)}
</EuiPage>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"index_patterns": [
"sso_logs-*-*"
"ss4o_logs-*-*"
],
"data_stream": {},
"template": {
Expand Down Expand Up @@ -215,8 +215,8 @@
}
},
"composed_of": [
"http_template",
"communication_template"
"communication",
"http"
],
"version": 1,
"_meta": {
Expand Down

0 comments on commit 5817a93

Please sign in to comment.