Skip to content

Commit

Permalink
feat: make createAssets API compatible with workspace (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#2101)

Signed-off-by: SuZhou-Joe <[email protected]>
Co-authored-by: Adam Tackett <[email protected]>
  • Loading branch information
SuZhou-Joe and TackAdam authored Aug 28, 2024
1 parent 428cc98 commit 58eb65c
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions server/routes/getting_started/getting_started_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
IOpenSearchDashboardsResponse,
IRouter,
ResponseError,
SavedObject,
} from '../../../../../src/core/server';
import { createSavedObjectsStreamFromNdJson } from '../../../../../src/core/server/saved_objects/routes/utils';
import { loadAssetsFromFile } from './helper';
import { getWorkspaceState } from '../../../../../src/core/server/utils';

export function registerGettingStartedRoutes(router: IRouter) {
// Fetch the tutorial assets
Expand Down Expand Up @@ -148,39 +150,49 @@ export function registerGettingStartedRoutes(router: IRouter) {
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
try {
const { mdsId, mdsLabel, tutorialId } = request.body;
const { requestWorkspaceId } = getWorkspaceState(request);
const fileData = await loadAssetsFromFile(tutorialId);

const objects = await createSavedObjectsStreamFromNdJson(Readable.from(fileData));
const loadedObjects = await objects.toArray();

const updatedObjects = loadedObjects.map((obj) => {
let newId = obj.id;
let references: SavedObject['references'] | undefined = obj.references;

if (requestWorkspaceId) {
newId = `workspaceId-${requestWorkspaceId}-${newId}`;
references = references?.map((ref) => {
return {
...ref,
id: `workspaceId-${requestWorkspaceId}-${ref.id}`,
};
});
}

if (mdsId) {
const newId = `mds-${mdsId}-objectId-${obj.id}`;

const newReferences =
obj.references?.map((ref: { id: any }) => {
return {
...ref,
id: `mds-${mdsId}-objectId-${ref.id}`,
};
}) || [];

if (obj.type === 'index-pattern') {
newReferences.push({
newId = `mds-${mdsId}-objectId-${obj.id}`;
references = references?.map((ref) => {
return {
...ref,
id: `mds-${mdsId}-objectId-${ref.id}`,
};
});

if (obj.type === 'index-pattern' && references) {
references.push({
id: mdsId,
type: 'data-source',
name: mdsLabel,
});
}

return {
...obj,
id: newId,
references: newReferences,
};
} else {
return obj;
}

return {
...obj,
id: newId,
...(references && { references }),
};
});

await context.core.savedObjects.client.bulkCreate(updatedObjects);
Expand Down

0 comments on commit 58eb65c

Please sign in to comment.