All files / src environmentVcapServices.ts

100% Statements 24/24
100% Branches 15/15
100% Functions 2/2
100% Lines 24/24

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  1x   1x 1x 1x   1x 1x               1x             12x 2x     10x 10x   10x 5x 5x     5x 1x         4x 1x         3x 1x         2x 2x 2x                   2x                  
// tslint:disable-next-line:no-var-requires
require("dotenv").config();
 
import ClientError from "./error";
import Server from "./server";
export { Server };
 
import Logger from "./logger";
const log: Logger = new Logger();
 
 
/**
 * returns the nextcloud credentials that is defined in the
 * "user-provided" service section of the VCAP_SERVICES environment
 * instanceName: the name of the nextcloud user provided service instance
 */
export default class EnvironmentVcapServices {
    public readonly url: string;
    public readonly userName: string;
    public readonly password: string;
 
    public constructor(instanceName: string) {
 
        if (!process.env.VCAP_SERVICES) {
            throw new ClientError("NCEnvironmentVcapServices: environment VCAP_SERVICES not found", "ERR_VCAP_SERVICES_NOT_FOUND");
        }
 
        const vcapServices = require("vcap_services");
        const cred = vcapServices.getCredentials("user-provided", undefined, instanceName);
 
        if (!cred || cred === undefined || (!cred.url && !cred.username && !cred.password)) {
            log.error("NCEnvironmentVcapServices: error credentials not found or not fully specified ", cred);
            throw new ClientError(`NCEnvironmentVcapServices: nextcloud credentials not found in environment VCAP_SERVICES. Service section: "user-provided", service instance name: "${instanceName}" `, "ERR_VCAP_SERVICES_NOT_FOUND");
        }
 
        if (!cred.url) {
            throw new ClientError("NCEnvironmentVcapServices: VCAP_SERVICES url not defined in user provided services for nextcloud"
                , "ERR_VCAP_SERVICES_URL_NOT_DEFINED",
                { credentials: cred });
        }
 
        if (!cred.password) {
            throw new ClientError("NCEnvironmentVcapServices: VCAP_SERVICES password not defined in user provided services for nextcloud",
                "ERR_VCAP_SERVICES_PASSWORD_NOT_DEFINED",
                { credentials: cred });
        }
 
        if (!cred.username) {
            throw new ClientError("NCEnvironmentVcapServices: VCAP_SERVICES username not defined in user provided services for nextcloud",
                "ERR_VCAP_SERVICES_USERNAME_NOT_DEFINED",
                { credentials: cred });
        }
 
        this.url = cred.url as string;
        this.userName = cred.username as string;
        this.password = cred.password as string;
    }
 
    /**
     * returns the nextcloud credentials that is defined in the
     * "user-provided" service section of the VCAP_SERVICES environment
     * @param instanceName the name of the nextcloud user provided service instance
     * @returns credentials from the VCAP_SERVICES environment (user provided service)
     */
    public getServer(): Server {
        return new Server({
            basicAuth: {
                password: this.password,
                username: this.userName,
            },
            url: this.url,
        });
    }
}