| 21 | const uuid = require('uuid'); |
| 22 | |
| 23 | class QueueManager { |
| 24 | constructor() { |
| 25 | this.pubsub = new PubSub(); |
| 26 | AWS.config.update({region: 'us-west-1'}); |
| 27 | this.sqs = new AWS.SQS({apiVersion: '2012-11-05'}); |
| 28 | |
| 29 | /** |
| 30 | * @type {Topic[]} |
| 31 | */ |
| 32 | this.pubsubTopics = []; |
| 33 | /** |
| 34 | * @type {Subscription[]} |
| 35 | */ |
| 36 | this.pubsubSubscriptions = []; |
| 37 | /** |
| 38 | * @type {string[]} |
| 39 | */ |
| 40 | this.sqsQueues = []; |
| 41 | } |
| 42 | |
| 43 | async generatePubsubSubscriptionId() { |
| 44 | const topicId = `sts-topic-id-${uuid.v4()}`; |
| 45 | const topic = await this.pubsub.createTopic(topicId); |
| 46 | this.pubsubTopics.push(topic[0]); |
| 47 | const subscriptionId = `sts-subscription-id-${uuid.v4()}`; |
| 48 | const subscription = await this.pubsub |
| 49 | .topic(topicId) |
| 50 | .createSubscription(subscriptionId); |
| 51 | this.pubsubSubscriptions.push(subscription[0]); |
| 52 | |
| 53 | return subscription[0].name; |
| 54 | } |
| 55 | |
| 56 | async deletePubsubSubscriptionsAndTopics() { |
| 57 | for (const s of this.pubsubSubscriptions) { |
| 58 | this.pubsub.subscription(s.name).delete(); |
| 59 | } |
| 60 | for (const t of this.pubsubTopics) { |
| 61 | this.pubsub.topic(t.name).delete(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | async generateSqsQueueArn() { |
| 66 | return await new Promise((resolve, reject) => { |
| 67 | const queueName = `sts-queue-name-${uuid.v4()}`; |
| 68 | this.sqs.createQueue( |
| 69 | {QueueName: queueName}, |
| 70 | (error, createQueueResult) => { |
| 71 | if (error) { |
| 72 | console.error(error); |
| 73 | return reject(error); |
| 74 | } |
| 75 | this.sqsQueues.push(createQueueResult.QueueUrl); |
| 76 | this.sqs.getQueueAttributes( |
| 77 | { |
| 78 | QueueUrl: createQueueResult.QueueUrl, |
| 79 | AttributeNames: ['QueueArn'], |
| 80 | }, |
nothing calls this directly
no outgoing calls
no test coverage detected