Skip to content

Commit

Permalink
Revert "[Refractor] Feature flag (#102)"
Browse files Browse the repository at this point in the history
This reverts commit e2f277c.
  • Loading branch information
ruanyl committed Sep 6, 2023
1 parent 8802477 commit c600ca7
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 121 deletions.
3 changes: 1 addition & 2 deletions src/plugins/workspace/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { schema, TypeOf } from '@osd/config-schema';

export const configSchema = schema.object({
enabled: schema.boolean({ defaultValue: false }),
dashboardAdmin: schema.object(
{
backendRoles: schema.arrayOf(schema.string(), {
Expand All @@ -21,5 +22,3 @@ export const configSchema = schema.object({
});

export type ConfigSchema = TypeOf<typeof configSchema>;

export const FEATURE_FLAG_KEY_IN_UI_SETTING = 'workspace_enabled';
14 changes: 1 addition & 13 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,7 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
public async setup(core: CoreSetup, { savedObjectsManagement }: WorkspacePluginSetupDeps) {
const workspaceClient = new WorkspaceClient(core.http, core.workspaces);
workspaceClient.init();
const featureFlagResp = await workspaceClient.getSettings();
if (featureFlagResp.success) {
core.workspaces.workspaceEnabled$.next(featureFlagResp.result.enabled);
} else {
core.workspaces.workspaceEnabled$.next(false);
}

if (!core.workspaces.workspaceEnabled$.getValue()) {
return {};
}
core.workspaces.workspaceEnabled$.next(true);

core.workspaces.registerWorkspaceMenuRender(renderWorkspaceMenu);

Expand Down Expand Up @@ -243,9 +234,6 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
}

public start(core: CoreStart) {
if (!core.workspaces.workspaceEnabled$.getValue()) {
return {};
}
this.coreStart = core;

this.currentWorkspaceSubscription = this._changeSavedObjectCurrentWorkspace();
Expand Down
12 changes: 0 additions & 12 deletions src/plugins/workspace/public/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,6 @@ export class WorkspaceClient {
return result;
}

public async getSettings(): Promise<
IResponse<{
enabled: boolean;
}>
> {
const result = await this.safeFetch(this.getPath(['settings']), {
method: 'get',
});

return result;
}

public stop() {
this.workspaces.workspaceList$.unsubscribe();
this.workspaces.currentWorkspaceId$.unsubscribe();
Expand Down
76 changes: 8 additions & 68 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { i18n } from '@osd/i18n';
import { BehaviorSubject, Observable } from 'rxjs';
import { Observable } from 'rxjs';

import {
PluginInitializerContext,
Expand All @@ -27,27 +27,18 @@ import { WorkspaceClientWithSavedObject } from './workspace_client';
import { WorkspaceSavedObjectsClientWrapper } from './saved_objects';
import { registerRoutes } from './routes';
import { WORKSPACE_OVERVIEW_APP_ID, WORKSPACE_UPDATE_APP_ID } from '../common/constants';
import { ConfigSchema, FEATURE_FLAG_KEY_IN_UI_SETTING } from '../config';
import { ConfigSchema } from '../config';

export class WorkspacePlugin implements Plugin<{}, {}> {
private readonly logger: Logger;
private client?: IWorkspaceDBImpl;
private coreStart?: CoreStart;
private config$: Observable<ConfigSchema>;
private enabled$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

private get isEnabled() {
return this.enabled$.getValue();
}

private proxyWorkspaceTrafficToRealHandler(setupDeps: CoreSetup) {
/**
* Proxy all {basePath}/w/{workspaceId}{osdPath*} paths to {basePath}{osdPath*}
*/
setupDeps.http.registerOnPreRouting(async (request, response, toolkit) => {
if (!this.isEnabled) {
return toolkit.next();
}
const regexp = /\/w\/([^\/]*)/;
const matchedResult = request.url.pathname.match(regexp);

Expand Down Expand Up @@ -90,27 +81,14 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
http: core.http,
logger: this.logger,
client: this.client as IWorkspaceDBImpl,
enabled$: this.enabled$,
config$: this.config$,
});

core.savedObjects.setClientFactoryProvider(
(repositoryFactory) => ({ request, includedHiddenTypes }) => {
const enabled = this.isEnabled;
if (enabled) {
return new SavedObjectsClient(repositoryFactory.createInternalRepository());
}

return new SavedObjectsClient(
repositoryFactory.createScopedRepository(request, includedHiddenTypes)
);
}
core.savedObjects.setClientFactoryProvider((repositoryFactory) => () =>
new SavedObjectsClient(repositoryFactory.createInternalRepository())
);

return {
client: this.client,
enabled$: this.enabled$,
setWorkspaceFeatureFlag: this.setWorkspaceFeatureFlag,
};
}

Expand Down Expand Up @@ -142,11 +120,8 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
}
}

private async setupWorkspaces() {
if (!this.coreStart) {
throw new Error('UI setting client can not be found');
}
const internalRepository = this.coreStart.savedObjects.createInternalRepository();
private async setupWorkspaces(startDeps: CoreStart) {
const internalRepository = startDeps.savedObjects.createInternalRepository();
this.client?.setInternalRepository(internalRepository);
const publicWorkspaceACL = new ACL().addPermission(
[WorkspacePermissionMode.LibraryRead, WorkspacePermissionMode.LibraryWrite],
Expand Down Expand Up @@ -188,50 +163,15 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
]);
}

private async getUISettingClient() {
if (!this.coreStart) {
throw new Error('UI setting client can not be found');
}
const { uiSettings, savedObjects } = this.coreStart as CoreStart;
const internalRepository = savedObjects.createInternalRepository();
const savedObjectClient = new SavedObjectsClient(internalRepository);
return uiSettings.asScopedToClient(savedObjectClient);
}

private async setWorkspaceFeatureFlag(featureFlag: boolean) {
const uiSettingClient = await this.getUISettingClient();
await uiSettingClient.set(FEATURE_FLAG_KEY_IN_UI_SETTING, featureFlag);
this.enabled$.next(featureFlag);
}

private async setupWorkspaceFeatureFlag() {
const uiSettingClient = await this.getUISettingClient();
const workspaceEnabled = await uiSettingClient.get(FEATURE_FLAG_KEY_IN_UI_SETTING);
this.enabled$.next(!!workspaceEnabled);
return workspaceEnabled;
}

public start(core: CoreStart) {
this.logger.debug('Starting SavedObjects service');

this.coreStart = core;

this.setupWorkspaceFeatureFlag();

this.enabled$.subscribe((enabled) => {
if (enabled) {
this.setupWorkspaces();
}
});
this.setupWorkspaces(core);

return {
client: this.client as IWorkspaceDBImpl,
enabled$: this.enabled$,
setWorkspaceFeatureFlag: this.setWorkspaceFeatureFlag,
};
}

public stop() {
this.enabled$.unsubscribe();
}
public stop() {}
}
26 changes: 0 additions & 26 deletions src/plugins/workspace/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { schema } from '@osd/config-schema';
import { BehaviorSubject, Observable } from 'rxjs';
import { first } from 'rxjs/operators';
import { ensureRawRequest } from '../../../../core/server';

import {
Expand All @@ -15,7 +13,6 @@ import {
WorkspacePermissionMode,
} from '../../../../core/server';
import { IWorkspaceDBImpl, WorkspaceRoutePermissionItem } from '../types';
import { ConfigSchema } from '../../config';

const WORKSPACES_API_BASE_URL = '/api/workspaces';

Expand Down Expand Up @@ -85,14 +82,10 @@ export function registerRoutes({
client,
logger,
http,
enabled$,
config$,
}: {
client: IWorkspaceDBImpl;
logger: Logger;
http: CoreSetup['http'];
enabled$: BehaviorSubject<boolean>;
config$: Observable<ConfigSchema>;
}) {
const router = http.createRouter();
router.post(
Expand Down Expand Up @@ -263,23 +256,4 @@ export function registerRoutes({
return res.ok({ body: result });
})
);

router.get(
{
path: `${WORKSPACES_API_BASE_URL}/settings`,
validate: {},
},
router.handleLegacyErrors(async (context, req, res) => {
const config = await config$.pipe(first()).toPromise();
return res.ok({
body: {
success: true,
result: {
...config,
enabled: enabled$.getValue(),
},
},
});
})
);
}

0 comments on commit c600ca7

Please sign in to comment.