abap_cloud_platform
Version:
A handful of cloud foundry API methods
298 lines • 14.6 kB
JavaScript
;
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.getAbapUserInfo = exports.getAbapSystemInfo = exports.cfCodeGrant = exports.loginServer = exports.cfPasswordGrant = exports.cfInstanceServiceKeyDelete = exports.cfInstanceServiceKeyCreate = exports.cfInstanceServiceKey = exports.cfInstanceServiceKeys = exports.cfServiceInstances = exports.cfServices = exports.cfSpaces = exports.cfOrganizations = exports.cfTokenKeys = exports.cfInfo = exports.isAbapEntity = exports.isAbapServiceKey = void 0;
const client_oauth2_1 = __importDefault(require("client-oauth2"));
const got_1 = __importDefault(require("got"));
const express_1 = __importDefault(require("express"));
const open_1 = __importDefault(require("open"));
const isCfLink = (x) => !!(x === null || x === void 0 ? void 0 : x.href);
const isCfInfo = (x) => {
let result = !!x.links;
if (result)
for (const k in x.links)
if (!(x.links[k] === null || isCfLink(x.links[k]))) {
result = false;
break;
}
return result;
};
const isUaaTokenKeys = (x) => !!((x === null || x === void 0 ? void 0 : x.kid) && (x === null || x === void 0 ? void 0 : x.value) && (x === null || x === void 0 ? void 0 : x.alg) && (x === null || x === void 0 ? void 0 : x.kty));
const isCfMetadata = (r) => !!(r === null || r === void 0 ? void 0 : r.guid) && !!r.created_at && !!r.url;
const isCfResource = (r) => !!(r === null || r === void 0 ? void 0 : r.entity) && !!isCfMetadata(r === null || r === void 0 ? void 0 : r.metadata);
const isCfResult = (keyRes) => Array.isArray(keyRes === null || keyRes === void 0 ? void 0 : keyRes.resources) &&
(keyRes.total_results || keyRes.total_results === 0);
const isCfOrganizationEntity = (x) => !!((x === null || x === void 0 ? void 0 : x.spaces_url) && (x === null || x === void 0 ? void 0 : x.app_events_url) && (x === null || x === void 0 ? void 0 : x.auditors_url) && (x === null || x === void 0 ? void 0 : x.name));
const isCfSpaceEntity = (x) => !!((x === null || x === void 0 ? void 0 : x.service_instances_url) &&
(x === null || x === void 0 ? void 0 : x.app_events_url) &&
(x === null || x === void 0 ? void 0 : x.auditors_url) &&
(x === null || x === void 0 ? void 0 : x.name));
const isCfServiceEntity = (x) => !!((x === null || x === void 0 ? void 0 : x.unique_id) && (x === null || x === void 0 ? void 0 : x.service_broker_guid) && (x === null || x === void 0 ? void 0 : x.description));
const isCfServiceInstanceEntity = (x) => !!((x === null || x === void 0 ? void 0 : x.service_guid) && (x === null || x === void 0 ? void 0 : x.service_url) && (x === null || x === void 0 ? void 0 : x.space_guid) && (x === null || x === void 0 ? void 0 : x.name));
const isAbapServiceKey = (x) => { var _a, _b, _c, _d; return !!(((_a = x === null || x === void 0 ? void 0 : x.catalogs) === null || _a === void 0 ? void 0 : _a.abap) && ((_b = x === null || x === void 0 ? void 0 : x.uaa) === null || _b === void 0 ? void 0 : _b.url) && ((_c = x.uaa) === null || _c === void 0 ? void 0 : _c.clientid) && ((_d = x.uaa) === null || _d === void 0 ? void 0 : _d.clientsecret)); };
exports.isAbapServiceKey = isAbapServiceKey;
const isAbapEntity = (x) => (0, exports.isAbapServiceKey)(x === null || x === void 0 ? void 0 : x.credentials);
exports.isAbapEntity = isAbapEntity;
const isAbapUserInfo = (x) => !!x.MANDT && !!x.UNAME;
const isInstalledLanguage = (x) => !!x.ISOLANG && !!x.SAPLANG;
const isAbapSystemInfo = (x) => !!(x.SYSID &&
x.SYSID.match(/^[A-Z][A-Z\d][A-Z\d]$/) &&
x.INSTALLED_LANGUAGES.every(isInstalledLanguage));
//////////////////////////////////////////////////////
function cfInfo(cfEndPoint) {
return __awaiter(this, void 0, void 0, function* () {
const headers = { Accept: "application/json" };
const resp = yield (0, got_1.default)(cfEndPoint, { headers });
const info = JSON.parse(resp.body);
if (!isCfInfo(info))
throw new Error("Failed to retrieve CloufFoundry info");
return info;
});
}
exports.cfInfo = cfInfo;
function cfTokenKeys(uaaLoginEndPoint) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const headers = { Accept: "application/json" };
const resp = yield (0, got_1.default)(`${uaaLoginEndPoint}/token_keys`, { headers });
const keys = (_a = JSON.parse(resp.body)) === null || _a === void 0 ? void 0 : _a.keys;
if (!Array.isArray(keys) || !keys.every(isUaaTokenKeys))
throw new Error("Failed to retrieve token keys");
return keys;
});
}
exports.cfTokenKeys = cfTokenKeys;
function cfOrganizations(cfEndPoint, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
Accept: "application/json"
};
const searchParams = { "order-by": "name", "order-direction": "asc" };
const resp = yield (0, got_1.default)(`${cfEndPoint}/v2/organizations`, {
headers,
searchParams
});
const orgRes = JSON.parse(resp.body);
if (!isCfResult(orgRes) ||
!orgRes.resources.every(r => isCfOrganizationEntity(r.entity)))
throw new Error("Unexpected response format for Organizations");
return orgRes.resources;
});
}
exports.cfOrganizations = cfOrganizations;
function cfSpaces(cfEndPoint, organization, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
Accept: "application/json"
};
const searchParams = { "order-by": "name", "order-direction": "asc" };
const resp = yield (0, got_1.default)(`${cfEndPoint}${organization.spaces_url}`, {
headers,
searchParams
});
const orgRes = JSON.parse(resp.body);
if (!isCfResult(orgRes) ||
!orgRes.resources.every(r => isCfSpaceEntity(r.entity)))
throw new Error("Unexpected response format for Spaces");
return orgRes.resources;
});
}
exports.cfSpaces = cfSpaces;
function cfServices(cfEndPoint, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
Accept: "application/json"
};
const searchParams = { "order-direction": "asc", active: true };
const options = { headers, searchParams };
const resp = yield (0, got_1.default)(`${cfEndPoint}/v2/services`, options);
const orgRes = JSON.parse(resp.body);
if (!isCfResult(orgRes) ||
!orgRes.resources.every(r => isCfServiceEntity(r.entity)))
throw new Error("Unexpected response format for service instance");
return orgRes.resources;
});
}
exports.cfServices = cfServices;
function cfServiceInstances(cfEndPoint, space, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
Accept: "application/json"
};
const searchParams = { "order-by": "name", "order-direction": "asc" };
const resp = yield (0, got_1.default)(`${cfEndPoint}${space.service_instances_url}`, {
headers,
searchParams
});
const orgRes = JSON.parse(resp.body);
if (!isCfResult(orgRes) ||
!orgRes.resources.every(r => isCfServiceInstanceEntity(r.entity)))
throw new Error("Unexpected response format for service instance");
return orgRes.resources;
});
}
exports.cfServiceInstances = cfServiceInstances;
function cfInstanceServiceKeys(cfEndPoint, instance, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
Accept: "application/json"
};
const resp = yield (0, got_1.default)(`${cfEndPoint}${instance.service_keys_url}`, { headers });
const keyRes = JSON.parse(resp.body);
if (!isCfResult(keyRes))
throw new Error("Unexpected response format for instance servicekey");
return keyRes.resources;
});
}
exports.cfInstanceServiceKeys = cfInstanceServiceKeys;
function cfInstanceServiceKey(cfEndPoint, instance, name, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
Accept: "application/json"
};
const resp = yield (0, got_1.default)(`${cfEndPoint}${instance.service_keys_url}`, {
headers,
searchParams: { q: `name:${name}` }
});
const keyRes = JSON.parse(resp.body);
if (!isCfResult(keyRes))
throw new Error("Unexpected response format for instance servicekey");
if (keyRes.total_results !== 1 || keyRes.resources.length !== 1)
throw new Error("Unexpected response format for instance servicekey");
return keyRes.resources[0];
});
}
exports.cfInstanceServiceKey = cfInstanceServiceKey;
function cfInstanceServiceKeyCreate(cfEndPoint, instance, name, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
"Content-Type": "application/json",
Accept: "application/json"
};
const body = JSON.stringify({
name,
service_instance_guid: instance.metadata.guid
});
const method = "POST";
const o = { headers, body };
const resp = yield (0, got_1.default)(`${cfEndPoint}/v2/service_keys`, Object.assign({ method }, o));
const keyRes = JSON.parse(resp.body);
if (!isCfResource(keyRes))
throw new Error("Unexpected response format for instance servicekey");
return keyRes;
});
}
exports.cfInstanceServiceKeyCreate = cfInstanceServiceKeyCreate;
function cfInstanceServiceKeyDelete(cfEndPoint, guid, token) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `bearer ${token}`,
"Content-Type": "application/json"
};
const method = "DELETE";
yield (0, got_1.default)(`${cfEndPoint}/v2/service_keys/${guid}`, {
method,
headers
});
});
}
exports.cfInstanceServiceKeyDelete = cfInstanceServiceKeyDelete;
function cfPasswordGrant(url, user, password) {
const oa = new client_oauth2_1.default({ accessTokenUri: `${url}/oauth/token` });
return oa.owner.getToken(user, password, {
headers: { Authorization: "Basic Y2Y6" }
});
}
exports.cfPasswordGrant = cfPasswordGrant;
function loginServer(port = 0, successHandler = (req, res) => res.send("Login successful, please close this window")) {
var _a;
const loginPath = "/oauth/client/redirect/link";
const successPath = "/logon/success";
const app = (0, express_1.default)();
const server = app.listen(port);
const actualPort = (_a = server.address()) === null || _a === void 0 ? void 0 : _a.port;
if (!actualPort)
throw new Error("Failed to start login server");
const callbackRequest = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
app.get(loginPath, (req, res) => {
res.status(302);
res.setHeader("Location", `http://localhost:${actualPort}${successPath}?code=${req.query.code}&action=link`);
res.send();
resolve(req);
});
app.get(successPath, (req, res, next) => {
successHandler(req, res, next);
server.close();
});
}));
const redirectUri = `http://localhost:${actualPort}${loginPath}`;
return { app, server, callbackRequest, redirectUri };
}
exports.loginServer = loginServer;
function cfCodeGrant(uaaUrl, clientId, clientSecret, server) {
return __awaiter(this, void 0, void 0, function* () {
const { redirectUri, callbackRequest } = server;
const oa = new client_oauth2_1.default({
authorizationUri: `${uaaUrl}/oauth/authorize`,
accessTokenUri: `${uaaUrl}/oauth/token`,
redirectUri,
clientId,
clientSecret
});
(0, open_1.default)(oa.code.getUri());
const url = (yield callbackRequest).url;
return yield oa.code.getToken(url);
});
}
exports.cfCodeGrant = cfCodeGrant;
function getAbapSystemInfo(abapUrl, token) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield (0, got_1.default)(`${abapUrl}/sap/bc/http/sap/a4c_api_session`, {
headers: {
Authorization: `bearer ${token}`,
Accept: "application/json"
},
searchParams: { q: "systeminfo" }
});
const systemInfo = JSON.parse(resp.body);
if (!isAbapSystemInfo(systemInfo))
throw new Error("Invalid system info record");
return systemInfo;
});
}
exports.getAbapSystemInfo = getAbapSystemInfo;
function getAbapUserInfo(abapUrl, token) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield (0, got_1.default)(`${abapUrl}/sap/bc/http/sap/a4c_api_session`, {
headers: {
Authorization: `bearer ${token}`,
Accept: "application/json"
},
searchParams: { q: "userinfo" }
});
const userInfo = JSON.parse(resp.body);
if (!isAbapUserInfo(userInfo))
throw new Error("Invalid user info record");
return userInfo;
});
}
exports.getAbapUserInfo = getAbapUserInfo;
//# sourceMappingURL=index.js.map