Skip to content

Commit

Permalink
Add workspaces parameter to resolve_import_errors API (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#133)

* Add workspaces parameter to resolve_import_errors API

Signed-off-by: gaobinlong <[email protected]>

* Fix bug

Signed-off-by: gaobinlong <[email protected]>

* Simplify some code

Signed-off-by: gaobinlong <[email protected]>

---------

Signed-off-by: gaobinlong <[email protected]>
  • Loading branch information
gaobinlong authored and ruanyl committed Sep 15, 2023
1 parent 00132f8 commit d17a233
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/core/server/saved_objects/import/resolve_import_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function resolveSavedObjectsImportErrors({
typeRegistry,
namespace,
createNewCopies,
workspaces,
}: SavedObjectsResolveImportErrorsOptions): Promise<SavedObjectsImportResponse> {
// throw a BadRequest error if we see invalid retries
validateRetries(retries);
Expand Down Expand Up @@ -126,6 +127,7 @@ export async function resolveSavedObjectsImportErrors({
namespace,
retries,
createNewCopies,
workspaces,
};
const checkConflictsResult = await checkConflicts(checkConflictsParams);
errorAccumulator = [...errorAccumulator, ...checkConflictsResult.errors];
Expand Down Expand Up @@ -157,6 +159,7 @@ export async function resolveSavedObjectsImportErrors({
importIdMap,
namespace,
overwrite,
workspaces,
};
const { createdObjects, errors: bulkCreateErrors } = await createSavedObjects(
createSavedObjectsParams
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/import/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export interface SavedObjectsResolveImportErrorsOptions {
namespace?: string;
/** If true, will create new copies of import objects, each with a random `id` and undefined `originId`. */
createNewCopies: boolean;
/** if specified, will import in given workspaces, else will import as global object */
workspaces?: string[];
}

export type CreatedObject<T> = SavedObject<T> & { destinationId?: string };
7 changes: 4 additions & 3 deletions src/core/server/saved_objects/routes/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ export const registerImportRoute = (router: IRouter, config: SavedObjectConfig)
});
}

const workspaces = req.query.workspaces
? Array<string>().concat(req.query.workspaces)
: undefined;
let workspaces = req.query.workspaces;
if (typeof workspaces === 'string') {
workspaces = [workspaces];
}

const result = await importSavedObjectsFromStream({
savedObjectsClient: context.core.savedObjects.client,
Expand Down
9 changes: 9 additions & 0 deletions src/core/server/saved_objects/routes/resolve_import_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export const registerResolveImportErrorsRoute = (router: IRouter, config: SavedO
validate: {
query: schema.object({
createNewCopies: schema.boolean({ defaultValue: false }),
workspaces: schema.maybe(
schema.oneOf([schema.string(), schema.arrayOf(schema.string())])
),
}),
body: schema.object({
file: schema.stream(),
Expand Down Expand Up @@ -98,13 +101,19 @@ export const registerResolveImportErrorsRoute = (router: IRouter, config: SavedO
});
}

let workspaces = req.query.workspaces;
if (typeof workspaces === 'string') {
workspaces = [workspaces];
}

const result = await resolveSavedObjectsImportErrors({
typeRegistry: context.core.savedObjects.typeRegistry,
savedObjectsClient: context.core.savedObjects.client,
readStream,
retries: req.body.retries,
objectLimit: maxImportExportSize,
createNewCopies: req.query.createNewCopies,
workspaces,
});

return res.ok({ body: result });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ async function callResolveImportErrorsApi(
http: HttpStart,
file: File,
retries: any,
createNewCopies: boolean
createNewCopies: boolean,
workspaces?: string[]
): Promise<SavedObjectsImportResponse> {
const formData = new FormData();
formData.append('file', file);
formData.append('retries', JSON.stringify(retries));
const query = createNewCopies ? { createNewCopies } : {};
const query = createNewCopies ? { createNewCopies, workspaces } : { workspaces };
return http.post<any>('/api/saved_objects/_resolve_import_errors', {
headers: {
// Important to be undefined, it forces proper headers to be set for FormData
Expand Down Expand Up @@ -167,6 +168,7 @@ export async function resolveImportErrors({
http,
getConflictResolutions,
state,
workspaces,
}: {
http: HttpStart;
getConflictResolutions: (
Expand All @@ -180,6 +182,7 @@ export async function resolveImportErrors({
file?: File;
importMode: { createNewCopies: boolean; overwrite: boolean };
};
workspaces?: string[];
}) {
const retryDecisionCache = new Map<string, RetryDecision>();
const replaceReferencesCache = new Map<string, Reference[]>();
Expand Down Expand Up @@ -264,7 +267,13 @@ export async function resolveImportErrors({
}

// Call API
const response = await callResolveImportErrorsApi(http, file!, retries, createNewCopies);
const response = await callResolveImportErrorsApi(
http,
file!,
retries,
createNewCopies,
workspaces
);
importCount = response.successCount; // reset the success count since we retry all successful results each time
failedImports = [];
for (const { error, ...obj } of response.errors || []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,14 @@ export class Flyout extends Component<FlyoutProps, FlyoutState> {
status: 'loading',
loadingMessage: undefined,
});
const { workspaces } = this.props;

try {
const updatedState = await resolveImportErrors({
http: this.props.http,
state: this.state,
getConflictResolutions: this.getConflictResolutions,
workspaces,
});
this.setState(updatedState);
} catch (e) {
Expand Down

0 comments on commit d17a233

Please sign in to comment.