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

fix(sns): allow tokens to be used in UrlSubscription #2938

Merged
merged 11 commits into from
Jul 4, 2019
14 changes: 7 additions & 7 deletions packages/@aws-cdk/aws-sns-subscriptions/lib/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sns = require('@aws-cdk/aws-sns');
import { SubscriptionProps } from './subscription';
import { Construct, Token } from '@aws-cdk/cdk';

/**
* Options for URL subscriptions.
Expand All @@ -23,17 +23,17 @@ export interface UrlSubscriptionProps extends SubscriptionProps {
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html
*/
export class UrlSubscription implements sns.ITopicSubscription {
constructor(private readonly url: string, private readonly props: UrlSubscriptionProps = {}) {
if (!url.startsWith('http://') && !url.startsWith('https://')) {
constructor(private readonly url: string, private readonly protocol: sns.SubscriptionProtocol, private readonly props: UrlSubscriptionProps = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have the protocol optionally inside the props, and derive it from the literal URL (if available).

Only force passing it if the URL is unresolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rix0rrr fixed in d524d12

if (!Token.isUnresolved(url) && !url.startsWith('http://') && !url.startsWith('https://')) {
throw new Error('URL must start with either http:// or https://');
}
}

public bind(_topic: sns.ITopic): sns.TopicSubscriptionConfig {
return {
subscriberId: this.url,
public bind(scope: Construct, topic: sns.ITopic): void {
new sns.Subscription(scope, Token.isUnresolved(this.url) ? 'UnresolvedUrl' : this.url, {
topic,
endpoint: this.url,
protocol: this.url.startsWith('https:') ? sns.SubscriptionProtocol.HTTPS : sns.SubscriptionProtocol.HTTP,
protocol: this.protocol,
rawMessageDelivery: this.props.rawMessageDelivery,
filterPolicy: this.props.filterPolicy,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-sns-subscriptions/test/subs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ beforeEach(() => {
});

test('url subscription', () => {
topic.addSubscription(new subs.UrlSubscription('https://foobar.com/'));
topic.addSubscription(new subs.UrlSubscription('https://foobar.com/', sns.SubscriptionProtocol.Https));

expect(stack).toMatchTemplate({
"Resources": {
Expand All @@ -45,7 +45,7 @@ test('url subscription', () => {
});

test('url subscription (with raw delivery)', () => {
topic.addSubscription(new subs.UrlSubscription('https://foobar.com/', {
topic.addSubscription(new subs.UrlSubscription('https://foobar.com/', sns.SubscriptionProtocol.Https, {
rawMessageDelivery: true
}));

Expand Down