From 4dbf3d3e4daacc77a60bc125c6dbdbb0de9aabc2 Mon Sep 17 00:00:00 2001 From: dlaird-ovo Date: Wed, 15 Mar 2023 09:42:45 +0000 Subject: [PATCH] Adding new EntitySwitch for differing views depending on ResourceType Signed-off-by: dlaird-ovo --- .changeset/hungry-monkeys-reply.md | 5 +++ plugins/catalog/api-report.md | 5 +++ .../EntitySwitch/conditions.test.ts | 41 ++++++++++++++++++- .../src/components/EntitySwitch/conditions.ts | 20 ++++++++- .../src/components/EntitySwitch/index.ts | 7 +++- 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 .changeset/hungry-monkeys-reply.md diff --git a/.changeset/hungry-monkeys-reply.md b/.changeset/hungry-monkeys-reply.md new file mode 100644 index 0000000000..c7f5d54ea1 --- /dev/null +++ b/.changeset/hungry-monkeys-reply.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': minor +--- + +Added a new EntitySwitch isResourceType to allow different views depending on Resource type diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index d891ebb7c8..5a81ea8d76 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -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; diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts index dfed546fd8..079a6f5943 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts @@ -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'); diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index 86fd38a900..222a76b0a9 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -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 diff --git a/plugins/catalog/src/components/EntitySwitch/index.ts b/plugins/catalog/src/components/EntitySwitch/index.ts index 6cccf4be6e..0cb85ae6eb 100644 --- a/plugins/catalog/src/components/EntitySwitch/index.ts +++ b/plugins/catalog/src/components/EntitySwitch/index.ts @@ -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';