2024-04-20 10:21:41 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
|
2024-06-05 12:13:03 +00:00
|
|
|
export type TClientType = 'api' | 'ci' | 'coreflow' | 'cli' | 'serverconfig';
|
2024-05-28 16:45:34 +00:00
|
|
|
|
2024-06-02 19:39:31 +00:00
|
|
|
import { Image } from './classes.image.js';
|
|
|
|
|
2024-06-05 12:13:03 +00:00
|
|
|
export class CloudlyApiClient {
|
2024-05-28 16:45:34 +00:00
|
|
|
private cloudlyUrl: string;
|
|
|
|
private registerAs: string;
|
|
|
|
|
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
|
|
|
public typedsocketClient: plugins.typedsocket.TypedSocket;
|
|
|
|
|
|
|
|
// Subjects
|
|
|
|
public configUpdateSubject = new plugins.smartrx.rxjs.Subject<
|
|
|
|
plugins.servezoneInterfaces.requests.config.IRequest_Cloudly_Coreflow_PushClusterConfig['request']
|
|
|
|
>();
|
|
|
|
|
|
|
|
public serverActionSubject = new plugins.smartrx.rxjs.Subject<
|
|
|
|
plugins.servezoneInterfaces.requests.server.IRequest_TriggerServerAction['request']
|
|
|
|
>();
|
|
|
|
|
2024-11-18 16:48:26 +00:00
|
|
|
constructor(optionsArg: {
|
2024-06-05 12:13:03 +00:00
|
|
|
registerAs: TClientType;
|
|
|
|
cloudlyUrl?: string;
|
|
|
|
}) {
|
|
|
|
this.registerAs = optionsArg.registerAs;
|
|
|
|
this.cloudlyUrl =
|
|
|
|
optionsArg?.cloudlyUrl || process.env.CLOUDLY_URL || 'https://cloudly.layer.io:443';
|
2024-05-28 16:45:34 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
`creating LoleCloudlyClient: registering as ${this.registerAs} and target url ${this.cloudlyUrl}`
|
|
|
|
);
|
|
|
|
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.config.IRequest_Cloudly_Coreflow_PushClusterConfig>(
|
|
|
|
new plugins.typedrequest.TypedHandler('pushClusterConfig', async (dataArg) => {
|
|
|
|
this.configUpdateSubject.next(dataArg);
|
|
|
|
return {};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.server.IRequest_TriggerServerAction>(
|
|
|
|
new plugins.typedrequest.TypedHandler('triggerServerAction', async (dataArg) => {
|
|
|
|
this.serverActionSubject.next(dataArg);
|
|
|
|
return {
|
|
|
|
actionConfirmed: true,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start() {
|
|
|
|
this.typedsocketClient = await plugins.typedsocket.TypedSocket.createClient(
|
|
|
|
this.typedrouter,
|
|
|
|
this.cloudlyUrl
|
|
|
|
);
|
2024-06-05 12:13:03 +00:00
|
|
|
console.log(
|
2024-10-16 12:35:38 +00:00
|
|
|
`CloudlyClient connected to cloudly at ${this.cloudlyUrl}. Remember to get an identity.`
|
2024-06-05 12:13:03 +00:00
|
|
|
);
|
2024-05-28 16:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async stop() {
|
|
|
|
await this.typedsocketClient.stop();
|
|
|
|
}
|
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
public identity: plugins.servezoneInterfaces.data.IIdentity;
|
2024-10-16 12:35:38 +00:00
|
|
|
public async getIdentityByToken(
|
|
|
|
token: string,
|
2024-08-25 12:29:26 +00:00
|
|
|
optionsArg?: {
|
|
|
|
tagConnection?: boolean;
|
|
|
|
statefullIdentity?: boolean;
|
|
|
|
}
|
|
|
|
): Promise<plugins.servezoneInterfaces.data.IIdentity> {
|
|
|
|
optionsArg = Object.assign({}, {
|
|
|
|
tagConnection: false,
|
|
|
|
statefullIdentity: true,
|
|
|
|
}, optionsArg);
|
|
|
|
|
2024-05-28 16:45:34 +00:00
|
|
|
const identityRequest =
|
2024-10-16 12:35:38 +00:00
|
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.identity.IRequest_Any_Cloudly_CoreflowManager_GetIdentityByToken>(
|
|
|
|
'getIdentityByToken'
|
2024-05-28 16:45:34 +00:00
|
|
|
);
|
2024-10-16 12:35:38 +00:00
|
|
|
console.log(`trying to get identity from cloudly with supplied jumpCodeArg: ${token}`);
|
2024-05-28 16:45:34 +00:00
|
|
|
const response = await identityRequest.fire({
|
2024-10-16 12:35:38 +00:00
|
|
|
token: token,
|
2024-05-28 16:45:34 +00:00
|
|
|
});
|
|
|
|
console.log('got identity response');
|
2024-08-25 12:29:26 +00:00
|
|
|
const identity = response.identity;
|
2024-05-28 16:45:34 +00:00
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
if (optionsArg.tagConnection) {
|
2024-05-28 16:45:34 +00:00
|
|
|
this.typedsocketClient.addTag('identity', identity);
|
|
|
|
}
|
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
if (optionsArg.statefullIdentity) {
|
2024-06-02 19:39:31 +00:00
|
|
|
this.identity = identity;
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:45:34 +00:00
|
|
|
return identity;
|
|
|
|
}
|
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
/**
|
|
|
|
* will use statefull identity by default
|
|
|
|
*/
|
2024-05-28 16:45:34 +00:00
|
|
|
public async getClusterConfigFromCloudlyByIdentity(
|
2024-08-25 12:29:26 +00:00
|
|
|
identityArg: plugins.servezoneInterfaces.data.IIdentity = this.identity
|
2024-05-28 16:45:34 +00:00
|
|
|
): Promise<plugins.servezoneInterfaces.data.ICluster> {
|
|
|
|
const clusterConfigRequest =
|
|
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetClusterConfig>(
|
|
|
|
'getClusterConfig'
|
|
|
|
);
|
|
|
|
const response = await clusterConfigRequest.fire({
|
2024-08-25 12:29:26 +00:00
|
|
|
identity: identityArg,
|
2024-05-28 16:45:34 +00:00
|
|
|
});
|
|
|
|
return response.configData;
|
|
|
|
}
|
2024-04-20 10:21:41 +00:00
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
/**
|
|
|
|
* will use statefull identity by default
|
|
|
|
*/
|
2024-05-28 16:45:34 +00:00
|
|
|
public async getServerConfigFromCloudlyByIdentity(
|
2024-08-25 12:29:26 +00:00
|
|
|
identityArg: plugins.servezoneInterfaces.data.IIdentity = this.identity
|
2024-05-28 16:45:34 +00:00
|
|
|
): Promise<plugins.servezoneInterfaces.data.IServer> {
|
|
|
|
const serverConfigRequest =
|
|
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetServerConfig>(
|
|
|
|
'getServerConfig'
|
|
|
|
);
|
|
|
|
const response = await serverConfigRequest.fire({
|
2024-08-25 12:29:26 +00:00
|
|
|
identity: identityArg,
|
2024-06-05 12:13:03 +00:00
|
|
|
serverId: '', // TODO: get server id here
|
2024-05-28 16:45:34 +00:00
|
|
|
});
|
|
|
|
return response.configData;
|
2024-04-20 10:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 16:45:34 +00:00
|
|
|
/**
|
|
|
|
* gets a certificate for a domain used by a service
|
|
|
|
*/
|
2024-08-25 12:29:26 +00:00
|
|
|
public async getCertificateForDomain(optionsArg: {
|
|
|
|
domainName: string;
|
|
|
|
type: plugins.servezoneInterfaces.requests.certificate.IRequest_Any_Cloudly_GetCertificateForDomain['request']['type'];
|
|
|
|
identity?: plugins.servezoneInterfaces.data.IIdentity;
|
|
|
|
}): Promise<plugins.tsclass.network.ICert> {
|
|
|
|
optionsArg.identity = optionsArg.identity || this.identity;
|
|
|
|
if (!optionsArg.identity) {
|
|
|
|
throw new Error('identity is required. Either provide one or login first.');
|
|
|
|
}
|
2024-05-28 16:45:34 +00:00
|
|
|
const typedCertificateRequest =
|
2024-08-25 12:29:26 +00:00
|
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.certificate.IRequest_Any_Cloudly_GetCertificateForDomain>(
|
|
|
|
'getCertificateForDomain'
|
2024-05-28 16:45:34 +00:00
|
|
|
);
|
|
|
|
const typedResponse = await typedCertificateRequest.fire({
|
2024-08-25 12:29:26 +00:00
|
|
|
identity: this.identity, // do proper auth here
|
|
|
|
domainName: optionsArg.domainName,
|
|
|
|
type: optionsArg.type,
|
2024-05-28 16:45:34 +00:00
|
|
|
});
|
|
|
|
return typedResponse.certificate;
|
2024-04-20 10:21:41 +00:00
|
|
|
}
|
2024-06-02 19:39:31 +00:00
|
|
|
|
2024-10-16 12:35:38 +00:00
|
|
|
public images = {
|
|
|
|
// Images
|
2024-11-18 18:52:15 +00:00
|
|
|
getImageById: async (imageIdArg: string) => {
|
|
|
|
return Image.getImageById(this, imageIdArg);
|
|
|
|
},
|
2024-10-16 12:35:38 +00:00
|
|
|
getImages: async () => {
|
|
|
|
return Image.getImages(this);
|
|
|
|
},
|
|
|
|
createImage: async (optionsArg: Parameters<typeof Image.createImage>[1]) => {
|
|
|
|
return Image.createImage(this, optionsArg);
|
|
|
|
}
|
2024-06-02 19:39:31 +00:00
|
|
|
}
|
2024-04-20 10:21:41 +00:00
|
|
|
}
|