Skip to content

Commit

Permalink
fix: refactor accentuate backend to use REST API instead of database
Browse files Browse the repository at this point in the history
  • Loading branch information
dweber019 committed Aug 29, 2024
1 parent 2409aaa commit a0c672a
Show file tree
Hide file tree
Showing 22 changed files with 339 additions and 433 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-beds-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dweber019/backstage-plugin-accentuate-backend': patch
---

Refactor accentuate backend to use REST API instead of database.
16 changes: 16 additions & 0 deletions .changeset/fast-trees-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@dweber019/backstage-plugin-api-docs-module-wsdl-backend': patch
'@dweber019/backstage-plugin-api-docs-spectral-linter': patch
'@dweber019/backstage-plugin-missing-entity-backend': patch
'@dweber019/backstage-plugin-api-docs-module-wsdl': patch
'@dweber019/backstage-plugin-accentuate-backend': patch
'@dweber019/backstage-plugin-endoflife-backend': patch
'@dweber019/backstage-plugin-relations-backend': patch
'@dweber019/backstage-plugin-simple-icons': patch
'@dweber019/backstage-plugin-accentuate': patch
'@dweber019/backstage-plugin-endoflife': patch
'@dweber019/backstage-plugin-relations': patch
'@dweber019/backstage-plugin-tips': patch
---

Update some dependencies.
4 changes: 2 additions & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
"devDependencies": {
"@backstage/test-utils": "^1.5.10",
"@playwright/test": "^1.32.3",
"@testing-library/dom": "^9.0.0",
"@testing-library/dom": "^10.0.0",
"@testing-library/jest-dom": "^6.0.0",
"@testing-library/react": "^14.0.0",
"@testing-library/react": "^15.0.0",
"@testing-library/user-event": "^14.0.0",
"@types/react-dom": "*",
"cross-env": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@dweber019/backstage-plugin-relations-backend": "0.0.8",
"app": "link:../app",
"better-sqlite3": "^9.0.0",
"express": "^4.17.1",
"express": "^4.18.1",
"express-promise-router": "^4.1.0",
"node-gyp": "^10.0.0",
"pg": "^8.11.3",
Expand Down
10 changes: 6 additions & 4 deletions plugins/accentuate-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@backstage/backend-common": "^0.24.0",
"@backstage/backend-defaults": "^0.4.3",
"@backstage/backend-plugin-api": "^0.8.0",
"@backstage/catalog-model": "^1.6.0",
Expand All @@ -46,7 +45,7 @@
"@dweber019/backstage-plugin-accentuate-common": "^0.0.7",
"@types/express": "^4.17.6",
"deepmerge": "^4.3.1",
"express": "^4.17.1",
"express": "^4.18.1",
"express-promise-router": "^4.1.0",
"knex": "^3.1.0",
"luxon": "^3.0.0",
Expand All @@ -55,9 +54,12 @@
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/backend-test-utils": "^0.5.1",
"@backstage/cli": "^0.27.0",
"@types/supertest": "^2.0.8",
"supertest": "^6.1.3"
"@backstage/test-utils": "^1.5.10",
"@types/supertest": "^6.0.0",
"msw": "^1.0.0",
"supertest": "^7.0.0"
},
"files": [
"dist",
Expand Down
10 changes: 5 additions & 5 deletions plugins/accentuate-backend/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { createRouterFromConfig } from './service/router';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';
import { AccentuateEntitiesProcessor } from './processor';
import { DatabaseManager } from '@backstage/backend-common';

/**
* @public
Expand Down Expand Up @@ -49,15 +48,16 @@ export const catalogModuleAccentuateProcessor = createBackendModule({
catalog: catalogProcessingExtensionPoint,
logger: coreServices.logger,
config: coreServices.rootConfig,
discovery: coreServices.discovery,
auth: coreServices.auth,
},
async init({ catalog, logger, config }) {
const databaseManager = DatabaseManager.fromConfig(config);
const databaseService = databaseManager.forPlugin('accentuate');
async init({ catalog, logger, config, discovery, auth }) {
catalog.addProcessor(
await AccentuateEntitiesProcessor.fromEnv({
logger,
config,
database: databaseService,
discovery,
auth
}),
);
},
Expand Down
84 changes: 54 additions & 30 deletions plugins/accentuate-backend/src/processor/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AccentuateEntitiesProcessor } from './processor';
import { ConfigReader } from '@backstage/config';
import { getVoidLogger } from '@backstage/backend-common';
import { AccentuateBackendClient } from '../api';
import { Entity } from '@backstage/catalog-model';
import { ANNOTATION_ACCENTUATE_DISABLE } from '@dweber019/backstage-plugin-accentuate-common';
import { mockCredentials, mockServices } from '@backstage/backend-test-utils';
import { registerMswTestHooks } from '@backstage/test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';

const logger = getVoidLogger();
const logger = mockServices.logger.mock();
const config = new ConfigReader({
accentuate: {
allowedKinds: [
Expand All @@ -20,21 +22,33 @@ const config = new ConfigReader({
});

describe('processor', () => {
const server = setupServer();
registerMswTestHooks(server);

const discovery = mockServices.discovery.mock({
getBaseUrl: jest.fn().mockResolvedValue('http://example.com'),
});
const auth = mockServices.auth();

test('should merge entity with stored data', async () => {
function mockAccentuateBackendClient(): jest.Mocked<AccentuateBackendClient> {
const mock = {
get: jest
.fn()
.mockReturnValue({
data: { spec: { owner: 'group:default/group-1' } },
server.use(
rest.get('http://example.com', async (req, res, ctx) => {
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'accentuate',
}),
};
return mock as unknown as jest.Mocked<AccentuateBackendClient>;
}
);
return res(ctx.json({
data: { spec: { owner: 'group:default/group-1' } },
}));
}),
);
const processor = new AccentuateEntitiesProcessor({
logger,
config,
accentuateBackendClient: mockAccentuateBackendClient(),
discovery,
auth,
});

const entity = {
Expand All @@ -59,20 +73,24 @@ describe('processor', () => {
});

test('should not merge entity if not allowed kind', async () => {
function mockAccentuateBackendClient(): jest.Mocked<AccentuateBackendClient> {
const mock = {
get: jest
.fn()
.mockReturnValue({
data: { spec: { owner: 'group:default/group-1' } },
server.use(
rest.get('http://example.com', async (req, res, ctx) => {
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'accentuate',
}),
};
return mock as unknown as jest.Mocked<AccentuateBackendClient>;
}
);
return res(ctx.json({
data: { spec: { owner: 'group:default/group-1' } },
}));
}),
);
const processor = new AccentuateEntitiesProcessor({
logger,
config,
accentuateBackendClient: mockAccentuateBackendClient(),
discovery,
auth,
});

const entity = {
Expand All @@ -97,16 +115,22 @@ describe('processor', () => {
});

test('should not merge entity if nothing found', async () => {
function mockAccentuateBackendClient(): jest.Mocked<AccentuateBackendClient> {
const mock = {
get: jest.fn().mockReturnValue(undefined),
};
return mock as unknown as jest.Mocked<AccentuateBackendClient>;
}
server.use(
rest.get('http://example.com', async (req, res, ctx) => {
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'accentuate',
}),
);
return res(ctx.status(404));
}),
);
const processor = new AccentuateEntitiesProcessor({
logger,
config,
accentuateBackendClient: mockAccentuateBackendClient(),
discovery,
auth,
});

const entity = {
Expand Down
55 changes: 36 additions & 19 deletions plugins/accentuate-backend/src/processor/processor.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { CatalogProcessor } from '@backstage/plugin-catalog-node';
import { Entity, stringifyEntityRef } from '@backstage/catalog-model';
import { AccentuateBackendDatabase } from '../db';
import { AccentuateBackendClient } from '../api';
import deepmerge from 'deepmerge';
import {
ANNOTATION_ACCENTUATE_DISABLE,
DEFAULT_ALLOWED_KINDS,
isAllowedKind,
} from '@dweber019/backstage-plugin-accentuate-common';
import { Config } from '@backstage/config';
import { DatabaseService, LoggerService } from '@backstage/backend-plugin-api';
import { AuthService, DiscoveryService, LoggerService } from '@backstage/backend-plugin-api';
import { AccentuateResponse } from '../api/AccentuateBackendClient';

export type PluginEnvironment = {
logger: LoggerService;
config: Config;
database: DatabaseService;
discovery: DiscoveryService;
auth: AuthService;
};

export class AccentuateEntitiesProcessor implements CatalogProcessor {
private readonly logger: LoggerService;
private readonly config: Config;
private readonly accentuateBackendClient: AccentuateBackendClient;
private readonly discovery: DiscoveryService;
private readonly auth: AuthService;

constructor(options: {
logger: LoggerService;
config: Config;
accentuateBackendClient: AccentuateBackendClient;
discovery: DiscoveryService;
auth: AuthService;
}) {
this.logger = options.logger.child({
type: 'processor',
processor: this.getProcessorName(),
});
this.config = options.config;
this.accentuateBackendClient = options.accentuateBackendClient;
this.discovery = options.discovery;
this.auth = options.auth;
}

getProcessorName(): string {
Expand All @@ -53,10 +56,30 @@ export class AccentuateEntitiesProcessor implements CatalogProcessor {
}) as any;
}

const accentuate = await this.accentuateBackendClient.get(
stringifyEntityRef(entity),
);
this.logger.debug('Does accentuate for entity exists', {
const baseUrl = await this.discovery.getBaseUrl('accentuate');
const { token } = await this.auth.getPluginRequestToken({
onBehalfOf: await this.auth.getOwnServiceCredentials(),
targetPluginId: 'accentuate',
});

const response = await fetch(`${baseUrl}?entityRef=${stringifyEntityRef(entity)}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
});
if (response.status === 404) {
return entity;
}
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}

const accentuate = await response.json() as AccentuateResponse;

this.logger.info('Does accentuate for entity exists', {
entity: stringifyEntityRef(entity),
exists: !!accentuate,
});
Expand All @@ -68,17 +91,11 @@ export class AccentuateEntitiesProcessor implements CatalogProcessor {
}

public static async fromEnv(env: PluginEnvironment) {
const accentuateBackendStore = await AccentuateBackendDatabase.create(
await env.database.getClient(),
);
const accentuateBackendClient = new AccentuateBackendClient(
env.logger,
accentuateBackendStore,
);
return new AccentuateEntitiesProcessor({
logger: env.logger,
config: env.config,
accentuateBackendClient,
discovery: env.discovery,
auth: env.auth,
});
}
}
17 changes: 0 additions & 17 deletions plugins/accentuate-backend/src/run.ts

This file was deleted.

Loading

0 comments on commit a0c672a

Please sign in to comment.