1
1
from .deployment_preference import DeploymentPreference
2
2
from samtranslator .model .codedeploy import CodeDeployApplication
3
3
from samtranslator .model .codedeploy import CodeDeployDeploymentGroup
4
+ from samtranslator .model .exceptions import InvalidResourceException
4
5
from samtranslator .model .iam import IAMRole
5
- from samtranslator .model .intrinsics import fnSub , is_intrinsic
6
+ from samtranslator .model .intrinsics import (
7
+ fnSub ,
8
+ is_intrinsic ,
9
+ is_intrinsic_if ,
10
+ is_intrinsic_no_value ,
11
+ validate_intrinsic_if_items ,
12
+ )
6
13
from samtranslator .model .update_policy import UpdatePolicy
7
14
from samtranslator .translator .arn_generator import ArnGenerator
8
15
import copy
@@ -125,11 +132,10 @@ def deployment_group(self, function_logical_id):
125
132
126
133
deployment_group = CodeDeployDeploymentGroup (self .deployment_group_logical_id (function_logical_id ))
127
134
128
- if deployment_preference .alarms is not None :
129
- deployment_group .AlarmConfiguration = {
130
- "Enabled" : True ,
131
- "Alarms" : [{"Name" : alarm } for alarm in deployment_preference .alarms ],
132
- }
135
+ try :
136
+ deployment_group .AlarmConfiguration = self ._convert_alarms (deployment_preference .alarms )
137
+ except ValueError as e :
138
+ raise InvalidResourceException (function_logical_id , str (e ))
133
139
134
140
deployment_group .ApplicationName = self .codedeploy_application .get_runtime_attr ("name" )
135
141
deployment_group .AutoRollbackConfiguration = {
@@ -152,6 +158,68 @@ def deployment_group(self, function_logical_id):
152
158
153
159
return deployment_group
154
160
161
+ def _convert_alarms (self , preference_alarms ):
162
+ """
163
+ Converts deployment preference alarms to an AlarmsConfiguration
164
+
165
+ Parameters
166
+ ----------
167
+ preference_alarms : dict
168
+ Deployment preference alarms
169
+
170
+ Returns
171
+ -------
172
+ dict
173
+ AlarmsConfiguration if alarms is set, None otherwise
174
+
175
+ Raises
176
+ ------
177
+ ValueError
178
+ If Alarms is in the wrong format
179
+ """
180
+ if not preference_alarms or is_intrinsic_no_value (preference_alarms ):
181
+ return None
182
+
183
+ if is_intrinsic_if (preference_alarms ):
184
+ processed_alarms = copy .deepcopy (preference_alarms )
185
+ alarms_list = processed_alarms .get ("Fn::If" )
186
+ validate_intrinsic_if_items (alarms_list )
187
+ alarms_list [1 ] = self ._build_alarm_configuration (alarms_list [1 ])
188
+ alarms_list [2 ] = self ._build_alarm_configuration (alarms_list [2 ])
189
+ return processed_alarms
190
+
191
+ return self ._build_alarm_configuration (preference_alarms )
192
+
193
+ def _build_alarm_configuration (self , alarms ):
194
+ """
195
+ Builds an AlarmConfiguration from a list of alarms
196
+
197
+ Parameters
198
+ ----------
199
+ alarms : list[str]
200
+ Alarms
201
+
202
+ Returns
203
+ -------
204
+ dict
205
+ AlarmsConfiguration for a deployment group
206
+
207
+ Raises
208
+ ------
209
+ ValueError
210
+ If alarms is not a list
211
+ """
212
+ if not isinstance (alarms , list ):
213
+ raise ValueError ("Alarms must be a list" )
214
+
215
+ if len (alarms ) == 0 or is_intrinsic_no_value (alarms [0 ]):
216
+ return {}
217
+
218
+ return {
219
+ "Enabled" : True ,
220
+ "Alarms" : [{"Name" : alarm } for alarm in alarms ],
221
+ }
222
+
155
223
def _replace_deployment_types (self , value , key = None ):
156
224
if isinstance (value , list ):
157
225
for i in range (len (value )):
0 commit comments