import * as plugins from '../plugins.js'; import { Cloudly } from '../classes.cloudly.js'; import { ExternalRegistry } from './classes.externalregistry.js'; export class ExternalRegistryManager { public cloudlyRef: Cloudly; public typedrouter = new plugins.typedrequest.TypedRouter(); public CExternalRegistry = plugins.smartdata.setDefaultManagerForDoc(this, ExternalRegistry); get db() { return this.cloudlyRef.mongodbConnector.smartdataDb; } constructor(cloudlyRef: Cloudly) { this.cloudlyRef = cloudlyRef; // Add typedrouter to cloudly's main router this.cloudlyRef.typedrouter.addTypedRouter(this.typedrouter); // Get registry by ID this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('getExternalRegistryById', async (dataArg) => { await plugins.smartguard.passGuardsOrReject(dataArg, [ this.cloudlyRef.authManager.validIdentityGuard, ]); const registry = await this.CExternalRegistry.getRegistryById(dataArg.id); if (!registry) { throw new Error(`Registry with id ${dataArg.id} not found`); } return { registry: await registry.createSavableObject(), }; }) ); // Get all registries this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('getExternalRegistries', async (dataArg) => { await plugins.smartguard.passGuardsOrReject(dataArg, [ this.cloudlyRef.authManager.validIdentityGuard, ]); const registries = await this.CExternalRegistry.getRegistries(); return { registries: await Promise.all( registries.map((registry) => registry.createSavableObject()) ), }; }) ); // Create registry this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('createExternalRegistry', async (dataArg) => { await plugins.smartguard.passGuardsOrReject(dataArg, [ this.cloudlyRef.authManager.validIdentityGuard, ]); const registry = await this.CExternalRegistry.createExternalRegistry(dataArg.registryData); return { registry: await registry.createSavableObject(), }; }) ); // Update registry this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('updateExternalRegistry', async (dataArg) => { await plugins.smartguard.passGuardsOrReject(dataArg, [ this.cloudlyRef.authManager.validIdentityGuard, ]); const registry = await this.CExternalRegistry.updateExternalRegistry( dataArg.registryId, dataArg.registryData ); return { resultRegistry: await registry.createSavableObject(), }; }) ); // Delete registry this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('deleteExternalRegistryById', async (dataArg) => { await plugins.smartguard.passGuardsOrReject(dataArg, [ this.cloudlyRef.authManager.validIdentityGuard, ]); const success = await this.CExternalRegistry.deleteExternalRegistry(dataArg.registryId); return { ok: success, }; }) ); // Verify registry connection this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('verifyExternalRegistry', async (dataArg) => { await plugins.smartguard.passGuardsOrReject(dataArg, [ this.cloudlyRef.authManager.validIdentityGuard, ]); const registry = await this.CExternalRegistry.getRegistryById(dataArg.registryId); if (!registry) { return { success: false, message: `Registry with id ${dataArg.registryId} not found`, }; } const result = await registry.verifyConnection(); return { success: result.success, message: result.message, registry: await registry.createSavableObject(), }; }) ); } public async start() { console.log('External Registry Manager started'); } public async stop() { console.log('External Registry Manager stopped'); } }