Skip to content

Commit bc33b8c

Browse files
authored
Removing topic ARN look-up and using ARN internal build method (#342)
* Removing topic ARN look up and using ARN building * Fixing minor issue * Adding check to avoid possible issues * Release prepare
1 parent 7bc0b7d commit bc33b8c

File tree

4 files changed

+26
-41
lines changed

4 files changed

+26
-41
lines changed

packages/sns/lib/utils/snsInitter.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ import type { SNSSQSQueueLocatorType } from '../sns/AbstractSnsSqsConsumer.ts'
1515
import { isCreateTopicCommand, type TopicResolutionOptions } from '../types/TopicTypes.ts'
1616
import type { SNSSubscriptionOptions } from './snsSubscriber.ts'
1717
import { subscribeToTopic } from './snsSubscriber.ts'
18-
import {
19-
assertTopic,
20-
deleteSubscription,
21-
deleteTopic,
22-
getTopicArnByName,
23-
getTopicAttributes,
24-
} from './snsUtils.ts'
18+
import { assertTopic, deleteSubscription, deleteTopic, getTopicAttributes } from './snsUtils.ts'
19+
import { buildTopicArn } from './stsUtils.js'
2520

2621
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: fixme
2722
export async function initSnsSqs(
@@ -93,7 +88,7 @@ export async function initSnsSqs(
9388
const checkPromises: Promise<Either<'not_found', unknown>>[] = []
9489
// Check for existing resources, using the locators
9590
const subscriptionTopicArn =
96-
locatorConfig.topicArn ?? (await getTopicArnByName(snsClient, locatorConfig.topicName))
91+
locatorConfig.topicArn ?? (await buildTopicArn(stsClient, locatorConfig.topicName ?? ''))
9792
const topicPromise = getTopicAttributes(snsClient, subscriptionTopicArn)
9893
checkPromises.push(topicPromise)
9994

@@ -212,16 +207,21 @@ export async function initSns(
212207
creationConfig?: SNSCreationConfig,
213208
) {
214209
if (locatorConfig) {
210+
if (!locatorConfig.topicArn && !locatorConfig.topicName) {
211+
throw new Error(
212+
'When locatorConfig for the topic is specified, either topicArn or topicName must be specified',
213+
)
214+
}
215+
215216
const topicArn =
216-
locatorConfig.topicArn ?? (await getTopicArnByName(snsClient, locatorConfig.topicName))
217+
locatorConfig.topicArn ?? (await buildTopicArn(stsClient, locatorConfig.topicName ?? ''))
218+
217219
const checkResult = await getTopicAttributes(snsClient, topicArn)
218220
if (checkResult.error === 'not_found') {
219221
throw new Error(`Topic with topicArn ${locatorConfig.topicArn} does not exist.`)
220222
}
221223

222-
return {
223-
topicArn,
224-
}
224+
return { topicArn }
225225
}
226226

227227
// create new topic if it does not exist
@@ -236,7 +236,6 @@ export async function initSns(
236236
allowedSourceOwner: creationConfig.allowedSourceOwner,
237237
forceTagUpdate: creationConfig.forceTagUpdate,
238238
})
239-
return {
240-
topicArn,
241-
}
239+
240+
return { topicArn }
242241
}

packages/sns/lib/utils/snsSubscriber.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { SNSClient, SubscribeCommandInput } from '@aws-sdk/client-sns'
22
import { SetSubscriptionAttributesCommand, SubscribeCommand } from '@aws-sdk/client-sns'
33
import type { CreateQueueCommandInput, SQSClient } from '@aws-sdk/client-sqs'
44
import type { STSClient } from '@aws-sdk/client-sts'
5+
import { InternalError, stringValueSerializer } from '@lokalise/node-core'
56
import type { ExtraParams } from '@message-queue-toolkit/core'
67
import type { ExtraSQSCreationParams } from '@message-queue-toolkit/sqs'
78
import { assertQueue } from '@message-queue-toolkit/sqs'
@@ -11,7 +12,8 @@ import {
1112
isSNSTopicLocatorType,
1213
type TopicResolutionOptions,
1314
} from '../types/TopicTypes.ts'
14-
import { assertTopic, findSubscriptionByTopicAndQueue, getTopicArnByName } from './snsUtils.ts'
15+
import { assertTopic, findSubscriptionByTopicAndQueue } from './snsUtils.ts'
16+
import { buildTopicArn } from './stsUtils.js'
1517

1618
export type SNSSubscriptionOptions = Omit<
1719
SubscribeCommandInput,
@@ -24,12 +26,11 @@ async function resolveTopicArnToSubscribeTo(
2426
topicConfiguration: TopicResolutionOptions,
2527
extraParams: (ExtraSNSCreationParams & ExtraSQSCreationParams & ExtraParams) | undefined,
2628
) {
27-
//If topicArn is present, let's use it and return early.
28-
if (isSNSTopicLocatorType(topicConfiguration) && topicConfiguration.topicArn) {
29-
return topicConfiguration.topicArn
29+
if (isSNSTopicLocatorType(topicConfiguration)) {
30+
if (topicConfiguration.topicArn) return topicConfiguration.topicArn
31+
if (topicConfiguration.topicName) return buildTopicArn(stsClient, topicConfiguration.topicName)
3032
}
3133

32-
//If input configuration is capable of creating a topic, let's create it and return its ARN.
3334
if (isCreateTopicCommand(topicConfiguration)) {
3435
return await assertTopic(snsClient, stsClient, topicConfiguration, {
3536
queueUrlsWithSubscribePermissionsPrefix: extraParams?.queueUrlsWithSubscribePermissionsPrefix,
@@ -38,8 +39,11 @@ async function resolveTopicArnToSubscribeTo(
3839
})
3940
}
4041

41-
//Last option: let's not create a topic but resolve a ARN based on the desired topic name.
42-
return await getTopicArnByName(snsClient, topicConfiguration.topicName)
42+
throw new InternalError({
43+
errorCode: 'invalid_topic_configuration',
44+
message: 'Invalid topic configuration provided, cannot resolve topic ARN',
45+
details: { topicConfiguration: stringValueSerializer(topicConfiguration) },
46+
})
4347
}
4448

4549
export async function subscribeToTopic(

packages/sns/lib/utils/snsUtils.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
GetTopicAttributesCommand,
77
ListSubscriptionsByTopicCommand,
88
ListTagsForResourceCommand,
9-
paginateListTopics,
109
SetTopicAttributesCommand,
1110
type SNSClient,
1211
TagResourceCommand,
@@ -182,23 +181,6 @@ export async function findSubscriptionByTopicAndQueue(
182181
})
183182
}
184183

185-
export async function getTopicArnByName(snsClient: SNSClient, topicName?: string): Promise<string> {
186-
if (!topicName) {
187-
throw new Error('topicName is not provided')
188-
}
189-
190-
// Use paginator to automatically handle NextToken
191-
const paginator = paginateListTopics({ client: snsClient }, {})
192-
for await (const page of paginator) {
193-
for (const topic of page.Topics || []) {
194-
if (topic.TopicArn?.includes(topicName)) {
195-
return topic.TopicArn
196-
}
197-
}
198-
}
199-
throw new Error(`Failed to resolve topic by name ${topicName}`)
200-
}
201-
202184
/**
203185
* Calculates the size of an outgoing SNS message.
204186
*

packages/sns/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@message-queue-toolkit/sns",
3-
"version": "23.1.2",
3+
"version": "23.1.3",
44
"private": false,
55
"license": "MIT",
66
"description": "SNS adapter for message-queue-toolkit",

0 commit comments

Comments
 (0)