From 4dbf3d3e4daacc77a60bc125c6dbdbb0de9aabc2 Mon Sep 17 00:00:00 2001 From: dlaird-ovo Date: Wed, 15 Mar 2023 09:42:45 +0000 Subject: [PATCH 1/3] 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'; From 76b1c126bd1b3571abe701d9a7bcb0647d963434 Mon Sep 17 00:00:00 2001 From: dlaird-ovo Date: Wed, 15 Mar 2023 10:20:41 +0000 Subject: [PATCH 2/3] fixing up api-report.md Signed-off-by: dlaird-ovo --- plugins/catalog/api-report.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index 5a81ea8d76..c678ce1d3d 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -433,11 +433,6 @@ 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; @@ -449,6 +444,11 @@ export function isNamespace( // @public export function isOrphan(entity: Entity): boolean; +// @public +export function isResourceType( + types: string | string[], +): (entity: Entity) => boolean; + // @public (undocumented) export type PluginCatalogComponentsNameToClassKey = { PluginCatalogEntityLinksEmptyState: EntityLinksEmptyStateClassKey; From 44e3878a8d797892a83d065dfb478e56955ae011 Mon Sep 17 00:00:00 2001 From: dlaird-ovo Date: Wed, 15 Mar 2023 11:22:24 +0000 Subject: [PATCH 3/3] Fix review feedback Signed-off-by: dlaird-ovo --- .../components/EntitySwitch/conditions.test.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts index 079a6f5943..5d8c7c8898 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts @@ -24,18 +24,25 @@ import { const kubernetesClusterResource: Entity = { apiVersion: '', - kind: 'resource', + kind: 'Resource', metadata: { name: 'aKubernetesCluster' }, spec: { type: 'kubernetes-cluster' }, }; const databaseResource: Entity = { apiVersion: '', - kind: 'resource', + kind: 'Resource', metadata: { name: 'aDatabase' }, spec: { type: 'database' }, }; +const notResource: Entity = { + apiVersion: '', + kind: 'not-Resource', + metadata: { name: 'aService' }, + spec: { type: 'service' }, +}; + const serviceComponent: Entity = { apiVersion: '', kind: 'component', @@ -79,10 +86,10 @@ const bNamespace: Entity = { }; describe('isResourceType', () => { - it('should false on non component kinds', () => { - const checkEntity = isResourceType('service'); + it('should false on non resource kinds', () => { + const checkEntity = isResourceType('kubernetes-cluster'); - expect(checkEntity(notComponent)).not.toBeTruthy(); + expect(checkEntity(notResource)).not.toBeTruthy(); }); it('should check for the intended type', () => { const checkEntity = isResourceType('kubernetes-cluster');