aruba-admin
Version:
A TypeScript/Node.js library for interacting with Aruba Cloud's SOAP API with full type support and multi-region connectivity.
147 lines • 7.1 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadWSDLs = exports.generateTypescriptApi = exports.downloadWSDL = void 0;
const xml_js_1 = __importDefault(require("xml-js"));
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const constants_1 = __importStar(require("../constants"));
/**
* Downloads the WSDL file for the specified data center and returns the path to it.
* First tries to use pre-packaged WSDL files, only downloads if not found.
* @param dc Data center object containing URL and name
* @return Promise<string> The path to the WSDL file
* @throws Error if the WSDL download fails or if there is an issue accessing the file
*/
const downloadWSDL = (dc) => __awaiter(void 0, void 0, void 0, function* () {
if (!dc || !dc.url || !dc.name) {
throw new Error("Invalid data center object. It must contain 'url' and 'name' properties.");
}
// Check if pre-packaged WSDL file exists
const prePackagedPath = path_1.default.join(constants_1.default.GENERATED_SOAP_API_DIR, `${dc.name}.wsdl`);
try {
yield promises_1.default.access(prePackagedPath);
// File exists, return the path
return prePackagedPath;
}
catch (error) {
// File doesn't exist, need to download it
console.log(`WSDL file for ${dc.name} not found locally, downloading...`);
}
// Download and process WSDL
const res = yield fetch(dc.url);
if (!res.ok) {
throw new Error(`Failed to download WSDL from ${dc.url}: ${res.statusText}`);
}
const wsdlContent = yield res.text();
// Update namespaces in WSDL
const jsonObj = xml_js_1.default.xml2js(wsdlContent, { compact: false });
const rootElement = jsonObj.elements[0]; // wsdl:definitions
const rootArttributes = rootElement.attributes;
for (const key in rootArttributes) {
if (rootArttributes[key] === constants_1.default.BROKEN_NAMESPACE) {
delete rootArttributes[key];
}
}
rootElement.elements = rootElement.elements.filter((el) => {
var _a;
// filter out wsdl:import elements with the broken namespace
return !(el.name === "wsdl:import" && ((_a = el === null || el === void 0 ? void 0 : el.attributes) === null || _a === void 0 ? void 0 : _a.namespace) === constants_1.default.BROKEN_NAMESPACE);
});
const newWsdlContent = xml_js_1.default.js2xml(jsonObj, { compact: false, spaces: 2 });
// Ensure directory exists before writing
try {
yield promises_1.default.mkdir(constants_1.default.GENERATED_SOAP_API_DIR, { recursive: true });
}
catch (error) {
// Directory might already exist, ignore error
}
yield promises_1.default.writeFile(prePackagedPath, newWsdlContent);
return prePackagedPath;
});
exports.downloadWSDL = downloadWSDL;
/**
* Generates TypeScript API from the WSDL file located in the generated SOAP API directory.
* It removes the existing generated SOAP API directory if it exists, creates a new one,
* downloads the WSDL, and then generates the TypeScript API.
* @returns {Promise<void>} A promise that resolves when the TypeScript API generation is complete.
* @throws {Error} If there is an error removing the directory or generating the API.
*/
const generateTypescriptApi = () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield promises_1.default.rm(constants_1.default.GENERATED_SOAP_API_DIR, { recursive: true });
}
catch (e) {
if (e.code !== 'ENOENT') {
console.error(`Error removing directory ${constants_1.default.GENERATED_SOAP_API_DIR}:`, e);
throw e;
}
}
yield promises_1.default.mkdir(constants_1.default.GENERATED_SOAP_API_DIR, { recursive: true });
yield (0, exports.downloadWSDL)(constants_1.DATA_CENTERS.DEFAULT);
const parseAndGenerate = require("wsdl-tsclient").parseAndGenerate;
// Generate TypeScript API from WSDL
yield parseAndGenerate(constants_1.default.WSDL_LOCATION, constants_1.default.GENERATED_SOAP_API_DIR, {
caseInsensitiveNames: true,
maxRecursiveDefinitionName: 1024
});
});
exports.generateTypescriptApi = generateTypescriptApi;
/**
* Downloads WSDL files for all specified data centers except the default one.
* @param dataCenters Array of DataCenter objects to download WSDLs for
* @returns {Promise<void>} A promise that resolves when all WSDLs are downloaded
*/
const downloadWSDLs = (dataCenters) => __awaiter(void 0, void 0, void 0, function* () {
console.log(`Downloading WSDLs for ${dataCenters.length} data centers...`);
for (const dc of dataCenters) {
yield (0, exports.downloadWSDL)(dc);
console.log(`WSDL for ${dc.name} downloaded successfully.`);
}
});
exports.downloadWSDLs = downloadWSDLs;
//# sourceMappingURL=Utils.js.map