Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SnsTemplate provides functionality to check for topic existence #1137

Open
hardikSinghBehl opened this issue Apr 20, 2024 · 2 comments
Open

Comments

@hardikSinghBehl
Copy link

hardikSinghBehl commented Apr 20, 2024

Type: Feature

Is your feature request related to a problem? Please describe.
When using SNS, I usually configure the SNS ARN using @ConfigurationProperties and pass it when using the SnsTemplate class. If an incorrect ARN is configured, the exception is thrown at runtime when the library tries to publish a message. To allow validation of SNS topic existence to be performed at application startup using ConstraintValidator, It would be great if a method boolean topicExists(String topicArn) is available in SnsTemplate class. This would be similar to the method boolean bucketExists(String bucketName) present in S3Template class.

Describe the solution you'd like
addition of method boolean topicExists(String topicArn) in SnsTemplate. This can be achieved using the read only action sns:ListTopics. Something like below

	public boolean topicExists(String topicArn) {
		Assert.notNull(topicArn, "topicArn is required");
		if (!isValidArnFormat(topicArn)) {
			throw SomeException();
		}
		final var snsTopics = getTopics();
		for (var snsTopic : snsTopics) {
			if (snsTopic.topicArn().equals(topicArn)) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}

	private List<Topic> getTopics() {
		final var snsTopics = new ArrayList<Topic>();
		ListTopicsResponse listTopicsResponse = snsClient.listTopics();
		snsTopics.addAll(listTopicsResponse.topics());

		while (listTopicsResponse.nextToken() != null) {
			var nextToken = listTopicsResponse.nextToken();
			listTopicsResponse = snsClient.listTopics(request -> request.nextToken(nextToken));
			snsTopics.addAll(listTopicsResponse.topics());
		}
		return snsTopics;
	}

Describe alternatives you've considered
Used the above solution in my application, but it'll be great if there was a concise way of doing this similar to S3Template

Additional context
Can submit PR if the feature is approved

@MatejNedic
Copy link
Member

Hey @hardikSinghBehl ,

Interesting idea. I am up for it but I would suggest using getTopicAttribute call.

Looking forward to your PR! :)

@hardikSinghBehl
Copy link
Author

sure @MatejNedic,

I've raised a PR and used the GetTopicAttributes call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants