feat: use CatalogEntityClient

This commit is contained in:
Marvin9
2020-10-13 19:14:49 +05:30
parent 53eb5e61e3
commit a24b657721
2 changed files with 23 additions and 3 deletions
+2 -1
View File
@@ -44,7 +44,8 @@
"nodegit": "0.27.0",
"uuid": "^8.2.0",
"winston": "^3.2.1",
"yaml": "^1.10.0"
"yaml": "^1.10.0",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@backstage/cli": "^0.1.1-alpha.25",
@@ -14,8 +14,12 @@
* limitations under the License.
*/
import {
loadBackendConfig,
SingleHostDiscovery,
} from '@backstage/backend-common';
import { JsonValue, ConfigReader } from '@backstage/config';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { JsonValue } from '@backstage/config';
import Docker from 'dockerode';
import express from 'express';
import Router from 'express-promise-router';
@@ -28,6 +32,7 @@ import {
TemplaterBuilder,
PublisherBuilder,
} from '../scaffolder';
import { CatalogEntityClient } from '../lib/catalog';
import { validate, ValidatorResult } from 'jsonschema';
export interface RouterOptions {
@@ -56,6 +61,10 @@ export async function createRouter(
const logger = parentLogger.child({ plugin: 'scaffolder' });
const jobProcessor = new JobProcessor();
const config = ConfigReader.fromConfigs(await loadBackendConfig());
const discovery = SingleHostDiscovery.fromConfig(config);
const entityClient = new CatalogEntityClient({ discovery });
router
.get('/v1/job/:jobId', ({ params }, res) => {
const job = jobProcessor.get(params.jobId);
@@ -81,14 +90,24 @@ export async function createRouter(
});
})
.post('/v1/jobs', async (req, res) => {
const template: TemplateEntityV1alpha1 = req.body.template;
const templateName: string = req.body.templateName;
const values: RequiredTemplateValues & Record<string, JsonValue> =
req.body.values;
let template: TemplateEntityV1alpha1;
try {
template = await entityClient.findTemplate(templateName);
} catch (e) {
// help me here
res.status(400).json({ errors: e });
return;
}
const validationResult: ValidatorResult = validate(
values,
template.spec.schema,
);
if (!validationResult.valid) {
res.status(400).json({ errors: validationResult.errors });
return;