forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from SuZhou-Joe/feature/workspace-service
Add interfaces and partial implement for public core.workspaces
- Loading branch information
Showing
7 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
export { WorkspacesClientContract, WorkspacesClient } from './workspaces_client'; | ||
export { WorkspacesStart, WorkspacesService } from './workspaces_service'; | ||
export { WorkspaceAttribute, WorkspaceFindOptions } from '../../server/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { resolve as resolveUrl } from 'url'; | ||
import type { PublicMethodsOf } from '@osd/utility-types'; | ||
import { WORKSPACES_API_BASE_URL } from '../../server/types'; | ||
import { HttpStart } from '../http'; | ||
import { WorkspaceAttribute, WorkspaceFindOptions } from '.'; | ||
|
||
/** | ||
* WorkspacesClientContract as implemented by the {@link WorkspacesClient} | ||
* | ||
* @public | ||
*/ | ||
export type WorkspacesClientContract = PublicMethodsOf<WorkspacesClient>; | ||
|
||
const join = (...uriComponents: Array<string | undefined>) => | ||
uriComponents | ||
.filter((comp): comp is string => Boolean(comp)) | ||
.map(encodeURIComponent) | ||
.join('/'); | ||
|
||
type IResponse<T> = | ||
| { | ||
result: T; | ||
success: true; | ||
} | ||
| { | ||
success: false; | ||
error?: string; | ||
}; | ||
|
||
/** | ||
* Workspaces is OpenSearchDashboards's visualize mechanism allowing admins to | ||
* organize related features | ||
* | ||
* @public | ||
*/ | ||
export class WorkspacesClient { | ||
private http: HttpStart; | ||
constructor(http: HttpStart) { | ||
this.http = http; | ||
} | ||
|
||
private getPath(path: Array<string | undefined>): string { | ||
return resolveUrl(`${WORKSPACES_API_BASE_URL}/`, join(...path)); | ||
} | ||
|
||
public async enterWorkspace(id: string): Promise<IResponse<null>> { | ||
return { | ||
success: false, | ||
error: 'Unimplement', | ||
}; | ||
} | ||
|
||
public async exitWorkspace(): Promise<IResponse<null>> { | ||
return { | ||
success: false, | ||
error: 'Unimplement', | ||
}; | ||
} | ||
|
||
public async getCurrentWorkspaceId(): Promise<IResponse<WorkspaceAttribute['id']>> { | ||
return { | ||
success: false, | ||
error: 'Unimplement', | ||
}; | ||
} | ||
|
||
public async getCurrentWorkspace(): Promise<IResponse<WorkspaceAttribute>> { | ||
return { | ||
success: false, | ||
error: 'Unimplement', | ||
}; | ||
} | ||
|
||
/** | ||
* Persists an workspace | ||
* | ||
* @param attributes | ||
* @returns | ||
*/ | ||
public create = ( | ||
attributes: Omit<WorkspaceAttribute, 'id'> | ||
): Promise<IResponse<WorkspaceAttribute>> => { | ||
if (!attributes) { | ||
return Promise.reject(new Error('requires attributes')); | ||
} | ||
|
||
const path = this.getPath([]); | ||
|
||
return this.http.fetch(path, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
attributes, | ||
}), | ||
}); | ||
}; | ||
|
||
/** | ||
* Deletes a workspace | ||
* | ||
* @param id | ||
* @returns | ||
*/ | ||
public delete = (id: string): Promise<IResponse<null>> => { | ||
if (!id) { | ||
return Promise.reject(new Error('requires id')); | ||
} | ||
|
||
return this.http.delete(this.getPath([id]), { method: 'DELETE' }); | ||
}; | ||
|
||
/** | ||
* Search for workspaces | ||
* | ||
* @param {object} [options={}] | ||
* @property {string} options.search | ||
* @property {string} options.search_fields - see OpenSearch Simple Query String | ||
* Query field argument for more information | ||
* @property {integer} [options.page=1] | ||
* @property {integer} [options.per_page=20] | ||
* @property {array} options.fields | ||
* @returns A find result with workspaces matching the specified search. | ||
*/ | ||
public list = ( | ||
options?: WorkspaceFindOptions | ||
): Promise< | ||
IResponse< | ||
WorkspaceAttribute & { | ||
total: number; | ||
perPage: number; | ||
page: number; | ||
} | ||
> | ||
> => { | ||
const path = this.getPath(['_list']); | ||
return this.http.fetch(path, { | ||
method: 'GET', | ||
query: options, | ||
}); | ||
}; | ||
|
||
/** | ||
* Fetches a single workspace | ||
* | ||
* @param {string} id | ||
* @returns The workspace for the given id. | ||
*/ | ||
public get = (id: string): Promise<IResponse<WorkspaceAttribute>> => { | ||
if (!id) { | ||
return Promise.reject(new Error('requires id')); | ||
} | ||
|
||
const path = this.getPath([id]); | ||
return this.http.fetch(path, { | ||
method: 'GET', | ||
}); | ||
}; | ||
|
||
/** | ||
* Updates a workspace | ||
* | ||
* @param {string} id | ||
* @param {object} attributes | ||
* @returns | ||
*/ | ||
public update(id: string, attributes: Partial<WorkspaceAttribute>): Promise<IResponse<boolean>> { | ||
if (!id || !attributes) { | ||
return Promise.reject(new Error('requires id and attributes')); | ||
} | ||
|
||
const path = this.getPath([id]); | ||
const body = { | ||
attributes, | ||
}; | ||
|
||
return this.http | ||
.fetch(path, { | ||
method: 'PUT', | ||
body: JSON.stringify(body), | ||
}) | ||
.then((resp: WorkspaceAttribute) => { | ||
return { | ||
result: true, | ||
success: true, | ||
}; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { CoreService } from 'src/core/types'; | ||
import { WorkspacesClient, WorkspacesClientContract } from './workspaces_client'; | ||
import { HttpStart } from '..'; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface WorkspacesStart { | ||
client: WorkspacesClientContract; | ||
} | ||
|
||
export class WorkspacesService implements CoreService<void, WorkspacesStart> { | ||
public async setup() {} | ||
public async start({ http }: { http: HttpStart }): Promise<WorkspacesStart> { | ||
return { client: new WorkspacesClient(http) }; | ||
} | ||
public async stop() {} | ||
} |