Skip to content

Commit

Permalink
[MDS] Fix the dsm plugin setup when mds feature flag is disabled (ope…
Browse files Browse the repository at this point in the history
…nsearch-project#7163)

* Fix the dsm plugin setup when mds feature flag is disabled

Signed-off-by: Ryan Liang <jiallian@amazon.com>

* Changeset file for PR opensearch-project#7163 created/updated

---------

Signed-off-by: Ryan Liang <jiallian@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
RyanL1997 and opensearch-changeset-bot[bot] committed Jul 22, 2024
1 parent bb4e1ad commit 5e51914
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 18 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7163.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [MDS] Fix the dsm plugin setup when mds feature flag is disabled ([#7163](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7163))
104 changes: 103 additions & 1 deletion src/plugins/data_source_management/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,43 @@
import { coreMock } from '../../../core/public/mocks';
import { DataSourceManagementPluginStart } from './plugin';
import { testDataSourceManagementPlugin, createAuthenticationMethod } from './mocks';
import {
DataSourceManagementPlugin,
DataSourceManagementSetupDependencies,
DSM_APP_ID,
} from './plugin';
import { PLUGIN_NAME } from '../common';

describe('#dataSourceManagement', () => {
let coreSetup: any;
let coreStart: any;
let mockDataSourceManagementPluginStart: MockedKeys<DataSourceManagementPluginStart>;

const managementMock = {
sections: {
section: {
opensearchDashboards: {
registerApp: jest.fn(),
},
},
},
};

const indexPatternManagementMock = {
columns: {
register: jest.fn(),
},
};

beforeEach(() => {
coreSetup = coreMock.createSetup({ pluginStartContract: mockDataSourceManagementPluginStart });
coreSetup = {
...coreMock.createSetup({ pluginStartContract: mockDataSourceManagementPluginStart }),
management: managementMock,
indexPatternManagement: indexPatternManagementMock,
};
coreStart = coreMock.createStart();
});

it('can register custom authentication method', () => {
const { setup, doStart } = testDataSourceManagementPlugin(coreSetup, coreStart);
const typeA = createAuthenticationMethod({ name: 'typeA' });
Expand All @@ -26,4 +54,78 @@ describe('#dataSourceManagement', () => {
getDataSourceMenu: expect.any(Function),
});
});

it('should not register any authentication method if feature flag is disabled', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: undefined, // Feature flag disabled
};

const setup = plugin.setup(coreSetup, setupDeps);
expect(setup).toBeUndefined();
});

it('should return setup object with methods when feature flag is enabled', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: {
awsSigV4AuthEnabled: true,
noAuthenticationTypeEnabled: true,
usernamePasswordAuthEnabled: true,
hideLocalCluster: false,
} as any,
};

const setup = plugin.setup(coreSetup, setupDeps);
expect(setup).toBeDefined();
expect(setup?.registerAuthenticationMethod).toBeInstanceOf(Function);
expect(setup?.dataSourceSelection).toBeInstanceOf(Object);
expect(setup?.ui.DataSourceSelector).toBeInstanceOf(Function);
expect(setup?.ui.getDataSourceMenu).toBeInstanceOf(Function);
});

it('should throw error if registering authentication method after startup', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: {
awsSigV4AuthEnabled: true,
noAuthenticationTypeEnabled: true,
usernamePasswordAuthEnabled: true,
hideLocalCluster: false,
} as any,
};

const setup = plugin.setup(coreSetup, setupDeps);
plugin.start(coreStart);
expect(() => {
setup?.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' }));
}).toThrow('cannot call `registerAuthenticationMethod` after data source management startup.');
});

it('should register application in the management section', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: undefined, // Feature flag disabled
};

plugin.setup(coreSetup, setupDeps);
expect(
setupDeps.management.sections.section.opensearchDashboards.registerApp
).toHaveBeenCalledWith(
expect.objectContaining({
id: DSM_APP_ID,
title: PLUGIN_NAME,
order: 1,
mount: expect.any(Function),
})
);
});
});
38 changes: 21 additions & 17 deletions src/plugins/data_source_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class DataSourceManagementPlugin
setBreadcrumbs: coreStart.chrome.setBreadcrumbs,
wrapInPage: true,
},
this.authMethodsRegistry
this.authMethodsRegistry,
featureFlagStatus
);
},
});
Expand Down Expand Up @@ -185,6 +186,11 @@ export class DataSourceManagementPlugin
},
]);

// when the feature flag is disabled, we don't need to register any of the mds components
if (!featureFlagStatus) {
return undefined;
}

const registerAuthenticationMethod = (authMethod: AuthenticationMethod) => {
if (this.started) {
throw new Error(
Expand All @@ -194,29 +200,27 @@ export class DataSourceManagementPlugin
this.authMethodsRegistry.registerAuthenticationMethod(authMethod);
};

if (dataSource) {
if (dataSource.noAuthenticationTypeEnabled) {
registerAuthenticationMethod(noAuthCredentialAuthMethod);
}
if (dataSource.usernamePasswordAuthEnabled) {
registerAuthenticationMethod(usernamePasswordAuthMethod);
}
if (dataSource.awsSigV4AuthEnabled) {
registerAuthenticationMethod(sigV4AuthMethod);
}

setHideLocalCluster({ enabled: dataSource.hideLocalCluster });
setUiSettings(uiSettings);
// This instance will be got in each data source selector component.
setDataSourceSelection(this.dataSourceSelection);
if (dataSource.noAuthenticationTypeEnabled) {
registerAuthenticationMethod(noAuthCredentialAuthMethod);
}
if (dataSource.usernamePasswordAuthEnabled) {
registerAuthenticationMethod(usernamePasswordAuthMethod);
}
if (dataSource.awsSigV4AuthEnabled) {
registerAuthenticationMethod(sigV4AuthMethod);
}

setHideLocalCluster({ enabled: dataSource.hideLocalCluster });
setUiSettings(uiSettings);
// This instance will be got in each data source selector component.
setDataSourceSelection(this.dataSourceSelection);

return {
registerAuthenticationMethod,
// Other plugins can get this instance from setupDeps and use to get selected data sources.
dataSourceSelection: this.dataSourceSelection,
ui: {
DataSourceSelector: dataSource ? createDataSourceSelector(uiSettings, dataSource) : null,
DataSourceSelector: createDataSourceSelector(uiSettings, dataSource),
getDataSourceMenu: <T>() => createDataSourceMenu<T>(),
},
getDefaultDataSourceId,
Expand Down

0 comments on commit 5e51914

Please sign in to comment.