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] Consume workspace id in saved object client #6014

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Create data source menu component able to be mount to nav bar ([#6082](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6082))
- [Multiple Datasource] Handle form values(request payload) if the selected type is available in the authentication registry ([#6049](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6049))
- [Multiple Datasource] Add Vega support to MDS by specifying a data source name in the Vega spec ([#5975](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5975))
- [Workspace] Consume workspace id in saved object client ([#6014](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6014))

### 🐛 Bug Fixes

Expand Down
332 changes: 332 additions & 0 deletions src/core/public/saved_objects/saved_objects_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,26 @@ describe('SavedObjectsClient', () => {
`);
});

test('makes HTTP call with workspaces', () => {
savedObjectsClient.create('index-pattern', attributes, {
workspaces: ['foo'],
});
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/index-pattern",
Object {
"body": "{\\"attributes\\":{\\"foo\\":\\"Foo\\",\\"bar\\":\\"Bar\\"},\\"workspaces\\":[\\"foo\\"]}",
"method": "POST",
"query": Object {
"overwrite": undefined,
},
},
],
]
`);
});

test('rejects when HTTP call fails', async () => {
http.fetch.mockRejectedValueOnce(new Error('Request failed'));
await expect(
Expand Down Expand Up @@ -386,6 +406,29 @@ describe('SavedObjectsClient', () => {
]
`);
});

test('makes HTTP call with workspaces', () => {
savedObjectsClient.bulkCreate([doc], {
workspaces: ['foo'],
});
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_bulk_create",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\",\\"updated_at\\":\\"${updatedAt}\\"}]",
"method": "POST",
"query": Object {
"overwrite": undefined,
"workspaces": Array [
"foo",
],
},
},
],
]
`);
});
});

describe('#bulk_update', () => {
Expand Down Expand Up @@ -510,5 +553,294 @@ describe('SavedObjectsClient', () => {
]
`);
});

test('makes HTTP call correctly with workspaces', () => {
const options = {
invalid: true,
workspaces: ['foo'],
};

// @ts-expect-error
savedObjectsClient.find(options);
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_find",
Object {
"body": undefined,
"method": "GET",
"query": Object {
"workspaces": Array [
"foo",
],
},
},
],
]
`);
});
});
});

describe('SavedObjectsClientWithWorkspaceSet', () => {
const updatedAt = new Date().toISOString();
const doc = {
id: 'AVwSwFxtcMV38qjDZoQg',
type: 'config',
attributes: { title: 'Example title' },
version: 'foo',
updated_at: updatedAt,
};

const http = httpServiceMock.createStartContract();
let savedObjectsClient: SavedObjectsClient;

beforeEach(() => {
savedObjectsClient = new SavedObjectsClient(http);
savedObjectsClient.setCurrentWorkspace('foo');
http.fetch.mockClear();
});

describe('#create', () => {
const attributes = { foo: 'Foo', bar: 'Bar' };

beforeEach(() => {
http.fetch.mockResolvedValue({ id: 'serverId', type: 'server-type', attributes });
});

test('makes HTTP call with ID', () => {
savedObjectsClient.create('index-pattern', attributes, { id: 'myId' });
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/index-pattern/myId",
Object {
"body": "{\\"attributes\\":{\\"foo\\":\\"Foo\\",\\"bar\\":\\"Bar\\"},\\"workspaces\\":[\\"foo\\"]}",
"method": "POST",
"query": Object {
"overwrite": undefined,
},
},
],
]
`);
});

test('makes HTTP call without ID', () => {
savedObjectsClient.create('index-pattern', attributes);
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/index-pattern",
Object {
"body": "{\\"attributes\\":{\\"foo\\":\\"Foo\\",\\"bar\\":\\"Bar\\"},\\"workspaces\\":[\\"foo\\"]}",
"method": "POST",
"query": Object {
"overwrite": undefined,
},
},
],
]
`);
});

test('makes HTTP call with workspaces', () => {
savedObjectsClient.create('index-pattern', attributes, {
workspaces: ['foo'],
});
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/index-pattern",
Object {
"body": "{\\"attributes\\":{\\"foo\\":\\"Foo\\",\\"bar\\":\\"Bar\\"},\\"workspaces\\":[\\"foo\\"]}",
"method": "POST",
"query": Object {
"overwrite": undefined,
},
},
],
]
`);
});
});

describe('#bulk_create', () => {
beforeEach(() => {
http.fetch.mockResolvedValue({ saved_objects: [doc] });
});

test('makes HTTP call', async () => {
await savedObjectsClient.bulkCreate([doc]);
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_bulk_create",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\",\\"updated_at\\":\\"${updatedAt}\\"}]",
"method": "POST",
"query": Object {
"overwrite": false,
"workspaces": Array [
"foo",
],
},
},
],
]
`);
});

test('makes HTTP call with overwrite query paramater', async () => {
await savedObjectsClient.bulkCreate([doc], { overwrite: true });
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_bulk_create",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\",\\"updated_at\\":\\"${updatedAt}\\"}]",
"method": "POST",
"query": Object {
"overwrite": true,
"workspaces": Array [
"foo",
],
},
},
],
]
`);
});

test('makes HTTP call with workspaces', () => {
savedObjectsClient.bulkCreate([doc], {
workspaces: ['bar'],
});
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_bulk_create",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\",\\"updated_at\\":\\"${updatedAt}\\"}]",
"method": "POST",
"query": Object {
"overwrite": undefined,
"workspaces": Array [
"bar",
],
},
},
],
]
`);
});
});

describe('#bulk_update', () => {
const bulkUpdateDoc = {
id: 'AVwSwFxtcMV38qjDZoQg',
type: 'config',
attributes: { title: 'Example title' },
version: 'foo',
};
beforeEach(() => {
http.fetch.mockResolvedValue({ saved_objects: [bulkUpdateDoc] });
});

test('makes HTTP call', async () => {
await savedObjectsClient.bulkUpdate([bulkUpdateDoc]);
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_bulk_update",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\"}]",
"method": "PUT",
"query": undefined,
},
],
]
`);
});
});

describe('#find', () => {
const object = { id: 'logstash-*', type: 'index-pattern', title: 'Test' };

beforeEach(() => {
http.fetch.mockResolvedValue({ saved_objects: [object], page: 0, per_page: 1, total: 1 });
});

test('makes HTTP call correctly mapping options into snake case query parameters', () => {
const options = {
defaultSearchOperator: 'OR' as const,
fields: ['title'],
hasReference: { id: '1', type: 'reference' },
page: 10,
perPage: 100,
search: 'what is the meaning of life?|life',
searchFields: ['title^5', 'body'],
sortField: 'sort_field',
type: 'index-pattern',
};

savedObjectsClient.find(options);
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_find",
Object {
"body": undefined,
"method": "GET",
"query": Object {
"default_search_operator": "OR",
"fields": Array [
"title",
],
"has_reference": "{\\"id\\":\\"1\\",\\"type\\":\\"reference\\"}",
"page": 10,
"per_page": 100,
"search": "what is the meaning of life?|life",
"search_fields": Array [
"title^5",
"body",
],
"sort_field": "sort_field",
"type": "index-pattern",
"workspaces": Array [
"foo",
],
},
},
],
]
`);
});

test('makes HTTP call correctly with workspaces', () => {
const options = {
invalid: true,
workspaces: ['bar'],
};

// @ts-expect-error
savedObjectsClient.find(options);
expect(http.fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/api/saved_objects/_find",
Object {
"body": undefined,
"method": "GET",
"query": Object {
"workspaces": Array [
"bar",
],
},
},
],
]
`);
});
});
});
Loading
Loading