Add configLocationProvider and provider identifiers

Co-authored-by: Fredrik Adelöw <freben@users.noreply.github.com>
Co-authored-by: Ben Lambert <ben@blam.sh>
Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-04-26 15:43:22 +02:00
committed by blam
parent c58f8fd147
commit aad89f5546
5 changed files with 64 additions and 6 deletions
@@ -0,0 +1,49 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EntityProviderConnection, EntityProvider } from './types';
import path from 'path';
import { Config } from '@backstage/config';
import { locationSpecToLocationEntity } from './util';
export class ConfigLocationProvider implements EntityProvider {
private connection: EntityProviderConnection | undefined;
constructor(private readonly config: Config) {}
getProviderName(): string {
return 'ConfigLocationProvider';
}
async connect(connection: EntityProviderConnection): Promise<void> {
this.connection = connection;
const locationConfigs =
this.config.getOptionalConfigArray('catalog.locations') ?? [];
const entities = locationConfigs.map(location =>
locationSpecToLocationEntity({
type: location.getString('type'),
target: path.resolve(location.getString('target')),
}),
);
await this.connection.applyMutation({
type: 'full',
entities,
});
}
}
@@ -68,9 +68,12 @@ export class DefaultCatalogProcessingEngine implements CatalogProcessingEngine {
async start() {
for (const provider of this.entityProviders) {
// TODO: this ID should be some form of identifier for the EntityProvider
const id = 'databaseProvider';
provider.connect(new Connection({ stateManager: this.stateManager, id }));
provider.connect(
new Connection({
stateManager: this.stateManager,
id: provider.getProviderName(),
}),
);
}
this.running = true;
@@ -30,6 +30,10 @@ export class DefaultLocationStore implements LocationStore, EntityProvider {
constructor(private readonly db: Database) {}
getProviderName(): string {
return 'DefaultLocationStore';
}
createLocation(spec: LocationSpec): Promise<Location> {
return this.db.transaction(async tx => {
// TODO: id should really be type and target combined and not a uuid.
@@ -50,6 +50,7 @@ import {
GithubDiscoveryProcessor,
GithubOrgReaderProcessor,
LdapOrgReaderProcessor,
LocationEntityProcessor,
MicrosoftGraphOrgReaderProcessor,
PlaceholderProcessor,
PlaceholderResolver,
@@ -73,6 +74,7 @@ import { CatalogProcessingEngine } from '../next/types';
import { NextEntitiesCatalog } from './NextEntitiesCatalog';
import { Stitcher } from './Stitcher';
import { CommonDatabase } from '../database/CommonDatabase';
import { ConfigLocationProvider } from './ConfigLocationProvider';
export type CatalogEnvironment = {
logger: Logger;
@@ -272,9 +274,10 @@ export class NextCatalogBuilder {
const locationStore = new DefaultLocationStore(db);
const stitcher = new Stitcher(dbClient, logger);
const configLocationProvider = new ConfigLocationProvider(config);
const processingEngine = new DefaultCatalogProcessingEngine(
logger,
[locationStore], // entityproviders
[locationStore, configLocationProvider],
stateManager,
orchestrator,
stitcher,
@@ -322,7 +325,6 @@ export class NextCatalogBuilder {
// These are always there no matter what
const processors: CatalogProcessor[] = [
StaticLocationProcessor.fromConfig(config),
new PlaceholderProcessor({ resolvers: placeholderResolvers, reader }),
new BuiltinKindsEntityProcessor(),
];
@@ -338,7 +340,6 @@ export class NextCatalogBuilder {
MicrosoftGraphOrgReaderProcessor.fromConfig(config, { logger }),
new UrlReaderProcessor({ reader, logger }),
CodeOwnersProcessor.fromConfig(config, { logger, reader }),
// new LocationEntityProcessor({ integrations }),
new AnnotateLocationEntityProcessor({ integrations }),
);
}
@@ -65,6 +65,7 @@ export interface EntityProviderConnection {
}
export interface EntityProvider {
getProviderName(): string;
connect(connection: EntityProviderConnection): Promise<void>;
}