UNPKG

@godspeedsystems/plugins-aws-as-datasource

Version:

aws as datasource plugin for Godspeed Framework

205 lines 9.22 kB
"use strict"; 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_CONFIG = exports.CONFIG_FILE_NAME = exports.Type = exports.SourceType = exports.DataSource = void 0; const core_1 = require("@godspeedsystems/core"); //Some example client mappings. Developer should set the mappings of all //service types she needs in their datasource's instance's yaml file. //If she sets even one mapping. that is used, and this one is ignored. const SERVICE_CLIENT_MAPPINGS = { dynamodb: 'DynamoDB', s3: 'S3', lambda: 'Lambda', ssm: 'SSM', sqs: 'SQS' }; class AWSDataSource extends core_1.GSDataSource { initClient() { return __awaiter(this, void 0, void 0, function* () { const client = yield this.initializeClients(); return client; }); } /** * Config will allow multiple clients initializations of the each type of service * * For ex. A sample aws services config * * ```yaml * type:aws * default_client_config: # default config. Config will be given. As per AWS sdk API specs * services: * s3_1: * type: s3 * config: #will override default * s3_2: * type: s3 * dynamodb: * type: dynamodb * ``` */ initializeClients() { return __awaiter(this, void 0, void 0, function* () { var _a; const dsConfig = this.config; const serviceModules = {}; const clients = {}; if (!dsConfig.services) { throw new Error(`The AWS datasource config in ${dsConfig.name}.yaml does not have any services declared under the key 'services'`); } const serviceClientMappings = this.config.types || SERVICE_CLIENT_MAPPINGS; for (let serviceName of Object.keys(dsConfig.services)) { const serviceConfig = dsConfig.services[serviceName]; const serviceType = serviceConfig.type; if (!serviceType) { throw new Error(`The AWS datasource config in ${dsConfig.name}.yaml does not define service 'type' for serviceName ${serviceName}. Define the type of service like s3, dynamodb etc`); } let serviceModule = serviceModules[serviceType]; if (!serviceModule) { const clientLibrary = yield (_a = `@aws-sdk/client-${serviceType}`, Promise.resolve().then(() => __importStar(require(_a)))); serviceModule = clientLibrary[serviceClientMappings[serviceType]]; serviceModules[serviceType] = serviceModule; } const Constructor = serviceModule; const serviceClient = new Constructor(serviceConfig.config || dsConfig.default_client_config); clients[serviceName] = serviceClient; } return clients; }); } /** * The `fnNameInWorkflow` arg represets the `fn` name which is like * `datasource.<aws_instance_name>.<service_name>.<method_name>` * For ex. `datasource.aws.s3_1.createTable` * @param ctx * @param args * @returns */ execute(ctx, args) { var _a; return __awaiter(this, void 0, void 0, function* () { const { meta: { entityType: serviceName, method } } = args, rest = __rest(args, ["meta"]); try { // fn validity checks // const fnParts = this.isFnValid(fnNameInWorkflow, ctx); // if (!fnParts) { // throw new Error(); // } if (!serviceName) { ctx.childLogger.error("Invalid aws datasource fn. Service name not found. fn is epxected to be of this format: datasource.<aws_instance_name>.<service_name>.<method_name>"); return new core_1.GSStatus(false, 500, undefined, { message: "Internal server error" }); } if (!method) { ctx.childLogger.error("Invalid aws datasource fn. Method's name Not found. fn is epxected to be of this format: datasource.<aws_instance_name>.<service_name>.<method_name>"); return new core_1.GSStatus(false, 500, undefined, { message: "Internal server error" }); } // let [, , serviceName, method] = fnParts; //@ts-ignore const serviceClient = this.client && this.client[serviceName]; if (!serviceClient) { ctx.childLogger.error(`Invalid AWS service name '${serviceName}'`); return new core_1.GSStatus(false, 500, undefined, { message: "Internal server error" }); } if (typeof serviceClient[method] === 'function') { //Invoke the method const response = yield serviceClient[method](rest); return new core_1.GSStatus(true, 200, undefined, response); } else { ctx.childLogger.error(`Invalid method '${method}' called for datasource.aws.${serviceName}`); return new core_1.GSStatus(false, 500, undefined, { message: "Internal server error" }); } } catch (error) { ctx.childLogger.error(`Error encountered in executing ${serviceName}.${method}. ${error}`); return new core_1.GSStatus(false, ((_a = error.$metadata) === null || _a === void 0 ? void 0 : _a.httpStatusCode) || 500, undefined, { message: "Internal server error" }); } }); } } exports.default = AWSDataSource; exports.DataSource = AWSDataSource; const SourceType = 'DS'; exports.SourceType = SourceType; const Type = 'aws'; // this is the loader file of the plugin, So the final loader file will be `types/${Type.js}` exports.Type = Type; const CONFIG_FILE_NAME = 'aws'; // in case of event source, this also works as event identifier, and in case of datasource works as datasource name exports.CONFIG_FILE_NAME = CONFIG_FILE_NAME; const DEFAULT_CONFIG = { type: 'aws', default_client_config: { region: "", credentials: { accessKeyId: "", secretAccessKey: "" } }, services: { s3: { type: "s3" }, dynamodb: { type: "dynamodb" }, sqs: { type: "sqs" }, ssm: { type: "ssm" }, lambda: { type: "lambda" } }, types: { dynamodb: "DynamoDB", s3: "S3", lambda: "Lambda", ssm: 'SSM', sqs: "SQS" } }; exports.DEFAULT_CONFIG = DEFAULT_CONFIG; //# sourceMappingURL=index.js.map