All files / src/util installation.ts

17.14% Statements 6/35
0% Branches 0/7
4.35% Functions 1/23
17.14% Lines 6/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103    1x 1x   1x           5x 5x 5x                                                                                                                                                                                  
import { MessageConnection } from 'vscode-jsonrpc';
import { EventEmitter } from 'events';
import { Messages, Protocol } from 'raas-core';
import { Common, ErrorMessages } from './common';
 
export class Installation {
 
    private connection: MessageConnection;
    private emitter: EventEmitter;
 
    constructor(connection: MessageConnection, emitter: EventEmitter) {
        this.connection = connection;
        this.emitter = emitter;
        this.listenToInstallationChanges();
    }
 
    private listenToInstallationChanges() {
        this.connection.onNotification(Messages.Client.InstallationAddedNotification.type,
            installation => {
            this.emitter.emit('installationAdded', installation);
        });
        this.connection.onNotification(Messages.Client.InstallingServerNotification.type,
            installingServer => {
            this.emitter.emit('installingServer', installingServer);
        });
        this.connection.onNotification(Messages.Client.InstallingServerMessageNotification.type,
            data => {
            this.emitter.emit('installingServerMessage', data);
        });
        this.connection.onNotification(Messages.Client.InstallingServerFailedNotification.type,
            data => {
            this.emitter.emit('installingServerFailedMessage', data);
        });
        this.connection.onNotification(Messages.Client.InstallingServerCancelledNotification.type,
            installingServer => {
            this.emitter.emit('installingServerCancelled', installingServer);
        });
        this.connection.onNotification(Messages.Client.InstallingServerCompletedNotification.type,
            data => {
            this.emitter.emit('installingServerCompleted', data);
        });
        this.connection.onNotification(Messages.Client.InstalledServerDeletedNotification.type,
            insallation => {
            this.emitter.emit('installedServerDeleted', insallation);
        });
        this.connection.onNotification(Messages.Client.InstalledServerUpdatedNotification.type,
            installation => {
            this.emitter.emit('installedServerUpdated', installation);
        });
    }
 
    addInstalledServer(request: Protocol.Installation, timeout: number = 20000): Promise<Protocol.Status> {
        return this.send(Messages.Server.AddInstalledServerRequest.type, request,
            'installationAdded',
            (param: Protocol.Installation) => param.id === request.id,
            timeout, ErrorMessages.ADDINSTALLEDSERVER_TIMEOUT);
    }
 
    installServer(request: Protocol.InstallServerRequest, timeout: number = 60000): Promise<Protocol.Status> {
        return this.send(Messages.Server.InstallServerRequest.type, request,
            'installingServerCompleted',
            (param: {installing: Protocol.InstallingServer, installation: Protocol.Installation}) => param.installing.installServerRequest.id === request.id,
            timeout, ErrorMessages.INSTALLSERVER_TIMEOUT);
    }
 
    cancelInstallation(installing: Protocol.InstallingServer, timeout: number = 2000): Promise<Protocol.Status> {
        return this.send(Messages.Server.CancelServerInstallationRequest.type, installing,
            'installingServerCancelled',
            (param: Protocol.InstallingServer) => param.installServerRequest.id === installing.id,
            timeout, ErrorMessages.CANCELINSTALLION_TIMEOUT);
    }
 
    deleteInstallation(installation: Protocol.Installation, timeout: number = 2000): Promise<Protocol.Status> {
        return this.send(Messages.Server.DeleteServerInstallationRequest.type, installation,
            'installedServerDeleted',
            (param: Protocol.Installation) => param.id === installation.id,
            timeout, ErrorMessages.DELETEINSTALLION_TIMEOUT);
    }
 
    updateInstallation(installation: Protocol.Installation, timeout: number = 2000): Promise<Protocol.Status> {
        return this.send(Messages.Server.UpdateServerInstallationRequest.type, installation,
            'installedServerUpdated',
            (param: Protocol.Installation) => param.id === installation.id,
            timeout, ErrorMessages.UPDATEINSTALLATION_TIMEOUT);
    }
 
    getAllInstalledServers(timeout: number = 2000): Promise<Protocol.Installation[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.GetInstalledServersRequest.type, null,
            timeout, ErrorMessages.GETALLINSTALLEDSERVERS_TIMEOUT);
    }
 
    getAllInstallingServers(timeout: number = 2000): Promise<Protocol.InstallingServer[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.GetInstallingServersRequest.type, null,
            timeout, ErrorMessages.GETALLINSTALLINGSERVERS_TIMEOUT);
    }
 
    private send(messageType: any, payload: any, id: string,
        listener: (params: any) => boolean, timeout: number,
        timeoutMessage: string): Promise<Protocol.Status> {
        return Common.sendRequestSync(this.connection, messageType,
            payload, this.emitter, id, listener, timeout, timeoutMessage);
    }
}