-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChimeSipRule.java
120 lines (106 loc) · 5.1 KB
/
ChimeSipRule.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package cloud.cleo.chimesma.cdk.customresources;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import lombok.AllArgsConstructor;
import software.amazon.awscdk.RemovalPolicy;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.customresources.AwsCustomResource;
import software.amazon.awscdk.customresources.AwsCustomResourcePolicy;
import software.amazon.awscdk.customresources.AwsCustomResourceProps;
import software.amazon.awscdk.customresources.AwsSdkCall;
import software.amazon.awscdk.customresources.PhysicalResourceId;
import software.amazon.awscdk.customresources.PhysicalResourceIdReference;
import software.amazon.awscdk.customresources.SdkCallsPolicyOptions;
import software.amazon.awscdk.services.logs.LogGroup;
import software.amazon.awscdk.services.logs.LogGroupProps;
import software.amazon.awscdk.services.logs.RetentionDays;
/**
* Base to support both URI and Phone number rules
*
* @author sjensen
*/
public abstract class ChimeSipRule extends AwsCustomResource {
private static final AtomicInteger ID_COUNTER = new AtomicInteger(0);
private static final String ID = "SR-CR";
private static final String ID_LOGS = ID + "-LOGS";
/**
* The SIP Rule ID in the API response
*/
private static final String SR_ID = "SipRule.SipRuleId";
protected ChimeSipRule(Stack scope, String triggerValue, List<ChimeSipMediaApp> smas, SipRuleTriggerType type, String name) {
super(scope, ID + ID_COUNTER.incrementAndGet(), AwsCustomResourceProps.builder()
.resourceType("Custom::SipRule")
.installLatestAwsSdk(Boolean.FALSE)
.policy(AwsCustomResourcePolicy.fromSdkCalls(SdkCallsPolicyOptions.builder().resources(AwsCustomResourcePolicy.ANY_RESOURCE).build()))
.onCreate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("CreateSipRuleCommand")
.physicalResourceId(PhysicalResourceId.fromResponse(SR_ID))
// SIP Rules are not region specific, so we need a unique name across all stacks, hence static counter
.parameters(Map.of(
"Name", name,
"TriggerType", type.toString(),
"TriggerValue",triggerValue,
"Disabled", false,
"TargetApplications", smas.stream().map(new TAMapper()).toList()
))
.build())
.onUpdate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("UpdateSipRuleCommand")
.physicalResourceId(PhysicalResourceId.fromResponse(SR_ID))
.parameters(Map.of(
"SipRuleId", new PhysicalResourceIdReference(),
"Name", name,
"TriggerType", type.toString(),
"TriggerValue",triggerValue,
"Disabled", false,
"TargetApplications", smas.stream().map(new TAMapper()).toList()
))
.build())
.onDelete(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("DeleteSipRuleCommand")
.parameters(Map.of("SipRuleId", new PhysicalResourceIdReference()))
.build())
.logGroup(new LogGroup(scope, ID_LOGS + ID_COUNTER.get(), LogGroupProps.builder()
.retention(RetentionDays.ONE_MONTH)
.removalPolicy(RemovalPolicy.DESTROY).build()))
.build());
}
public enum SipRuleTriggerType {
RequestUriHostname,
ToPhoneNumber
}
/**
* Map SMA's to Target applications with incrementing priority
*/
public static class TAMapper implements Function<ChimeSipMediaApp,TargetApplication> {
private final AtomicInteger counter = new AtomicInteger(0);
/**
*
* @param sma
* @return
*/
@Override
public TargetApplication apply(ChimeSipMediaApp sma) {
return new TargetApplication(sma.getSMAId(), counter.incrementAndGet(), sma.getRegion());
}
}
@AllArgsConstructor
private static class TargetApplication {
@JsonProperty(value = "SipMediaApplicationId")
String sipMediaApplicationId;
@JsonProperty(value = "Priority")
Integer priority;
@JsonProperty(value = "AwsRegion")
String awsRegion;
}
}