Skip to content

Commit

Permalink
feat: remove useless code (opensearch-project#310)
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe authored Mar 27, 2024
1 parent efe6254 commit e3ce2e2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 71 deletions.
41 changes: 1 addition & 40 deletions src/core/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ const createGenericNotFoundError = (...args) =>
const createUnsupportedTypeError = (...args) =>
SavedObjectsErrorHelpers.createUnsupportedTypeError(...args).output.payload;

const omitWorkspace = (object) => {
const newObject = JSON.parse(JSON.stringify(object));
delete newObject.workspaces;
return newObject;
};

describe('SavedObjectsRepository', () => {
let client;
let savedObjectsRepository;
Expand Down Expand Up @@ -499,9 +493,7 @@ describe('SavedObjectsRepository', () => {
opensearchClientMock.createSuccessTransportRequestPromise(response)
);
const result = await savedObjectsRepository.bulkCreate(objects, options);
expect(client.mget).toHaveBeenCalledTimes(
multiNamespaceObjects?.length || options?.workspaces ? 1 : 0
);
expect(client.mget).toHaveBeenCalledTimes(multiNamespaceObjects?.length ? 1 : 0);
return result;
};

Expand Down Expand Up @@ -1801,7 +1793,6 @@ describe('SavedObjectsRepository', () => {
const obj6 = { type: NAMESPACE_AGNOSTIC_TYPE, id: 'six' };
const obj7 = { type: NAMESPACE_AGNOSTIC_TYPE, id: 'seven' };
const obj8 = { type: 'dashboard', id: 'eight', workspaces: ['foo'] };
const obj9 = { type: 'dashboard', id: 'nine', workspaces: ['bar'] };
const namespace = 'foo-namespace';

const checkConflicts = async (objects, options) =>
Expand Down Expand Up @@ -1923,36 +1914,6 @@ describe('SavedObjectsRepository', () => {
],
});
});

it(`expected results with workspaces`, async () => {
const objects = [obj8, obj9];
const response = {
status: 200,
docs: [getMockGetResponse(obj8), getMockGetResponse(obj9)],
};
client.mget.mockResolvedValue(
opensearchClientMock.createSuccessTransportRequestPromise(response)
);

const result = await checkConflicts(objects, {
workspaces: ['foo'],
});
expect(client.mget).toHaveBeenCalledTimes(1);
expect(result).toEqual({
errors: [
{ ...omitWorkspace(obj8), error: createConflictError(obj8.type, obj8.id) },
{
...omitWorkspace(obj9),
error: {
...createConflictError(obj9.type, obj9.id),
metadata: {
isNotOverwritable: true,
},
},
},
],
});
});
});
});

Expand Down
38 changes: 7 additions & 31 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,28 +360,15 @@ export class SavedObjectsRepository {

const method = object.id && overwrite ? 'index' : 'create';
const requiresNamespacesCheck = object.id && this._registry.isMultiNamespace(object.type);
/**
* It requires a check when overwriting objects to target workspaces
*/
const requiresWorkspaceCheck = !!(object.id && options.workspaces);

if (object.id == null) object.id = uuid.v1();

let opensearchRequestIndexPayload = {};

if (requiresNamespacesCheck || requiresWorkspaceCheck) {
opensearchRequestIndexPayload = {
opensearchRequestIndex: bulkGetRequestIndexCounter,
};
bulkGetRequestIndexCounter++;
}

return {
tag: 'Right' as 'Right',
value: {
method,
object,
...opensearchRequestIndexPayload,
...(requiresNamespacesCheck && { opensearchRequestIndex: bulkGetRequestIndexCounter++ }),
},
};
});
Expand All @@ -392,7 +379,7 @@ export class SavedObjectsRepository {
.map(({ value: { object: { type, id } } }) => ({
_id: this._serializer.generateRawId(namespace, type, id),
_index: this.getIndexForType(type),
_source: ['type', 'namespaces', 'workspaces'],
_source: ['type', 'namespaces'],
}));
const bulkGetResponse = bulkGetDocs.length
? await this.client.mget(
Expand Down Expand Up @@ -423,7 +410,7 @@ export class SavedObjectsRepository {
if (opensearchRequestIndex !== undefined) {
const indexFound = bulkGetResponse?.statusCode !== 404;
const actualResult = indexFound
? bulkGetResponse?.body.docs?.[opensearchRequestIndex]
? bulkGetResponse?.body.docs[opensearchRequestIndex]
: undefined;
const docFound = indexFound && actualResult?.found === true;
// @ts-expect-error MultiGetHit._source is optional
Expand Down Expand Up @@ -575,7 +562,7 @@ export class SavedObjectsRepository {
const bulkGetDocs = expectedBulkGetResults.filter(isRight).map(({ value: { type, id } }) => ({
_id: this._serializer.generateRawId(namespace, type, id),
_index: this.getIndexForType(type),
_source: ['type', 'namespaces', 'workspaces'],
_source: ['type', 'namespaces'],
}));
const bulkGetResponse = bulkGetDocs.length
? await this.client.mget(
Expand All @@ -598,24 +585,13 @@ export class SavedObjectsRepository {
const { type, id, opensearchRequestIndex } = expectedResult.value;
const doc = bulkGetResponse?.body.docs[opensearchRequestIndex];
if (doc?.found) {
let workspaceConflict = false;
if (options.workspaces) {
const transformedObject = this._serializer.rawToSavedObject(doc as SavedObjectsRawDoc);
const filteredWorkspaces = SavedObjectsUtils.filterWorkspacesAccordingToBaseWorkspaces(
options.workspaces,
transformedObject.workspaces
);
if (filteredWorkspaces.length) {
workspaceConflict = true;
}
}
errors.push({
id,
type,
error: {
...errorContent(SavedObjectsErrorHelpers.createConflictError(type, id)),
// @ts-expect-error MultiGetHit._source is optional
...((!this.rawDocExistsInNamespace(doc!, namespace) || workspaceConflict) && {
...(!this.rawDocExistsInNamespace(doc!, namespace) && {
metadata: { isNotOverwritable: true },
}),
},
Expand Down Expand Up @@ -740,9 +716,9 @@ export class SavedObjectsRepository {
}

/**
* Deletes all objects from the provided workspace. It used when deleting a workspace.
* Deletes all objects from the provided workspace.
*
* @param {string} workspace
* @param {string} workspace - workspace id
* @param options SavedObjectsDeleteByWorkspaceOptions
* @returns {promise} - { took, timed_out, total, deleted, batches, version_conflicts, noops, retries, failures }
*/
Expand Down

0 comments on commit e3ce2e2

Please sign in to comment.