1 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2 | return new (P || (P = Promise))(function (resolve, reject) {
|
3 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
4 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
5 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
6 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
7 | });
|
8 | };
|
9 | const { getProfile } = require('./profile');
|
10 | const { getFcClient } = require('./client');
|
11 | const _ = require('lodash');
|
12 | function makeDestination(serviceName, functionName, asyncConfiguration, qualifier = 'LATEST') {
|
13 | return __awaiter(this, void 0, void 0, function* () {
|
14 | const { accountId, defaultRegion } = yield getProfile();
|
15 | const { OnSuccess, OnFailure } = asyncConfiguration.Destination;
|
16 | const destinationConfig = {};
|
17 | if (OnSuccess) {
|
18 | destinationConfig.onSuccess = {
|
19 | destination: OnSuccess.replace(':::', `:${defaultRegion}:${accountId}:`)
|
20 | };
|
21 | }
|
22 | if (OnFailure) {
|
23 | destinationConfig.onFailure = {
|
24 | destination: OnFailure.replace(':::', `:${defaultRegion}:${accountId}:`)
|
25 | };
|
26 | }
|
27 | const asyncConfig = {
|
28 | maxAsyncRetryAttempts: asyncConfiguration.MaxAsyncRetryAttempts,
|
29 | maxAsyncEventAgeInSeconds: asyncConfiguration.MaxAsyncEventAgeInSeconds,
|
30 | destinationConfig
|
31 | };
|
32 | let hasAsyncConfig = false;
|
33 | const fcClient = yield getFcClient();
|
34 | try {
|
35 | const { data } = yield fcClient.getFunctionAsyncConfig(serviceName, functionName, qualifier);
|
36 | const asyncConfigCache = {
|
37 | destinationConfig: data.destinationConfig,
|
38 | maxAsyncEventAgeInSeconds: data.maxAsyncEventAgeInSeconds,
|
39 | maxAsyncRetryAttempts: data.maxAsyncRetryAttempts
|
40 | };
|
41 | if (_.isEqual(asyncConfig, asyncConfigCache)) {
|
42 | return;
|
43 | }
|
44 | hasAsyncConfig = true;
|
45 | }
|
46 | catch (ex) {
|
47 | if (ex.code !== 'AsyncConfigNotExists') {
|
48 | throw ex;
|
49 | }
|
50 | }
|
51 | if (hasAsyncConfig) {
|
52 | try {
|
53 | yield fcClient.deleteFunctionAsyncConfig(serviceName, functionName, qualifier);
|
54 | }
|
55 | catch (ex) {
|
56 | throw ex;
|
57 | }
|
58 | }
|
59 | try {
|
60 | yield fcClient.putFunctionAsyncConfig(serviceName, functionName, qualifier, asyncConfig);
|
61 | }
|
62 | catch (ex) {
|
63 | throw ex;
|
64 | }
|
65 | });
|
66 | }
|
67 | module.exports = {
|
68 | makeDestination
|
69 | };
|