Adding new EntitySwitch for differing views depending on ResourceType

Signed-off-by: dlaird-ovo <daniel.laird@ovo.com>
This commit is contained in:
dlaird-ovo
2023-03-15 09:42:45 +00:00
parent 003e13297f
commit 4dbf3d3e4d
5 changed files with 75 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': minor
---
Added a new EntitySwitch isResourceType to allow different views depending on Resource type
+5
View File
@@ -433,6 +433,11 @@ export function isComponentType(
types: string | string[],
): (entity: Entity) => boolean;
// @public
export function isResourceType(
types: string | string[],
): (entity: Entity) => boolean;
// @public
export function isKind(kinds: string | string[]): (entity: Entity) => boolean;
@@ -15,7 +15,26 @@
*/
import { Entity } from '@backstage/catalog-model';
import { isComponentType, isKind, isNamespace } from './conditions';
import {
isComponentType,
isKind,
isNamespace,
isResourceType,
} from './conditions';
const kubernetesClusterResource: Entity = {
apiVersion: '',
kind: 'resource',
metadata: { name: 'aKubernetesCluster' },
spec: { type: 'kubernetes-cluster' },
};
const databaseResource: Entity = {
apiVersion: '',
kind: 'resource',
metadata: { name: 'aDatabase' },
spec: { type: 'database' },
};
const serviceComponent: Entity = {
apiVersion: '',
@@ -59,6 +78,26 @@ const bNamespace: Entity = {
spec: { type: 'service' },
};
describe('isResourceType', () => {
it('should false on non component kinds', () => {
const checkEntity = isResourceType('service');
expect(checkEntity(notComponent)).not.toBeTruthy();
});
it('should check for the intended type', () => {
const checkEntity = isResourceType('kubernetes-cluster');
expect(checkEntity(databaseResource)).not.toBeTruthy();
expect(checkEntity(kubernetesClusterResource)).toBeTruthy();
});
it('should check for multiple types', () => {
const checkEntity = isResourceType(['database', 'kubernetes-cluster']);
expect(checkEntity(databaseResource)).toBeTruthy();
expect(checkEntity(kubernetesClusterResource)).toBeTruthy();
});
});
describe('isComponentType', () => {
it('should false on non component kinds', () => {
const checkEntity = isComponentType('service');
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { ComponentEntity, Entity } from '@backstage/catalog-model';
import {
ComponentEntity,
ResourceEntity,
Entity,
} from '@backstage/catalog-model';
function strCmp(a: string | undefined, b: string | undefined): boolean {
return Boolean(
@@ -50,6 +54,20 @@ export function isComponentType(types: string | string[]) {
};
}
/**
* For use in EntitySwitch.Case. Matches if the entity is a Resource of a given spec.type.
* @public
*/
export function isResourceType(types: string | string[]) {
return (entity: Entity) => {
if (!strCmp(entity.kind, 'resource')) {
return false;
}
const resourceEntity = entity as ResourceEntity;
return strCmpAll(resourceEntity.spec.type, types);
};
}
/**
* For use in EntitySwitch.Case. Matches if the entity is in a given namespace.
* @public
@@ -16,4 +16,9 @@
export { EntitySwitch } from './EntitySwitch';
export type { EntitySwitchProps, EntitySwitchCaseProps } from './EntitySwitch';
export { isKind, isNamespace, isComponentType } from './conditions';
export {
isKind,
isNamespace,
isComponentType,
isResourceType,
} from './conditions';