UNPKG

@zowe/cli

Version:

Zowe CLI is a command line interface (CLI) that provides a simple and streamlined way to interact with IBM z/OS.

100 lines 4.99 kB
"use strict"; /* * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * * Copyright Contributors to the Zowe Project. * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const imperative_1 = require("@zowe/imperative"); const path_1 = require("path"); const util = require("util"); const rest_1 = require("../../../../../rest"); const utils_1 = require("../../../../../utils"); const ZosFiles_constants_1 = require("../../constants/ZosFiles.constants"); const ZosFiles_messages_1 = require("../../constants/ZosFiles.messages"); /** * This class holds helper functions that are used to execute AMS control statements through the z/OS MF APIs */ class Invoke { /** * Send the AMS request to z/OS MF * * @param {AbstractSession} session - z/OS MF connection info * @param {string | string[]} controlStatements - contains the statements or the file path to them * * @returns {Promise<IZosFilesResponse>} A response indicating the outcome of the API * * @throws {ImperativeError} controlStatements must be set * @throws {Error} When the {@link ZosmfRestClient} throws an error * * @see https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.izua700/IZUHPINFO_API_PUTIDCAMSAccessMethodsServices.htm */ static ams(session, controlStatements) { return __awaiter(this, void 0, void 0, function* () { // required imperative_1.ImperativeExpect.toNotBeNullOrUndefined(controlStatements, ZosFiles_messages_1.ZosFilesMessages.missingStatements.message); imperative_1.ImperativeExpect.toNotBeEqual(controlStatements.length, 0, ZosFiles_messages_1.ZosFilesMessages.missingStatements.message); let statements; if (typeof controlStatements === "string") { // We got a file path try { statements = fs.readFileSync(controlStatements).toString().toUpperCase().split(/\r?\n/); } catch (error) { imperative_1.Logger.getAppLogger().error(error); throw error; } } else { statements = controlStatements.map((stmt) => stmt.toUpperCase()); } const longStmtIndex = statements.findIndex((stmt) => stmt.length > ZosFiles_constants_1.ZosFilesConstants.MAX_AMS_LINE); if (longStmtIndex >= 0) { throw new imperative_1.ImperativeError({ msg: util.format(ZosFiles_messages_1.ZosFilesMessages.longAmsStatement.message, longStmtIndex + 1, ZosFiles_constants_1.ZosFilesConstants.MAX_AMS_LINE, utils_1.getErrorContext(statements, longStmtIndex)) }); } try { // Format the endpoint to send the request to const endpoint = path_1.posix.join(ZosFiles_constants_1.ZosFilesConstants.RESOURCE, ZosFiles_constants_1.ZosFilesConstants.RES_AMS); imperative_1.Logger.getAppLogger().debug(`Endpoint: ${endpoint}`); // The request payload const reqPayload = { input: statements }; // The request headers const reqHeaders = [ imperative_1.Headers.APPLICATION_JSON, { [imperative_1.Headers.CONTENT_LENGTH]: JSON.stringify(reqPayload).length.toString() } ]; const response = yield rest_1.ZosmfRestClient.putExpectJSON(session, endpoint, reqHeaders, reqPayload); return { success: true, commandResponse: ZosFiles_messages_1.ZosFilesMessages.amsCommandExecutedSuccessfully.message, apiResponse: response }; } catch (error) { imperative_1.Logger.getAppLogger().error(error); throw error; } }); } } exports.Invoke = Invoke; //# sourceMappingURL=Invoke.js.map