Split dryRun to separate method, map errors.

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-05-10 14:32:18 +02:00
parent 6185b62e38
commit 60a6090f24
@@ -38,51 +38,8 @@ export class DefaultLocationService implements LocationService {
dryRun: boolean,
): Promise<{ location: Location; entities: Entity[] }> {
if (dryRun) {
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Location',
metadata: {
name: locationSpecToMetadataName({
type: spec.type,
target: spec.target,
}),
namespace: 'default',
annotations: {
[LOCATION_ANNOTATION]: `${spec.type}:${spec.target}`,
[ORIGIN_LOCATION_ANNOTATION]: `${spec.type}:${spec.target}`,
},
},
spec: {
type: spec.type,
target: spec.target,
},
};
const unprocessedEntities: Entity[] = [entity];
const entities: Entity[] = [];
const state = new Map(); // ignored
while (unprocessedEntities.length) {
const currentEntity = unprocessedEntities.pop();
if (!currentEntity) {
continue;
}
const processed = await this.orchestrator.process({
entity: currentEntity,
state,
});
if (processed.ok) {
unprocessedEntities.push(...processed.deferredEntities);
entities.push(processed.completedEntity);
} else {
throw Error(processed.errors.join(', '));
}
}
return {
location: { ...spec, id: `${spec.type}:${spec.target}` },
entities,
};
return this.dryRunCreateLocation(spec);
}
const location = await this.store.createLocation(spec);
return { location, entities: [] };
}
@@ -96,4 +53,53 @@ export class DefaultLocationService implements LocationService {
deleteLocation(id: string): Promise<void> {
return this.store.deleteLocation(id);
}
private async dryRunCreateLocation(
spec: LocationSpec,
): Promise<{ location: Location; entities: Entity[] }> {
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Location',
metadata: {
name: locationSpecToMetadataName({
type: spec.type,
target: spec.target,
}),
namespace: 'default',
annotations: {
[LOCATION_ANNOTATION]: `${spec.type}:${spec.target}`,
[ORIGIN_LOCATION_ANNOTATION]: `${spec.type}:${spec.target}`,
},
},
spec: {
type: spec.type,
target: spec.target,
},
};
const unprocessedEntities: Entity[] = [entity];
const entities: Entity[] = [];
const state = new Map(); // ignored
while (unprocessedEntities.length) {
const currentEntity = unprocessedEntities.pop();
if (!currentEntity) {
continue;
}
const processed = await this.orchestrator.process({
entity: currentEntity,
state,
});
if (processed.ok) {
unprocessedEntities.push(...processed.deferredEntities);
entities.push(processed.completedEntity);
} else {
throw Error(processed.errors.map(String).join(', '));
}
}
return {
location: { ...spec, id: `${spec.type}:${spec.target}` },
entities,
};
}
}