Merge pull request #16862 from dlaird-ovo/feat/add_resource_switch

Adding new EntitySwitch for differing views depending on ResourceType
This commit is contained in:
Fredrik Adelöw
2023-03-15 13:51:58 +01:00
committed by GitHub
5 changed files with 82 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
@@ -444,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;
@@ -15,7 +15,33 @@
*/
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 notResource: Entity = {
apiVersion: '',
kind: 'not-Resource',
metadata: { name: 'aService' },
spec: { type: 'service' },
};
const serviceComponent: Entity = {
apiVersion: '',
@@ -59,6 +85,26 @@ const bNamespace: Entity = {
spec: { type: 'service' },
};
describe('isResourceType', () => {
it('should false on non resource kinds', () => {
const checkEntity = isResourceType('kubernetes-cluster');
expect(checkEntity(notResource)).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';