1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_1 = __importDefault(require("fs"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const utils_1 = require("@terascope/utils");
|
9 | const execution_context_1 = require("./execution-context");
|
10 |
|
11 | function getOpConfig(job, name) {
|
12 | return job.operations.find((op) => op._op === name);
|
13 | }
|
14 | exports.getOpConfig = getOpConfig;
|
15 |
|
16 | async function getAssetPath(assetDir, assets, name) {
|
17 | if (!assetDir) {
|
18 | throw new Error('No asset_directroy has been configured, cannot get asset path');
|
19 | }
|
20 | const assetIds = assets || [];
|
21 | if (!name) {
|
22 | throw new Error('Invalid asset name');
|
23 | }
|
24 | if (name.length === 40) {
|
25 | const assetPath = path_1.default.join(assetDir, name);
|
26 | if (fs_1.default.existsSync(assetDir))
|
27 | return assetPath;
|
28 | }
|
29 | const [assetName] = name.split(':').map(s => s.trim());
|
30 | for (const id of assetIds) {
|
31 | const rawAssetJSON = fs_1.default.readFileSync(path_1.default.join(assetDir, id, 'asset.json'));
|
32 | const assetJSON = utils_1.parseJSON(rawAssetJSON);
|
33 | if (assetJSON.name === assetName) {
|
34 | return path_1.default.join(assetDir, id);
|
35 | }
|
36 | }
|
37 | throw new Error(`Unable to find asset "${name}"`);
|
38 | }
|
39 | exports.getAssetPath = getAssetPath;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function getClient(context, config, type) {
|
47 | const clientConfig = {
|
48 | type,
|
49 | cached: true,
|
50 | endpoint: 'default',
|
51 | };
|
52 | const events = context.apis.foundation.getSystemEvents();
|
53 | if (config && config.connection) {
|
54 | clientConfig.endpoint = config.connection || 'default';
|
55 | const isCached = config.connection_cache != null;
|
56 | clientConfig.cached = isCached ? config.connection_cache : true;
|
57 | }
|
58 | else {
|
59 | clientConfig.endpoint = 'default';
|
60 | clientConfig.cached = true;
|
61 | }
|
62 | try {
|
63 | return context.foundation.getConnection(clientConfig).client;
|
64 | }
|
65 | catch (err) {
|
66 | const message = `No configuration for endpoint ${clientConfig.endpoint} was found in the terafoundation connectors config`;
|
67 | context.logger.error(err, message);
|
68 | events.emit('client:initialization:error', {
|
69 | error: message,
|
70 | stack: err,
|
71 | });
|
72 | }
|
73 | }
|
74 | exports.getClient = getClient;
|
75 | function registerApis(context, job, assetIds) {
|
76 | if (context.apis.executionContext == null) {
|
77 | context.apis.registerAPI('executionContext', new execution_context_1.ExecutionContextAPI(context, job));
|
78 | }
|
79 | delete context.apis.op_runner;
|
80 | context.apis.registerAPI('op_runner', {
|
81 | getClient(config, type) {
|
82 | return getClient(context, config, type);
|
83 | },
|
84 | });
|
85 | delete context.apis.job_runner;
|
86 | context.apis.registerAPI('job_runner', {
|
87 | getOpConfig(name) {
|
88 | return getOpConfig(job, name);
|
89 | },
|
90 | });
|
91 | const assetDir = context.sysconfig.teraslice.assets_directory;
|
92 | delete context.apis.assets;
|
93 | context.apis.registerAPI('assets', {
|
94 | getPath(name) {
|
95 | return getAssetPath(assetDir || '', assetIds || job.assets, name);
|
96 | },
|
97 | });
|
98 | }
|
99 | exports.registerApis = registerApis;
|
100 |
|
\ | No newline at end of file |