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>
  • Loading branch information
Swiddis authored Aug 9, 2023
1 parent aeec6ff commit 8f040ef
Showing 1 changed file with 50 additions and 37 deletions.
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 8f040ef

Please sign in to comment.