@netzreich/openstack-clients
Version:
Openstack Client generated via OpenAPI Spec via openapi-generator
203 lines (202 loc) • 12.7 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Identity = exports.Network = exports.Compute = void 0;
var Compute = require("./compute");
exports.Compute = Compute;
var Network = require("./network");
exports.Network = Network;
var Identity = require("./identity");
exports.Identity = Identity;
var logger_1 = require("./logger");
function removeVersionFromPath(path) {
if (path === void 0) { path = ""; }
// Regex zum Entfernen der Version und des letzten Slashes
return path.replace(/\/v[\d.]+$/, "");
}
var OpenStack = /** @class */ (function () {
function OpenStack(configuration, _auth, logger) {
if (logger === void 0) { logger = new logger_1.Logger(); }
var _this = this;
this._auth = _auth;
this.logger = logger;
this.isAuthenticated = false;
this.firsTime = true;
this.Identity = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, Identity.AuthApiFactory(configuration)), Identity.RolesApiFactory(configuration)), Identity.RoleAssignmentsApiFactory(configuration)), Identity.UsersApiFactory(configuration)), Identity.ProjectsApiFactory(configuration)), Identity.DomainsApiFactory(configuration)), Identity.ServicesApiFactory(configuration)), Identity.EndpointsApiFactory(configuration)), Identity.GroupsApiFactory(configuration));
this.configuration = configuration;
this.Compute = __assign(__assign(__assign(__assign(__assign(__assign({}, Compute.ExtensionsApiFactory(configuration)), Compute.OsSimpleTenantUsageApiFactory(configuration)), Compute.ServersApiFactory(configuration)), Compute.FlavorsApiFactory(configuration)), Compute.ImagesApiFactory(configuration)), Compute.OsKeypairsApiFactory(configuration));
this.Network = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, Network.NetworksApiFactory(configuration)), Network.SubnetsApiFactory(configuration)), Network.PortsApiFactory(configuration)), Network.SecurityGroupsApiFactory(configuration)), Network.SecurityGroupRulesApiFactory(configuration)), Network.FloatingipPoolsApiFactory(configuration)), Network.QosApiFactory(configuration)), Network.RoutersApiFactory(configuration));
setInterval(function () {
if (_this.isTokenExpired()) {
_this.authenticate(_this._auth, false, 1000).catch(function (e) {
_this.logger.error(e.message);
});
}
}, 1000 * 10);
}
OpenStack.prototype._authenticate = function () {
return __awaiter(this, arguments, void 0, function (credentials, timeout) {
var authResponse, token, catalog;
var _a, _b, _c;
if (credentials === void 0) { credentials = this._auth; }
if (timeout === void 0) { timeout = 1000; }
return __generator(this, function (_d) {
switch (_d.label) {
case 0: return [4 /*yield*/, this.Identity.authTokensPost(credentials, {
timeout: timeout,
})];
case 1:
authResponse = _d.sent();
token = authResponse.headers["x-subject-token"];
this._token = token;
catalog = (_a = authResponse.data.token) === null || _a === void 0 ? void 0 : _a.catalog;
if (!catalog) {
this.logger.error("No catalog found in response");
throw new Error("No catalog found in response");
}
if ((_b = authResponse.data.token) === null || _b === void 0 ? void 0 : _b.expires_at) {
this.logger.log("New Token expires at: ", authResponse.data.token.expires_at);
this.expires_at = new Date(authResponse.data.token.expires_at);
}
if (((_c = authResponse.data.token) === null || _c === void 0 ? void 0 : _c.expires_at) === null) {
// null means token never expires
this.expires_at = new Date(new Date().setFullYear(new Date().getFullYear() + 100));
}
this._catalog = catalog;
this.initializeApis(catalog);
this.isAuthenticated = true;
return [2 /*return*/, authResponse];
}
});
});
};
OpenStack.prototype.isTokenExpired = function () {
if (this._token &&
this.expires_at &&
!(this.expires_at.getTime() < new Date().getTime())) {
return false;
}
return true;
};
OpenStack.prototype.authenticate = function () {
return __awaiter(this, arguments, void 0, function (credentials, retryOnError, retryInterval, timeout) {
var _this = this;
if (credentials === void 0) { credentials = this._auth; }
if (retryOnError === void 0) { retryOnError = true; }
if (retryInterval === void 0) { retryInterval = 60 * 1000; }
if (timeout === void 0) { timeout = 1000; }
return __generator(this, function (_a) {
try {
if (this.isTokenExpired()) {
this.logger.log("Token expired - authenticating");
return [2 /*return*/, this._authenticate(credentials, timeout)
.then(function (resp) { return resp.data; })
.catch(function (e) {
_this.logger.error(e.message);
return Promise.reject(e);
})];
}
return [2 /*return*/, void 0];
}
catch (e) {
this.isAuthenticated = false;
this._token = undefined;
this.expires_at = undefined;
this.logger.error("Failed to authenticate: ".concat(e.message));
if (retryOnError) {
this.logger.log("Retrying every 10s");
}
throw e;
}
return [2 /*return*/];
});
});
};
OpenStack.prototype.switchEndpointInterface = function (interfaceName) {
if (!this._catalog) {
this.logger.error("No catalog found in response");
throw new Error("No catalog found in response");
}
this.initializeApis(this._catalog, interfaceName);
};
OpenStack.prototype.initializeApis = function (catalog, entrypointInterface) {
if (entrypointInterface === void 0) { entrypointInterface = "public"; }
this.initializeApi("compute", catalog, entrypointInterface);
this.initializeApi("network", catalog, entrypointInterface);
this.initializeApi("identity", catalog, entrypointInterface);
};
OpenStack.prototype.initializeApi = function (type, catalog, entrypointInterface) {
if (entrypointInterface === void 0) { entrypointInterface = "public"; }
var service = catalog.find(function (entry) { return entry.type === type; });
if (!service || !service.endpoints) {
this.logger.error("No ".concat(type, " service found in catalog"));
throw new Error("No ".concat(type, " service found in catalog"));
}
var endpoint = service.endpoints.find(function (endpoint) { return endpoint.interface === entrypointInterface; });
if (!endpoint || !endpoint.url) {
this.logger.error("No ".concat(entrypointInterface, " endpoint found in ").concat(type, " service"));
throw new Error("No ".concat(entrypointInterface, " endpoint found in compute service"));
}
var parsedUrl = new URL(endpoint.url);
var config = new Identity.Configuration(__assign({}, this.configuration));
config.basePath = "".concat(parsedUrl.protocol, "//").concat(parsedUrl.host).concat(removeVersionFromPath(parsedUrl.pathname));
config.baseOptions.headers["X-Auth-Token"] = this._token;
if (type === "compute") {
this.Compute = __assign(__assign(__assign(__assign(__assign(__assign({}, Compute.ExtensionsApiFactory(config)), Compute.OsSimpleTenantUsageApiFactory(config)), Compute.ServersApiFactory(config)), Compute.FlavorsApiFactory(config)), Compute.ImagesApiFactory(config)), Compute.OsKeypairsApiFactory(config));
}
if (type === "network") {
this.Network = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, Network.NetworksApiFactory(config)), Network.SubnetsApiFactory(config)), Network.PortsApiFactory(config)), Network.SecurityGroupsApiFactory(config)), Network.SecurityGroupRulesApiFactory(config)), Network.FloatingipPoolsApiFactory(config)), Network.QosApiFactory(config)), Network.RoutersApiFactory(config));
}
if (type === "identity") {
this.Identity = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, Identity.AuthApiFactory(config)), Identity.RolesApiFactory(config)), Identity.UsersApiFactory(config)), Identity.RoleAssignmentsApiFactory(config)), Identity.ProjectsApiFactory(config)), Identity.DomainsApiFactory(config)), Identity.ServicesApiFactory(config)), Identity.EndpointsApiFactory(config)), Identity.GroupsApiFactory(config));
}
};
return OpenStack;
}());
exports.default = OpenStack;