scaffolder: Add registration step
Co-authored-by: Fredrik Adelöw <freben@gmail.com> Co-authored-by: blam<ben@blam.sh> Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -26,6 +26,7 @@ import {
|
||||
import { SingleHostDiscovery } from '@backstage/backend-common';
|
||||
import type { PluginEnvironment } from '../types';
|
||||
import Docker from 'dockerode';
|
||||
import { CatalogClient } from '@backstage/catalog-client';
|
||||
|
||||
export default async function createPlugin({
|
||||
logger,
|
||||
@@ -46,6 +47,7 @@ export default async function createPlugin({
|
||||
|
||||
const discovery = SingleHostDiscovery.fromConfig(config);
|
||||
const entityClient = new CatalogEntityClient({ discovery });
|
||||
const catalogClient = new CatalogClient({ discoveryApi: discovery });
|
||||
|
||||
return await createRouter({
|
||||
preparers,
|
||||
@@ -56,5 +58,6 @@ export default async function createPlugin({
|
||||
dockerClient,
|
||||
entityClient,
|
||||
database,
|
||||
catalogClient,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,19 +19,28 @@ import { FilePreparer, PreparerBuilder } from './prepare';
|
||||
import Docker from 'dockerode';
|
||||
import { TemplaterBuilder, TemplaterValues } from './templater';
|
||||
import { PublisherBuilder } from './publish';
|
||||
import { CatalogApi } from '@backstage/catalog-client';
|
||||
import { getEntityName } from '@backstage/catalog-model';
|
||||
|
||||
type Options = {
|
||||
dockerClient: Docker;
|
||||
preparers: PreparerBuilder;
|
||||
templaters: TemplaterBuilder;
|
||||
publishers: PublisherBuilder;
|
||||
catalogClient: CatalogApi;
|
||||
};
|
||||
|
||||
export function registerLegacyActions(
|
||||
registry: TemplateActionRegistry,
|
||||
options: Options,
|
||||
) {
|
||||
const { dockerClient, preparers, templaters, publishers } = options;
|
||||
const {
|
||||
dockerClient,
|
||||
preparers,
|
||||
templaters,
|
||||
publishers,
|
||||
catalogClient,
|
||||
} = options;
|
||||
|
||||
registry.register({
|
||||
id: 'legacy:prepare',
|
||||
@@ -107,4 +116,22 @@ export function registerLegacyActions(
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
registry.register({
|
||||
id: 'catalog:register',
|
||||
async handler(ctx) {
|
||||
const { logger } = ctx;
|
||||
const { catalogInfoUrl } = ctx.parameters; // TODO update schema
|
||||
|
||||
logger.info(`Registering ${catalogInfoUrl} in the catalog`);
|
||||
const result = await catalogClient.addLocation({
|
||||
type: 'url',
|
||||
target: catalogInfoUrl as string,
|
||||
});
|
||||
if (result.entities.length === 1) {
|
||||
const { kind, name, namespace } = getEntityName(result.entities[0]);
|
||||
ctx.output('entityRef', `${kind}:${namespace}/${name}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -76,6 +76,15 @@ export function templateEntityToSpec(
|
||||
},
|
||||
});
|
||||
|
||||
steps.push({
|
||||
id: 'register',
|
||||
name: 'Register',
|
||||
action: 'catalog:register',
|
||||
parameters: {
|
||||
catalogInfoUrl: '{{ steps.publish.output.catalogInfoUrl }}',
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
steps,
|
||||
output: {
|
||||
|
||||
@@ -48,6 +48,7 @@ import {
|
||||
NotFoundError,
|
||||
PluginDatabaseManager,
|
||||
} from '@backstage/backend-common';
|
||||
import { CatalogClient } from '@backstage/catalog-client';
|
||||
|
||||
export interface RouterOptions {
|
||||
preparers: PreparerBuilder;
|
||||
@@ -59,6 +60,7 @@ export interface RouterOptions {
|
||||
dockerClient: Docker;
|
||||
entityClient: CatalogEntityClient;
|
||||
database: PluginDatabaseManager;
|
||||
catalogClient: CatalogClient;
|
||||
}
|
||||
|
||||
export async function createRouter(
|
||||
@@ -76,6 +78,7 @@ export async function createRouter(
|
||||
dockerClient,
|
||||
entityClient,
|
||||
database,
|
||||
catalogClient,
|
||||
} = options;
|
||||
|
||||
const logger = parentLogger.child({ plugin: 'scaffolder' });
|
||||
@@ -99,6 +102,7 @@ export async function createRouter(
|
||||
preparers,
|
||||
publishers,
|
||||
templaters,
|
||||
catalogClient,
|
||||
});
|
||||
|
||||
worker.start();
|
||||
@@ -254,9 +258,7 @@ export async function createRouter(
|
||||
})
|
||||
.get('/v2/tasks/:taskId', async (req, res) => {
|
||||
const { taskId } = req.params;
|
||||
console.warn('getting task');
|
||||
const task = await taskBroker.get(taskId);
|
||||
console.warn('got task', task);
|
||||
if (!task) {
|
||||
throw new NotFoundError(`task with id ${taskId} does not exist`);
|
||||
}
|
||||
|
||||
@@ -30,9 +30,10 @@
|
||||
"clean": "backstage-cli clean"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/catalog-client": "^0.3.6",
|
||||
"@backstage/catalog-model": "^0.7.1",
|
||||
"@backstage/core": "^0.6.1",
|
||||
"@backstage/config": "^0.1.2",
|
||||
"@backstage/core": "^0.6.1",
|
||||
"@backstage/plugin-catalog-react": "^0.0.3",
|
||||
"@backstage/theme": "^0.2.3",
|
||||
"@material-ui/core": "^4.11.0",
|
||||
@@ -60,7 +61,6 @@
|
||||
"@backstage/cli": "^0.6.0",
|
||||
"@backstage/dev-utils": "^0.1.10",
|
||||
"@backstage/test-utils": "^0.1.7",
|
||||
"@backstage/catalog-client": "^0.3.6",
|
||||
"@testing-library/jest-dom": "^5.10.1",
|
||||
"@testing-library/react": "^10.4.1",
|
||||
"@testing-library/user-event": "^12.0.7",
|
||||
|
||||
Reference in New Issue
Block a user