-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [Spaces] Dynamically set the space disabled feature based on th…
- Loading branch information
Showing
5 changed files
with
251 additions
and
5 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
89 changes: 89 additions & 0 deletions
89
x-pack/plugins/spaces/server/lib/utils/space_solution_disabled_features.test.ts
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,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import type { KibanaFeature } from '@kbn/features-plugin/server'; | ||
|
||
import { withSpaceSolutionDisabledFeatures } from './space_solution_disabled_features'; | ||
|
||
const features = [ | ||
{ id: 'feature1', category: { id: 'observability' } }, | ||
{ id: 'feature2', category: { id: 'enterpriseSearch' } }, | ||
{ id: 'feature3', category: { id: 'securitySolution' } }, | ||
{ id: 'feature4', category: { id: 'should_not_be_returned' } }, // not a solution, it should never appeared in the disabled features | ||
] as KibanaFeature[]; | ||
|
||
describe('#withSpaceSolutionDisabledFeatures', () => { | ||
describe('when the space solution is not set (undefined)', () => { | ||
test('it does not remove any features', () => { | ||
const spaceDisabledFeatures: string[] = ['foo']; | ||
|
||
const result = withSpaceSolutionDisabledFeatures(features, spaceDisabledFeatures); | ||
|
||
expect(result).toEqual(['foo']); | ||
}); | ||
}); | ||
|
||
describe('when the space solution is "classic"', () => { | ||
test('it does not remove any features', () => { | ||
const spaceDisabledFeatures: string[] = ['foo']; | ||
const spaceSolution = 'classic'; | ||
|
||
const result = withSpaceSolutionDisabledFeatures( | ||
features, | ||
spaceDisabledFeatures, | ||
spaceSolution | ||
); | ||
|
||
expect(result).toEqual(['foo']); | ||
}); | ||
}); | ||
|
||
describe('when the space solution is "es"', () => { | ||
test('it removes the "oblt" and "security" features', () => { | ||
const spaceDisabledFeatures: string[] = ['foo']; | ||
const spaceSolution = 'es'; | ||
|
||
const result = withSpaceSolutionDisabledFeatures( | ||
features, | ||
spaceDisabledFeatures, | ||
spaceSolution | ||
); | ||
|
||
// merges the spaceDisabledFeatures with the disabledFeatureKeysFromSolution | ||
expect(result).toEqual(['feature1', 'feature3']); // "foo" from the spaceDisabledFeatures should not be removed | ||
}); | ||
}); | ||
|
||
describe('when the space solution is "oblt"', () => { | ||
test('it removes the "search" and "security" features', () => { | ||
const spaceDisabledFeatures: string[] = []; | ||
const spaceSolution = 'oblt'; | ||
|
||
const result = withSpaceSolutionDisabledFeatures( | ||
features, | ||
spaceDisabledFeatures, | ||
spaceSolution | ||
); | ||
|
||
expect(result).toEqual(['feature2', 'feature3']); | ||
}); | ||
}); | ||
|
||
describe('when the space solution is "security"', () => { | ||
test('it removes the "observability" and "enterpriseSearch" features', () => { | ||
const spaceDisabledFeatures: string[] = ['baz']; | ||
const spaceSolution = 'security'; | ||
|
||
const result = withSpaceSolutionDisabledFeatures( | ||
features, | ||
spaceDisabledFeatures, | ||
spaceSolution | ||
); | ||
|
||
expect(result).toEqual(['feature1', 'feature2']); // "baz" from the spaceDisabledFeatures should not be removed | ||
}); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
x-pack/plugins/spaces/server/lib/utils/space_solution_disabled_features.ts
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,64 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import type { KibanaFeature } from '@kbn/features-plugin/server'; | ||
|
||
import type { SolutionView } from '../../../common'; | ||
|
||
const getFeatureIdsForCategories = ( | ||
features: KibanaFeature[], | ||
categories: Array<'observability' | 'enterpriseSearch' | 'securitySolution'> | ||
) => { | ||
return features | ||
.filter((feature) => | ||
feature.category | ||
? categories.includes( | ||
feature.category.id as 'observability' | 'enterpriseSearch' | 'securitySolution' | ||
) | ||
: false | ||
) | ||
.map((feature) => feature.id); | ||
}; | ||
|
||
/** | ||
* When a space has a `solution` defined, we want to disable features that are not part of that solution. | ||
* This function takes the current space's disabled features and the space solution and returns | ||
* the updated array of disabled features. | ||
* | ||
* @param spaceDisabledFeatures The current space's disabled features | ||
* @param spaceSolution The current space's solution (es, oblt, security or classic) | ||
* @returns The updated array of disabled features | ||
*/ | ||
export function withSpaceSolutionDisabledFeatures( | ||
features: KibanaFeature[], | ||
spaceDisabledFeatures: string[] = [], | ||
spaceSolution: SolutionView = 'classic' | ||
): string[] { | ||
if (spaceSolution === 'classic') { | ||
return spaceDisabledFeatures; | ||
} | ||
|
||
let disabledFeatureKeysFromSolution: string[] = []; | ||
|
||
if (spaceSolution === 'es') { | ||
disabledFeatureKeysFromSolution = getFeatureIdsForCategories(features, [ | ||
'observability', | ||
'securitySolution', | ||
]); | ||
} else if (spaceSolution === 'oblt') { | ||
disabledFeatureKeysFromSolution = getFeatureIdsForCategories(features, [ | ||
'enterpriseSearch', | ||
'securitySolution', | ||
]); | ||
} else if (spaceSolution === 'security') { | ||
disabledFeatureKeysFromSolution = getFeatureIdsForCategories(features, [ | ||
'observability', | ||
'enterpriseSearch', | ||
]); | ||
} | ||
|
||
return Array.from(new Set([...disabledFeatureKeysFromSolution])); | ||
} |