Skip to content

Commit

Permalink
Fix index name collisions on integration sample creation. (opensearch…
Browse files Browse the repository at this point in the history
…-project#818) (opensearch-project#838)

* Add integration name to created index mapping

* Switch template naming to loosely reflect SS4O convention

* Split mapping creation by type for code clarity

* Re-introduce confusing result block

* Modify naming template to better match SS4O convention

---------

Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
(cherry picked from commit 8f040ef)
  • Loading branch information
Swiddis authored and A9 Swift Project User committed Sep 28, 2023
1 parent 453d4a3 commit 181be5f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
4 changes: 2 additions & 2 deletions auto_sync_commit_metadata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"last_github_commit": "aeec6ff0211914bc9e01226d81966a3f4c94788a",
"last_gitfarm_commit": "812f9d28be30d8bba2e680f010be64c6a4c8763e"
"last_github_commit": "8f040ef67617134858dbc76a5bf26ddd976ea49d",
"last_gitfarm_commit": "072a3ac70eb0ddf085c77c6ed8808a08f27ded2e"
}
87 changes: 50 additions & 37 deletions public/components/integrations/components/integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,36 @@ export function Integration(props: AvailableIntegrationProps) {

const [isFlyoutVisible, setIsFlyoutVisible] = useState(false);
const { setToast } = useToast();
const [integration, setIntegration] = useState({});
const [integration, setIntegration] = useState({} as { name: string; type: string });

const [integrationMapping, setMapping] = useState(null);
const [integrationAssets, setAssets] = useState([]);
const [loading, setLoading] = useState(false);

const createMappings = async (
const createComponentMapping = async (
componentName: string,
payload: {
template: { mappings: { _meta: { version: string } } };
composed_of: string[];
index_patterns: string[];
}
): Promise<{ [key: string]: { properties: any } } | null> => {
const version = payload.template.mappings._meta.version;
return http
.post('/api/console/proxy', {
body: JSON.stringify(payload),
query: {
path: `_component_template/ss4o_${componentName}-${version}-template`,
method: 'POST',
},
})
.catch((err: any) => {
console.error(err);
return err;
});
};

const createIndexMapping = async (
componentName: string,
payload: {
template: { mappings: { _meta: { version: string } } };
Expand All @@ -47,56 +70,46 @@ export function Integration(props: AvailableIntegrationProps) {
dataSourceName: string
): Promise<{ [key: string]: { properties: any } } | null> => {
const version = payload.template.mappings._meta.version;
if (componentName !== integration.type) {
return http
.post('/api/console/proxy', {
body: JSON.stringify(payload),
query: {
path: `_component_template/ss4o_${componentName}_${version}_template`,
method: 'POST',
},
})
.catch((err: any) => {
console.error(err);
return err;
});
} else {
payload.index_patterns = [dataSourceName];
return http
.post('/api/console/proxy', {
body: JSON.stringify(payload),
query: {
path: `_index_template/${componentName}_${version}`,
method: 'POST',
},
})
.catch((err: any) => {
console.error(err);
return err;
});
}
payload.index_patterns = [dataSourceName];
return http
.post('/api/console/proxy', {
body: JSON.stringify(payload),
query: {
path: `_index_template/ss4o_${componentName}-${integration.name}-${version}-sample`,
method: 'POST',
},
})
.catch((err: any) => {
console.error(err);
return err;
});
};

const createDataSourceMappings = async (targetDataSource: string): Promise<any> => {
const data = await http.get(`${INTEGRATIONS_BASE}/repository/${integrationTemplateId}/schema`);
let error = null;
const mappings = data.data.mappings;
mappings[integration.type].composed_of = mappings[integration.type].composed_of.map(
(templateName: string) => {
const version = mappings[templateName].template.mappings._meta.version;
return `ss4o_${templateName}_${version}_template`;
(componentName: string) => {
const version = mappings[componentName].template.mappings._meta.version;
return `ss4o_${componentName}-${version}-template`;
}
);
// Create component mappings before the index mapping
// The assumption is that index mapping relies on component mappings for creation
Object.entries(mappings).forEach(async ([key, mapping]) => {
if (key === integration.type) {
return;
}
await createMappings(key, mapping as any, targetDataSource);
await createComponentMapping(key, mapping as any);
});
await createMappings(integration.type, mappings[integration.type], targetDataSource);
await createIndexMapping(integration.type, mappings[integration.type], targetDataSource);

for (const [key, mapping] of Object.entries(data.data.mappings)) {
const result = await createMappings(key, mapping as any, targetDataSource);
const result =
key === integration.type
? await createIndexMapping(key, mapping as any, targetDataSource)
: await createComponentMapping(key, mapping as any);

if (result && result.error) {
error = (result.error as any).reason;
Expand Down Expand Up @@ -244,7 +257,7 @@ export function Integration(props: AvailableIntegrationProps) {

const [selectedTabId, setSelectedTabId] = useState('assets');

const onSelectedTabChanged = (id) => {
const onSelectedTabChanged = (id: string) => {
setSelectedTabId(id);
};

Expand Down

0 comments on commit 181be5f

Please sign in to comment.