-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
queue-ref.ts
92 lines (78 loc) · 2.31 KB
/
queue-ref.ts
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
import { Construct, Output, PolicyStatement, Token } from '@aws-cdk/core';
import { QueuePolicy } from './policy';
import { QueueArn } from './sqs.generated';
/**
* Reference to a new or existing Amazon SQS queue
*/
export abstract class QueueRef extends Construct {
/**
* Import an existing queue
*/
public static import(parent: Construct, name: string, props: QueueRefProps) {
new ImportedQueue(parent, name, props);
}
/**
* The ARN of this queue
*/
public abstract readonly queueArn: QueueArn;
/**
* The URL of this queue
*/
public abstract readonly queueUrl: QueueUrl;
/**
* Controls automatic creation of policy objects.
*
* Set by subclasses.
*/
protected abstract readonly autoCreatePolicy: boolean;
private policy?: QueuePolicy;
/**
* Export a queue
*/
public export(): QueueRefProps {
return {
queueArn: new Output(this, 'QueueArn', { value: this.queueArn }).makeImportValue(),
queueUrl: new Output(this, 'QueueUrl', { value: this.queueUrl }).makeImportValue(),
};
}
/**
* Adds a statement to the IAM resource policy associated with this queue.
*
* If this queue was created in this stack (`new Queue`), a queue policy
* will be automatically created upon the first call to `addToPolicy`. If
* the queue is improted (`Queue.import`), then this is a no-op.
*/
public addToResourcePolicy(statement: PolicyStatement) {
if (!this.policy && this.autoCreatePolicy) {
this.policy = new QueuePolicy(this, 'Policy', { queues: [ this ] });
}
if (this.policy) {
this.policy.document.addStatement(statement);
}
}
}
/**
* Reference to a queue
*/
export interface QueueRefProps {
queueArn: QueueArn;
queueUrl: QueueUrl;
}
/**
* A queue that has been imported
*/
class ImportedQueue extends QueueRef {
public readonly queueArn: QueueArn;
public readonly queueUrl: QueueUrl;
protected readonly autoCreatePolicy = false;
constructor(parent: Construct, name: string, props: QueueRefProps) {
super(parent, name);
this.queueArn = props.queueArn;
this.queueUrl = props.queueUrl;
}
}
/**
* URL of a queue
*/
export class QueueUrl extends Token {
}