@sap/destination-instance-service-provider
Version:
Provide service consumption of SAP IT services
268 lines • 14.8 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasServiceRetriever = void 0;
const _ = require("lodash");
const vkbeautify = require("vkbeautify");
const xml2jsonParser = require("xml2js");
const Adapters = require("./serviceProviderAdapter");
const messages_1 = require("./i18n/messages");
const service_provider_apis_1 = require("@sap/service-provider-apis");
const API_LIST_DESTINATION_PATH = "/api/listDestinations";
const ODATA_V2_CATALOG_SERVICES = "/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/";
const V2_SERVICES_PATH = "ServiceCollection";
const V2_RECOMMENDED_SERVICES_PATH = "RecommendedServiceCollection";
const ODATA_V4_CATALOG_SERVICES = "/sap/opu/odata4/iwfnd/config/default/iwfnd/catalog/0002/";
const V4_SERVICES_PATH = "ServiceGroups?$expand=DefaultSystem($expand=Services)";
const V4_RECOMMENDED_SERVICES_PATH = "ServiceGroups?$expand=DefaultSystem($expand=RecommendedServices)";
const DESTINATION_PATH = "/destinations";
const META_DATA_SUFFIX = "/$metadata";
const ANNOTATIONS_SUFFIX = "/Annotations";
class BasServiceRetriever {
constructor(serviceCredentials) {
this.logger = service_provider_apis_1.DefaultLogger;
this.clientId = serviceCredentials.clientId;
this.clientSecret = serviceCredentials.clientSecret;
this.h2oUrl = _.get(process, "env.H2O_URL");
this.proxy = _.get(process, "env.http_proxy");
}
retrieveServices(destinationName, destinationUrl, credentials, filter) {
return __awaiter(this, void 0, void 0, function* () {
this.logger.debug(`Inside retrieveServices`);
const serviceRetrieverUrls = this.getServiceRetrieveUrl(filter === null || filter === void 0 ? void 0 : filter.get("protocol"), destinationName);
let error;
const services = [];
let errorCount = 0;
for (const serviceRetrieverUrl of serviceRetrieverUrls) {
try {
yield this.executeSendServiceRequest(destinationName, serviceRetrieverUrl, services, credentials);
}
catch (e) {
error = e;
this.logger.warn(`${serviceRetrieverUrl} failed with error: ${error.message}`);
errorCount++;
}
}
if (error && errorCount === serviceRetrieverUrls.length) {
this.logger.error(`retrieveServices fails with: ${error}`);
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, error);
}
this.logger.debug(`Exiting retrieveServices with ${services.length} services`);
return services;
});
}
retrieveMetadata(destinationName, serviceUrl, encoding, credentials) {
return __awaiter(this, void 0, void 0, function* () {
if (encoding !== service_provider_apis_1.EncodingMode.XML) {
const e = new Error(`${service_provider_apis_1.messages.SYS_ERROR_FAIL_PARSE_RESPONSE}`);
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e);
}
const basCredentialsHeader = this.getBasCredentialsHeader(credentials);
const serviceUrlObj = new URL(`https://example.org${serviceUrl}`);
const serviceRetrieverUrl = `${DESTINATION_PATH}/${destinationName}${serviceUrlObj.pathname}${META_DATA_SUFFIX}${serviceUrlObj.search}${serviceUrlObj.hash}`;
try {
const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceRetrieverUrl, this.proxy, false, basCredentialsHeader);
return {
data: vkbeautify.xml(responseBody),
encoding: service_provider_apis_1.EncodingMode.XML,
};
}
catch (e) {
e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_METADATA}: ${e.message}`;
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e);
}
});
}
retrieveAnnotations(destinationName, serviceId, credentials) {
return __awaiter(this, void 0, void 0, function* () {
const basCredentialsHeader = this.getBasCredentialsHeader(credentials);
const serviceRetrieverUrl = `${DESTINATION_PATH}/${destinationName}${ODATA_V2_CATALOG_SERVICES}${V2_SERVICES_PATH}('${serviceId}')${ANNOTATIONS_SUFFIX}`;
try {
const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceRetrieverUrl, this.proxy, false, basCredentialsHeader);
const parsedAnnotations = yield xml2jsonParser.parseStringPromise(responseBody, {
explicitArray: false,
mergeAttrs: true,
});
let annotations = Adapters.AnnotationResponseToAnnotations(parsedAnnotations);
annotations = yield this.retrieveAnnotationsData(destinationName, annotations, basCredentialsHeader);
return annotations;
}
catch (e) {
e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_ANNOTATIONS}: ${e.message}`;
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e);
}
});
}
retrieveDestinations(filter) {
return __awaiter(this, void 0, void 0, function* () {
const urlPath = API_LIST_DESTINATION_PATH;
const basCredentialsHeader = this.getBasCredentialsHeader();
try {
const systems = [];
const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, urlPath, this.proxy, true, basCredentialsHeader);
responseBody.forEach((destinationConfiguration) => {
if (this.filterDestination(destinationConfiguration, filter)) {
systems.push(Adapters.DestinationToProviderSystem(destinationConfiguration, this));
}
});
return systems;
}
catch (e) {
e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_DESTINATIONS}: ${e.message}`;
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e);
}
});
}
getBasCredentialsHeader(credentials) {
const headerValue = Buffer.from(`${encodeURIComponent(this.clientId)}:${encodeURIComponent(this.clientSecret)}`).toString("base64");
const headers = { "bas-destination-instance-cred": headerValue };
if (credentials) {
headers.Authorization = credentials.getEncodedBasicAuthentication();
}
return headers;
}
filterDestination(destinationConfiguration, filter) {
let passFilter = true;
const webIDEEnabled = _.get(destinationConfiguration, "WebIDEEnabled", "");
if (webIDEEnabled != "true") {
return false;
}
if (filter) {
filter.forEach((filterValues, key) => {
const destinationValues = _.get(destinationConfiguration, key, "").trim().replace(" ", "").split(",");
if (_.intersection(filterValues, destinationValues).length == 0) {
passFilter = false;
return;
}
});
}
return passFilter;
}
getServiceRetrieveOldUrl(serviceRetrieverUrl, destinationName) {
if (serviceRetrieverUrl.includes(V4_RECOMMENDED_SERVICES_PATH)) {
return `${DESTINATION_PATH}/${destinationName}${ODATA_V4_CATALOG_SERVICES}${V4_SERVICES_PATH}`;
}
return `${DESTINATION_PATH}/${destinationName}${ODATA_V2_CATALOG_SERVICES}${V2_SERVICES_PATH}`;
}
getServiceRetrieveUrl(protocol, destinationName) {
const serviceRetriveUrls = [];
if (protocol) {
protocol.forEach((protocolValue) => {
if (protocolValue.toLowerCase() == "odatav2") {
serviceRetriveUrls.push(`${DESTINATION_PATH}/${destinationName}${ODATA_V2_CATALOG_SERVICES}${V2_RECOMMENDED_SERVICES_PATH}`);
}
if (protocolValue.toLowerCase() == "odatav4") {
serviceRetriveUrls.push(`${DESTINATION_PATH}/${destinationName}${ODATA_V4_CATALOG_SERVICES}${V4_RECOMMENDED_SERVICES_PATH}`);
}
});
}
else {
serviceRetriveUrls.push(`${DESTINATION_PATH}/${destinationName}${ODATA_V2_CATALOG_SERVICES}${V2_RECOMMENDED_SERVICES_PATH}`);
}
return serviceRetriveUrls;
}
executeSendServiceRequest(destinationName, destinationUrl, services, credentials) {
return __awaiter(this, void 0, void 0, function* () {
this.logger.debug(`Inside executeSendServiceRequest`);
try {
yield this.sendServiceRequest(destinationUrl, credentials, services);
}
catch (e) {
this.logger.debug(`executeSendServiceRequest: failed to retrieve data using ${destinationUrl} url, error: ${e}`);
const serviceRetrieveOldUrl = this.getServiceRetrieveOldUrl(destinationUrl, destinationName);
this.logger.debug(`Trying with old API using URL ${serviceRetrieveOldUrl}`);
yield this.sendServiceRequest(serviceRetrieveOldUrl, credentials, services);
}
return services;
});
}
sendServiceRequest(serviceUrl, credentials, services) {
return __awaiter(this, void 0, void 0, function* () {
const basCredentialsHeader = this.getBasCredentialsHeader(credentials);
let responseBody;
try {
responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceUrl, this.proxy, true, basCredentialsHeader);
}
catch (e) {
e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_SERVICES}: ${e.message}`;
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e);
}
yield this.handleServicesResponse(responseBody, serviceUrl, credentials, services);
});
}
handleServicesResponse(responseBody, serviceUrl, credentials, services) {
return __awaiter(this, void 0, void 0, function* () {
let responseServices;
const protocol = BasServiceRetriever.getServiceProtocolFromUrl(serviceUrl);
switch (protocol) {
case "odatav2":
responseServices = _.get(responseBody, ["d", "results"]);
break;
case "odatav4":
responseServices = _.get(responseBody, ["value"]);
break;
default:
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.INTERNAL_ERROR, service_provider_apis_1.messages.SYS_ERROR_FAIL_PARSE_RESPONSE);
}
if (!responseServices) {
throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, service_provider_apis_1.messages.SYS_ERROR_FAIL_PARSE_RESPONSE);
}
responseServices.forEach((responseService) => {
Adapters.ServiceResponseToService(responseService, protocol).forEach((service) => {
services.push(service);
});
});
if (protocol == "odatav4" && responseBody["@odata.nextLink"]) {
const nextUrl = responseBody["@odata.nextLink"];
const skipToken = nextUrl.substring(nextUrl.lastIndexOf("$skiptoken"), nextUrl.length);
const newServiceUrl = serviceUrl.lastIndexOf("&$skiptoken") < 0
? serviceUrl
: serviceUrl.substring(0, serviceUrl.lastIndexOf("$skiptoken"));
yield this.sendServiceRequest(`${newServiceUrl}&${skipToken}`, credentials, services);
}
});
}
static getServiceProtocolFromUrl(serviceUrl) {
if (serviceUrl.includes(ODATA_V2_CATALOG_SERVICES)) {
return "odatav2";
}
if (serviceUrl.includes(ODATA_V4_CATALOG_SERVICES)) {
return "odatav4";
}
return "";
}
retrieveAnnotationsData(destinationName, annotations, basCredentialsHeader) {
return __awaiter(this, void 0, void 0, function* () {
for (const annotation of annotations) {
if (annotation) {
const requestPath = `${DESTINATION_PATH}/${destinationName}${ODATA_V2_CATALOG_SERVICES}/${annotation.src}`;
try {
const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, requestPath, this.proxy, false, basCredentialsHeader);
annotation.data = vkbeautify.xml(responseBody);
}
catch (e) {
this.logger.warn(`Could not retrieve Annotations value from: ${requestPath}`);
this.logger.warn("Error message: " + e.message);
annotation.data = "";
}
}
}
return annotations;
});
}
retrieveLiveData() {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(messages_1.messages.ERROR_METHOD_NOT_IMPL);
});
}
}
exports.BasServiceRetriever = BasServiceRetriever;
//# sourceMappingURL=basServiceRetriever.js.map