Skip to content

Commit

Permalink
resolve conflicts in http types
Browse files Browse the repository at this point in the history
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
  • Loading branch information
yuye-aws committed Jul 24, 2023
2 parents 12ac2db + 84fee6a commit 0b8d50e
Show file tree
Hide file tree
Showing 34 changed files with 803 additions and 58 deletions.
16 changes: 10 additions & 6 deletions src/core/public/http/base_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import { modifyUrl } from '@osd/std';
import type { PrependOptions } from './types';

export class BasePath {
constructor(
Expand All @@ -45,7 +46,8 @@ export class BasePath {
return this.basePath;
};

public prepend = (path: string, withoutWorkspace: boolean = false): string => {
public prepend = (path: string, prependOptions?: PrependOptions): string => {
const { withoutWorkspace } = prependOptions || {};
const basePath = withoutWorkspace ? this.basePath : this.get();
if (!basePath) return path;
return modifyUrl(path, (parts) => {
Expand All @@ -55,17 +57,19 @@ export class BasePath {
});
};

public remove = (path: string): string => {
if (!this.get()) {
public remove = (path: string, prependOptions?: PrependOptions): string => {
const { withoutWorkspace } = prependOptions || {};
const basePath = withoutWorkspace ? this.basePath : this.get();
if (!basePath) {
return path;
}

if (path === this.get()) {
if (path === basePath) {
return '/';
}

if (path.startsWith(`${this.get()}/`)) {
return path.slice(this.get().length);
if (path.startsWith(`${basePath}/`)) {
return path.slice(basePath.length);
}

return path;
Expand Down
3 changes: 2 additions & 1 deletion src/core/public/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { LoadingCountService } from './loading_count_service';
import { Fetch } from './fetch';
import { CoreService } from '../../types';
import { getWorkspaceIdFromUrl } from '../utils';
import { WORKSPACE_PATH_PREFIX } from '../../utils/constants';

interface HttpDeps {
injectedMetadata: InjectedMetadataSetup;
Expand All @@ -54,7 +55,7 @@ export class HttpService implements CoreService<HttpSetup, HttpStart> {
let workspaceBasePath = '';
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
workspaceBasePath = `/w/${workspaceId}`;
workspaceBasePath = `${WORKSPACE_PATH_PREFIX}/${workspaceId}`;
}
const basePath = new BasePath(
injectedMetadata.getBasePath(),
Expand Down
14 changes: 12 additions & 2 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export interface HttpSetup {
*/
export type HttpStart = HttpSetup;

/**
* prepend options
*
* withoutWorkspace option will prepend a relative url with only basePath
* workspaceId will rewrite the /w/{workspaceId} part, if workspace id is an empty string, prepend will remove the workspaceId part
*/
export interface PrependOptions {
withoutWorkspace?: boolean;
}

/**
* APIs for manipulating the basePath on URL segments.
* @public
Expand All @@ -105,12 +115,12 @@ export interface IBasePath {
/**
* Prepends `path` with the basePath + workspace.
*/
prepend: (url: string, withoutWorkspace?: boolean) => string;
prepend: (url: string, prependOptions?: PrependOptions) => string;

/**
* Removes the prepended basePath + workspace from the `path`.
*/
remove: (url: string) => string;
remove: (url: string, prependOptions?: PrependOptions) => string;

/**
* Returns the server's root basePath as configured, without any namespace prefix.
Expand Down
2 changes: 2 additions & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,5 @@ export {
WorkspaceAttribute,
WorkspaceFindOptions,
} from './workspace';

export { getWorkspaceIdFromUrl, WORKSPACE_TYPE } from './utils';
3 changes: 2 additions & 1 deletion src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
export { shareWeakReplay } from './share_weak_replay';
export { Sha256 } from './crypto';
export { MountWrapper, mountReactNode } from './mount';
export { getWorkspaceIdFromUrl } from './workspace';
export { getWorkspaceIdFromUrl, WORKSPACE_TYPE } from './workspace';
export { WORKSPACE_PATH_PREFIX } from '../../utils';
2 changes: 2 additions & 0 deletions src/core/public/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const getWorkspaceIdFromUrl = (url: string): string => {

return '';
};

export const WORKSPACE_TYPE = 'workspace';
2 changes: 1 addition & 1 deletion src/core/public/workspace/workspaces_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class WorkspacesClient {
/**
* Initialize workspace list
*/
init() {
public init() {
this.updateWorkspaceListAndNotify();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,4 @@ export const config = {
},
};

export { formatWorkspaces, workspacesValidator } from './workspaces';
export { formatWorkspaces, workspacesValidator, WORKSPACE_TYPE } from './workspaces';
82 changes: 82 additions & 0 deletions src/core/server/saved_objects/routes/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { schema } from '@osd/config-schema';
import { IRouter } from '../../http';
import { SavedObjectConfig } from '../saved_objects_config';
import { exportSavedObjectsToStream } from '../export';
import { validateObjects } from './utils';
import { importSavedObjectsFromStream } from '../import';

export const registerCopyRoute = (router: IRouter, config: SavedObjectConfig) => {
const { maxImportExportSize } = config;

router.post(
{
path: '/_copy',
validate: {
body: schema.object({
objects: schema.maybe(
schema.arrayOf(
schema.object({
type: schema.string(),
id: schema.string(),
}),
{ maxSize: maxImportExportSize }
)
),
includeReferencesDeep: schema.boolean({ defaultValue: false }),
targetWorkspace: schema.string(),
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const savedObjectsClient = context.core.savedObjects.client;
const { objects, includeReferencesDeep, targetWorkspace } = req.body;

// need to access the registry for type validation, can't use the schema for this
const supportedTypes = context.core.savedObjects.typeRegistry
.getImportableAndExportableTypes()
.map((t) => t.name);

if (objects) {
const validationError = validateObjects(objects, supportedTypes);
if (validationError) {
return res.badRequest({
body: {
message: validationError,
},
});
}
}

const objectsListStream = await exportSavedObjectsToStream({
savedObjectsClient,
objects,
exportSizeLimit: maxImportExportSize,
includeReferencesDeep,
excludeExportDetails: true,
});

const result = await importSavedObjectsFromStream({
savedObjectsClient: context.core.savedObjects.client,
typeRegistry: context.core.savedObjects.typeRegistry,
readStream: objectsListStream,
objectLimit: maxImportExportSize,
overwrite: false,
createNewCopies: true,
workspaces: [targetWorkspace],
});

return res.ok({ body: result });
})
);
};
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { registerExportRoute } from './export';
import { registerImportRoute } from './import';
import { registerResolveImportErrorsRoute } from './resolve_import_errors';
import { registerMigrateRoute } from './migrate';
import { registerCopyRoute } from './copy';

export function registerRoutes({
http,
Expand All @@ -70,6 +71,7 @@ export function registerRoutes({
registerLogLegacyImportRoute(router, logger);
registerExportRoute(router, config);
registerImportRoute(router, config);
registerCopyRoute(router, config);
registerResolveImportErrorsRoute(router, config);

const internalRouter = http.createRouter('/internal/saved_objects/');
Expand Down
7 changes: 4 additions & 3 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export interface SavedObjectsIncrementCounterOptions extends SavedObjectsBaseOpt
*
* @public
*/
export interface SavedObjectsDeleteByNamespaceOptions extends SavedObjectsBaseOptions {
export interface SavedObjectsDeleteByNamespaceOptions
extends Omit<SavedObjectsBaseOptions, 'workspaces'> {
/** The OpenSearch supports only boolean flag for this operation */
refresh?: boolean;
}
Expand Down Expand Up @@ -891,7 +892,7 @@ export class SavedObjectsRepository {
*/
async bulkGet<T = unknown>(
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
options: Omit<SavedObjectsBaseOptions, 'workspaces'> = {}
): Promise<SavedObjectsBulkResponse<T>> {
const namespace = normalizeNamespace(options.namespace);

Expand Down Expand Up @@ -979,7 +980,7 @@ export class SavedObjectsRepository {
async get<T = unknown>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
options: Omit<SavedObjectsBaseOptions, 'workspaces'> = {}
): Promise<SavedObject<T>> {
if (!this._allowedTypes.includes(type)) {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export interface SavedObjectsBulkUpdateOptions extends SavedObjectsBaseOptions {
*
* @public
*/
export interface SavedObjectsDeleteOptions extends SavedObjectsBaseOptions {
export interface SavedObjectsDeleteOptions extends Omit<SavedObjectsBaseOptions, 'workspaces'> {
/** The OpenSearch Refresh setting for this operation */
refresh?: MutatingOperationRefreshSetting;
/** Force deletion of an object that exists in multiple namespaces */
Expand Down
6 changes: 6 additions & 0 deletions src/core/server/workspaces/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const WORKSPACE_TYPE = 'workspace';
1 change: 1 addition & 0 deletions src/core/server/workspaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export { WorkspaceAttribute, WorkspaceFindOptions } from './types';

export { WorkspacePermissionControl } from './workspace_permission_control';
export { workspacesValidator, formatWorkspaces } from './utils';
export { WORKSPACE_TYPE } from './constants';
1 change: 1 addition & 0 deletions src/core/server/workspaces/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export { workspace } from './workspace';
export { WorkspaceSavedObjectsClientWrapper } from './workspace_saved_objects_client_wrapper';
23 changes: 5 additions & 18 deletions src/core/server/workspaces/saved_objects/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,15 @@
*/

import { SavedObjectsType } from 'opensearch-dashboards/server';
import { WORKSPACE_TYPE } from '../constants';

export const workspace: SavedObjectsType = {
name: 'workspace',
name: WORKSPACE_TYPE,
namespaceType: 'agnostic',
hidden: false,
management: {
icon: 'apps', // todo: pending ux #2034
defaultSearchField: 'title',
importableAndExportable: true,
getTitle(obj) {
return obj.attributes.title;
},
getEditUrl(obj) {
return `/management/opensearch-dashboards/dataSources/${encodeURIComponent(obj.id)}`;
},
getInAppUrl(obj) {
return {
path: `/app/management/opensearch-dashboards/dataSources/${encodeURIComponent(obj.id)}`,
uiCapabilitiesPath: 'management.opensearchDashboards.dataSources',
};
},
},
/**
* workspace won't appear in management page.
*/
mappings: {
dynamic: false,
properties: {
Expand Down
Loading

0 comments on commit 0b8d50e

Please sign in to comment.