Skip to content

Commit

Permalink
[Workspace] Consume workspace id in saved object client (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#6014)

* feat: consume current workspace in saved objects management and saved objects client

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add unit test for each change

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: update snapshot of unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update snapshot

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* revert: saved object management changes

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add CHANGELOG

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: address some comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: address comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: remove useless return

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: bootstrap error

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update CHANGELOG

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update CHANGELOG

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Mar 11, 2024
1 parent 0ee4023 commit 598e384
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 43 deletions.
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

0 comments on commit 598e384

Please sign in to comment.