From 05e63bedea7a923eff67052367a254ae8bc4ddaf Mon Sep 17 00:00:00 2001 From: Alexandre Couedelo Date: Mon, 11 Nov 2024 14:56:46 +0000 Subject: [PATCH] chore: make the code simpler and do not emit errors --- .../CatalogOPAEntityValidator.test.ts | 14 ++-------- .../processor/CatalogOPAEntityValidator.ts | 27 +++---------------- 2 files changed, 5 insertions(+), 36 deletions(-) diff --git a/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.test.ts b/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.test.ts index 0256f2b..79d47ef 100644 --- a/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.test.ts +++ b/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.test.ts @@ -1,7 +1,5 @@ import {Entity} from "@backstage/catalog-model"; import {CatalogOPAEntityValidator} from "./CatalogOPAEntityValidator"; -import {mockServices} from "@backstage/backend-test-utils"; -import {LocationSpec} from "@backstage/plugin-catalog-common"; import {OpaResult} from "../service/entityCheckerApi"; describe('CatalogOPAEntityValidator', () => { @@ -14,13 +12,6 @@ describe('CatalogOPAEntityValidator', () => { }, }; - const location: LocationSpec = { - type: 'url', - target: - 'https://example.com', - }; - - const mockLogger = mockServices.logger.mock(); const mockEntityCheckerApi = { checkEntity: jest.fn((): Promise => Promise.resolve( { @@ -36,11 +27,10 @@ describe('CatalogOPAEntityValidator', () => { } )) }; - const mockCatalogProcessorEmit = jest.fn() - const processor = new CatalogOPAEntityValidator(mockEntityCheckerApi, mockLogger) + const processor = new CatalogOPAEntityValidator(mockEntityCheckerApi) - expect(await processor.preProcessEntity(entity, location, mockCatalogProcessorEmit)).toEqual({ + expect(await processor.preProcessEntity(entity)).toEqual({ apiVersion: 'backstage.io/v1alpha1', kind: 'Component', metadata: { diff --git a/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.ts b/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.ts index 5464cab..76f7b44 100644 --- a/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.ts +++ b/plugins/backstage-opa-backend/src/processor/CatalogOPAEntityValidator.ts @@ -1,8 +1,6 @@ -import { CatalogProcessor, CatalogProcessorEmit, processingResult } from '@backstage/plugin-catalog-node'; +import { CatalogProcessor } from '@backstage/plugin-catalog-node'; import {Entity} from "@backstage/catalog-model"; -import { LocationSpec } from '@backstage/plugin-catalog-common' import {EntityCheckerApi} from "../service/entityCheckerApi"; -import {LoggerService} from "@backstage/backend-plugin-api"; import { merge } from 'lodash'; const OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION = "entity-checker.opa/good-entity" @@ -10,7 +8,6 @@ const OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION = "entity-checker.opa/good-entit export class CatalogOPAEntityValidator implements CatalogProcessor { constructor( private readonly api: EntityCheckerApi, - private readonly logger: LoggerService ) {} getProcessorName(): string { @@ -19,35 +16,17 @@ export class CatalogOPAEntityValidator implements CatalogProcessor { async preProcessEntity( entity: Entity, - location: LocationSpec, - emit: CatalogProcessorEmit, ): Promise { - let isGoodEntity = true - - await this.api.checkEntity({ + let opaResult = await this.api.checkEntity({ entityMetadata: JSON.stringify(entity) - }).then(data => { - this.logger.debug(JSON.stringify(data)) - - if (data.result) { - data.result.forEach((e) => { - emit(processingResult.inputError(location, e.message.toString())) - }) - - isGoodEntity = false - } }) - if (isGoodEntity) { - return entity; - } - return merge( { metadata: { annotations: { - [OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION]: "false" + [OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION]: opaResult.good_entity.toString() } } },