All files / aws-sqs/dist component.js

90.54% Statements 67/74
80% Branches 36/45
92.86% Functions 13/14
94.2% Lines 65/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151  1x 41x 11x 41x   52x 11x     1x 1x   1x 1x 1x 1x 1x 1x 1x                         8x 8x 8x 8x           8x         8x         8x 8x 8x 8x       8x       8x 6x 6x           2x         2x 1x 1x 1x   2x 2x           8x 2x 2x   8x 8x 8x 8x 8x 8x 8x       2x 2x       2x           2x 2x 1x   1x                     1x 1x 1x 1x       1x 1x           1x 1x 1x 1x 1x       1x  
"use strict";
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 });
const aws_sdk_1 = __importDefault(require("aws-sdk"));
const ramda_1 = require("ramda");
const utils_1 = require("./utils");
const core_1 = require("@serverless/core");
const outputsList = ["arn", "url"];
const defaults = {
    name: "serverless",
    region: "us-east-1",
    tags: undefined
};
class AwsSqsQueue extends core_1.Component {
    default(inputs = {
        name: undefined,
        region: undefined,
        arn: undefined,
        url: undefined,
        tags: undefined
    }) {
        return __awaiter(this, void 0, void 0, function* () {
            const config = ramda_1.mergeDeepRight(utils_1.getDefaults({ defaults }), inputs);
            const accountId = yield utils_1.getAccountId();
            Iif (aws_sdk_1.default && aws_sdk_1.default.config) {
                aws_sdk_1.default.config.update({
                    maxRetries: parseInt(process.env.SLS_NEXT_MAX_RETRIES || "10"),
                    retryDelayOptions: { base: 200 }
                });
            }
            const arn = utils_1.getArn({
                accountId,
                name: config.name,
                region: config.region
            });
            const queueUrl = utils_1.getUrl({
                accountId,
                name: config.name,
                region: config.region
            });
            config.arn = arn;
            config.url = queueUrl;
            this.context.status(`Deploying`);
            const sqs = new aws_sdk_1.default.SQS({
                region: config.region,
                credentials: this.context.credentials.aws
            });
            const prevInstance = yield utils_1.getQueue({
                sqs,
                queueUrl: this.state.url || queueUrl
            });
            if (ramda_1.isEmpty(prevInstance)) {
                this.context.status(`Creating`);
                yield utils_1.createQueue({
                    sqs,
                    config: config
                });
            }
            else {
                Iif (this.state.url === queueUrl) {
                    this.context.status(`Updating`);
                    yield utils_1.setAttributes(sqs, queueUrl, config);
                }
                else {
                    if (this.state.url) {
                        this.context.debug(`The QueueUrl has changed`);
                        this.context.debug(`Deleting previous queue`);
                        yield utils_1.deleteQueue({ sqs, queueUrl: this.state.url });
                    }
                    this.context.debug(`Creating new queue`);
                    yield utils_1.createQueue({
                        sqs,
                        config: config
                    });
                }
            }
            if (config.tags) {
                this.context.debug("Configuring SQS queue tags since they are specified.");
                yield utils_1.configureTags(this.context, sqs, queueUrl, config.tags);
            }
            this.state.name = config.name;
            this.state.arn = config.arn;
            this.state.url = config.url;
            this.state.region = config.region;
            this.state.tags = config.tags;
            yield this.save();
            return ramda_1.pick(outputsList, config);
        });
    }
    addEventSource(functionArn) {
        return __awaiter(this, void 0, void 0, function* () {
            const lambda = new aws_sdk_1.default.Lambda({
                region: this.state.region,
                credentials: this.context.credentials.aws
            });
            const existing = yield lambda
                .listEventSourceMappings({
                EventSourceArn: this.state.arn,
                FunctionName: functionArn
            })
                .promise();
            const mappings = existing.EventSourceMappings || [];
            if (mappings.length) {
                return;
            }
            yield lambda
                .createEventSourceMapping({
                EventSourceArn: this.state.arn,
                FunctionName: functionArn
            })
                .promise();
        });
    }
    remove(inputs = {
        name: undefined
    }) {
        return __awaiter(this, void 0, void 0, function* () {
            const config = ramda_1.mergeDeepRight(defaults, inputs);
            config.name = inputs.name || this.state.name || defaults.name;
            const sqs = new aws_sdk_1.default.SQS({
                region: config.region,
                credentials: this.context.credentials.aws
            });
            const accountId = yield utils_1.getAccountId();
            const queueUrl = this.state.url ||
                utils_1.getUrl({
                    accountId,
                    name: config.name,
                    region: config.region
                });
            this.context.status(`Removing`);
            yield utils_1.deleteQueue({ sqs, queueUrl });
            this.state = {};
            yield this.save();
            return {};
        });
    }
}
exports.default = AwsSqsQueue;