import * as plugins from '../plugins.js'; import { Cloudly } from '../classes.cloudly.js'; /** * in charge of talking to coreflow services on clusters * coreflow runs on a server when ServerManager is done. */ export class CloudlyCoreflowManager { public cloudlyRef: Cloudly; public typedRouter = new plugins.typedrequest.TypedRouter(); constructor(cloudlyRefArg: Cloudly) { this.cloudlyRef = cloudlyRefArg; this.cloudlyRef.typedrouter.addTypedRouter(this.typedRouter); this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler('getIdentityByJumpCode', async (requestData) => { const cluster = await this.cloudlyRef.clusterManager.getClusterBy_JumpCode( requestData.jumpCode ); if (!cluster) { throw new plugins.typedrequest.TypedResponseError('The supplied jumpCode is not valid. No cluster found.'); } const user = await this.cloudlyRef.authManager.CUser.getInstance({ id: cluster.data.userId, }); if (!user) { throw new plugins.typedrequest.TypedResponseError('The supplied jumpCode is not valid. No user found.'); } const expiryTimestamp = Date.now() + 3600 * 1000 * 24 * 365; return { identity: { name: cluster.data.name, role: 'cluster', type: 'machine', userId: cluster.data.userId, expiresAt: expiryTimestamp, clusterId: cluster.id, clusterName: cluster.data.name, jwt: await this.cloudlyRef.authManager.smartjwtInstance.createJWT({ status: 'loggedIn', userId: cluster.data.userId, expiresAt: expiryTimestamp, }) }, }; }) ); // lets enable the getting of cluster configs this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler( 'getClusterConfig', async (dataArg) => { const identity = dataArg.identity; console.log('trying to get clusterConfigSet'); console.log(dataArg); const cluster = await this.cloudlyRef.clusterManager.getClusterBy_Identity( identity ); console.log('got cluster config and sending it back to coreflow'); return { configData: await cluster.createSavableObject(), deploymentDirectives: [], }; } ) ); // lets enable getting of certificates this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler( 'getCertificateForDomain', async (dataArg) => { console.log(`incoming API request for certificate ${dataArg.domainName}`); const cert = await this.cloudlyRef.letsencryptConnector.getCertificateForDomain( dataArg.domainName ); console.log(`got certificate ready for reponse ${dataArg.domainName}`); return { certificate: await cert.createSavableObject(), }; } ) ); } }