Files
coreflow/ts/coreflow.connector.cloudlyconnector.ts
T

67 lines
2.4 KiB
TypeScript

// This file contains logic to connect to a servezone cloudly instance
// The connection is done using websockets to have an always open channel
// that is bidirectional
import * as plugins from './coreflow.plugins.js';
import { Coreflow } from './coreflow.classes.coreflow.js';
export class CloudlyConnector {
public coreflowRef: Coreflow;
public cloudlyApiClient!: plugins.servezoneApi.CloudlyApiClient;
public coreflowJumpCode!: string;
public identity!: plugins.servezoneInterfaces.data.IIdentity;
constructor(coreflowRefArg: Coreflow) {
this.coreflowRef = coreflowRefArg;
}
public async start() {
const cloudlyUrl = await this.coreflowRef.serviceQenv.getEnvVarOnDemand('CLOUDLY_URL');
if (!cloudlyUrl) {
throw new Error('Missing required CLOUDLY_URL environment variable');
}
this.cloudlyApiClient = new plugins.servezoneApi.CloudlyApiClient({
registerAs: 'coreflow',
cloudlyUrl,
});
await this.cloudlyApiClient.start();
const coreflowJumpCode = await this.coreflowRef.serviceQenv.getEnvVarOnDemand('JUMPCODE');
if (!coreflowJumpCode) {
throw new Error('Missing required JUMPCODE environment variable');
}
this.coreflowJumpCode = coreflowJumpCode;
// get identity and tag connection (second parameter is true -> tags the connection)
this.identity = await this.cloudlyApiClient.getIdentityByToken(this.coreflowJumpCode, {
statefullIdentity: true,
tagConnection: true,
});
}
public async stop() {
await this.cloudlyApiClient.stop();
}
public async getConfigFromCloudly(): Promise<plugins.servezoneInterfaces.data.ICluster> {
const config = await this.getClusterConfigPayloadFromCloudly();
return config.configData as unknown as plugins.servezoneInterfaces.data.ICluster;
}
public async getClusterConfigPayloadFromCloudly(): Promise<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetClusterConfig['response'] & {
externalGateway?: import('./coreflow.connector.externalgateway.js').IExternalGatewayConfig;
}> {
return await this.cloudlyApiClient.getClusterConfigFromCloudlyByIdentity(this.identity) as any;
}
public async getCertificateForDomainFromCloudly(
domainNameArg: string,
): Promise<plugins.tsclass.network.ICert> {
const certificate = await this.cloudlyApiClient.getCertificateForDomain({
identity: this.identity,
domainName: domainNameArg,
type: 'ssl',
});
return certificate;
}
}