Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Add User level setting #7953

Merged
merged 32 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0a6312c
user level setting
Hailong-am Aug 24, 2024
060a5c3
add unit test
Hailong-am Sep 2, 2024
de214ec
add unit test
Hailong-am Sep 2, 2024
c2dd98e
Changeset file for PR #7953 created/updated
opensearch-changeset-bot[bot] Sep 2, 2024
80fc1dc
replace with constant DEFAULT_WORKSPACE
Hailong-am Sep 2, 2024
fd3f6de
Merge branch 'main' into use_level_settings
Hailong-am Sep 3, 2024
b3e2acf
update test snapshot
Hailong-am Sep 3, 2024
32becd6
update incorrect type in test data
Hailong-am Sep 3, 2024
af15055
Merge branch 'main' into use_level_settings
Hailong-am Sep 3, 2024
cabf38a
fix lint
Hailong-am Sep 3, 2024
6776cdc
replace user placeholder when security not enabled
Hailong-am Sep 3, 2024
9dee281
remove version for user level settings
Hailong-am Sep 3, 2024
a5156dc
clean up
Hailong-am Sep 3, 2024
6689ff4
Merge remote-tracking branch 'upstream/main' into use_level_settings
Hailong-am Sep 3, 2024
ee9dd6d
Merge branch 'main' into use_level_settings
Hailong-am Sep 4, 2024
cf755c8
add customized render for permissions column
Hailong-am Sep 4, 2024
0482438
Merge branch 'main' into use_level_settings
Hailong-am Sep 4, 2024
87ef0b9
Merge remote-tracking branch 'upstream/main' into use_level_settings
Hailong-am Sep 5, 2024
f40d9b5
address review comments
Hailong-am Sep 5, 2024
88019a6
Merge remote-tracking branch 'upstream/main' into use_level_settings
Hailong-am Sep 5, 2024
9d2cc6e
address review comment
Hailong-am Sep 5, 2024
28e0135
Merge branch 'main' into use_level_settings
Hailong-am Sep 5, 2024
06d29b8
add scope to ui settings route as query parameter
Hailong-am Sep 5, 2024
d856c5e
Merge remote-tracking branch 'upstream/main' into use_level_settings
Hailong-am Sep 6, 2024
0043a9b
fix merge issue
Hailong-am Sep 6, 2024
3993fb3
remove version support for user settings
Hailong-am Sep 6, 2024
a50975b
remove refrences from user settings
Hailong-am Sep 6, 2024
b4f5d46
Merge branch 'main' into use_level_settings
Hailong-am Sep 6, 2024
e2dada6
Update src/plugins/advanced_settings/public/management_app/advanced_s…
Hailong-am Sep 6, 2024
099e199
unify PermissionModeId
Hailong-am Sep 6, 2024
37d7afc
Merge branch 'use_level_settings' of https://github.com/Hailong-am/Op…
Hailong-am Sep 6, 2024
5e83f3f
fix ut
Hailong-am Sep 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7953.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [Workspace] Support user personal settings ([#7953](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7953))
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export {
StringValidation,
StringValidationRegex,
StringValidationRegexString,
CURRENT_USER,
} from './ui_settings';

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () {
Object {
"newVersion": "4.0.1",
"prevVersion": "4.0.0",
"userLevel": false,
},
],
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,27 @@ import { SavedObjectsErrorHelpers } from '../../saved_objects/';
import { Logger } from '../../logging';

import { getUpgradeableConfig } from './get_upgradeable_config';
import { CURRENT_USER } from '../ui_settings_client';

interface Options {
savedObjectsClient: SavedObjectsClientContract;
version: string;
buildNum: number;
log: Logger;
handleWriteErrors: boolean;
userLevel?: boolean;
}

export async function createOrUpgradeSavedConfig(
options: Options
): Promise<Record<string, any> | undefined> {
const { savedObjectsClient, version, buildNum, log, handleWriteErrors } = options;
const { savedObjectsClient, version, buildNum, log, handleWriteErrors, userLevel } = options;

// try to find an older config we can upgrade
const upgradeableConfig = await getUpgradeableConfig({
savedObjectsClient,
version,
userLevel,
});

// default to the attributes of the upgradeableConfig if available
Expand All @@ -62,8 +65,9 @@ export async function createOrUpgradeSavedConfig(
);

try {
const docId = userLevel ? `${CURRENT_USER}_${version}` : version;
// create the new SavedConfig
await savedObjectsClient.create('config', attributes, { id: version });
await savedObjectsClient.create('config', attributes, { id: docId });
} catch (error) {
if (handleWriteErrors) {
if (SavedObjectsErrorHelpers.isConflictError(error)) {
Expand All @@ -85,6 +89,7 @@ export async function createOrUpgradeSavedConfig(
log.debug(`Upgrade config from ${upgradeableConfig.id} to ${version}`, {
prevVersion: upgradeableConfig.id,
newVersion: version,
userLevel: !!userLevel,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import { SavedObjectsClientContract } from '../../saved_objects/types';
import { CURRENT_USER } from '../ui_settings_client';
import { isConfigVersionUpgradeable } from './is_config_version_upgradeable';

/**
Expand All @@ -41,9 +42,11 @@ import { isConfigVersionUpgradeable } from './is_config_version_upgradeable';
export async function getUpgradeableConfig({
savedObjectsClient,
version,
userLevel,
}: {
savedObjectsClient: SavedObjectsClientContract;
version: string;
userLevel?: boolean;
}) {
// attempt to find a config we can upgrade
const { saved_objects: savedConfigs } = await savedObjectsClient.find({
Expand All @@ -52,6 +55,7 @@ export async function getUpgradeableConfig({
perPage: 1000,
sortField: 'buildNum',
sortOrder: 'desc',
...(userLevel ? { hasReference: { type: 'user', id: CURRENT_USER } } : {}),
});

// try to find a config that we can upgrade
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/ui_settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

export { UiSettingsClient, UiSettingsServiceOptions } from './ui_settings_client';
export { UiSettingsClient, UiSettingsServiceOptions, CURRENT_USER } from './ui_settings_client';

export { config } from './ui_settings_config';
export { UiSettingsService } from './ui_settings_service';
Expand Down
77 changes: 72 additions & 5 deletions src/core/server/ui_settings/ui_settings_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { createOrUpgradeSavedConfigMock } from './create_or_upgrade_saved_config

import { SavedObjectsClient } from '../saved_objects';
import { savedObjectsClientMock } from '../saved_objects/service/saved_objects_client.mock';
import { UiSettingsClient } from './ui_settings_client';
import { CURRENT_USER, UiSettingsClient } from './ui_settings_client';
import { CannotOverrideError } from './ui_settings_errors';

const logger = loggingSystemMock.create().get();
Expand Down Expand Up @@ -105,6 +105,22 @@ describe('ui settings', () => {
});
});

it('updates several values in one operation includes user level settings', async () => {
const value = chance.word();
const defaults = { key: { value, scope: 'user' } };
const { uiSettings, savedObjectsClient } = setup({ defaults });
await uiSettings.setMany({ one: 'value', another: 'val', key: 'value' });

expect(savedObjectsClient.update).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.update).toHaveBeenCalledWith(TYPE, ID, {
one: 'value',
another: 'val',
});
expect(savedObjectsClient.update).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`, {
key: 'value',
});
});

it('automatically creates the savedConfig if it is missing', async () => {
const { uiSettings, savedObjectsClient, createOrUpgradeSavedConfig } = setup();
savedObjectsClient.update
Expand Down Expand Up @@ -357,8 +373,9 @@ describe('ui settings', () => {
const { uiSettings, savedObjectsClient } = setup();
await uiSettings.getUserProvided();

expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, ID);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`);
});

it('returns user configuration', async () => {
Expand Down Expand Up @@ -410,6 +427,9 @@ describe('ui settings', () => {
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
]
`);
});
Expand All @@ -419,11 +439,13 @@ describe('ui settings', () => {
savedObjectsClient.get = jest
.fn()
.mockRejectedValueOnce(SavedObjectsClient.errors.createGenericNotFoundError())
.mockResolvedValueOnce({ attributes: {} })
.mockRejectedValueOnce(SavedObjectsClient.errors.createGenericNotFoundError())
.mockResolvedValueOnce({ attributes: {} });

expect(await uiSettings.getUserProvided()).toStrictEqual({});

expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(3);

expect(createOrUpgradeSavedConfig).toHaveBeenCalledTimes(1);
expect(createOrUpgradeSavedConfig).toHaveBeenCalledWith(
Expand Down Expand Up @@ -518,8 +540,9 @@ describe('ui settings', () => {
const opensearchDocSource = {};
const { uiSettings, savedObjectsClient } = setup({ opensearchDocSource });
await uiSettings.getAll();
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, ID);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`);
});

it('returns defaults when opensearch doc is empty', async () => {
Expand Down Expand Up @@ -552,6 +575,9 @@ describe('ui settings', () => {
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
]
`);
});
Expand Down Expand Up @@ -599,6 +625,43 @@ describe('ui settings', () => {
bar: 'user-provided',
});
});

// verify user level settings override global
it(`user level values will override global settings`, async () => {
const defaults = {
foo: {
value: 'default',
},
bar: {
value: 'default',
scope: 'user',
},
};

const { uiSettings, savedObjectsClient } = setup({ defaults });

savedObjectsClient.get.mockImplementation((_type, id, _options) => {
if (id === `${CURRENT_USER}_${ID}`) {
return Promise.resolve({
attributes: {
bar: 'my personal value',
},
} as any);
} else {
return Promise.resolve({
attributes: {
foo: 'default1',
bar: 'global value',
},
} as any);
}
});

expect(await uiSettings.getAll()).toStrictEqual({
foo: 'default1',
bar: 'my personal value',
});
});
});

describe('#getDefault()', () => {
Expand Down Expand Up @@ -626,8 +689,9 @@ describe('ui settings', () => {
const { uiSettings, savedObjectsClient } = setup({ opensearchDocSource });
await uiSettings.get('any');

expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, ID);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`);
});

it(`returns the promised value for a key`, async () => {
Expand Down Expand Up @@ -722,6 +786,9 @@ describe('ui settings', () => {
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
]
`);
});
Expand Down
Loading
Loading