Skip to content

Commit 1c2e550

Browse files
keetonianhawflau
authored andcommitted
fix code deploy conditions bug
1 parent 2be47df commit 1c2e550

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

samtranslator/model/preferences/deployment_preference.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@
2525
"""
2626
DeploymentPreferenceTuple = namedtuple(
2727
"DeploymentPreferenceTuple",
28-
["deployment_type", "pre_traffic_hook", "post_traffic_hook", "alarms", "enabled", "role", "trigger_configurations"],
28+
[
29+
"deployment_type",
30+
"pre_traffic_hook",
31+
"post_traffic_hook",
32+
"alarms",
33+
"enabled",
34+
"role",
35+
"trigger_configurations",
36+
"condition",
37+
],
2938
)
3039

3140

@@ -37,17 +46,18 @@ class DeploymentPreference(DeploymentPreferenceTuple):
3746
"""
3847

3948
@classmethod
40-
def from_dict(cls, logical_id, deployment_preference_dict):
49+
def from_dict(cls, logical_id, deployment_preference_dict, condition=None):
4150
"""
4251
:param logical_id: the logical_id of the resource that owns this deployment preference
4352
:param deployment_preference_dict: the dict object taken from the SAM template
53+
:param condition: condition on this deployment preference
4454
:return:
4555
"""
4656
enabled = deployment_preference_dict.get("Enabled", True)
4757
enabled = False if enabled in ["false", "False"] else enabled
4858

4959
if not enabled:
50-
return DeploymentPreference(None, None, None, None, False, None, None)
60+
return DeploymentPreference(None, None, None, None, False, None, None, None)
5161

5262
if "Type" not in deployment_preference_dict:
5363
raise InvalidResourceException(logical_id, "'DeploymentPreference' is missing required Property 'Type'")
@@ -65,5 +75,12 @@ def from_dict(cls, logical_id, deployment_preference_dict):
6575
role = deployment_preference_dict.get("Role", None)
6676
trigger_configurations = deployment_preference_dict.get("TriggerConfigurations", None)
6777
return DeploymentPreference(
68-
deployment_type, pre_traffic_hook, post_traffic_hook, alarms, enabled, role, trigger_configurations
78+
deployment_type,
79+
pre_traffic_hook,
80+
post_traffic_hook,
81+
alarms,
82+
enabled,
83+
role,
84+
trigger_configurations,
85+
condition,
6986
)

samtranslator/model/preferences/deployment_preference_collection.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ def __init__(self):
4848
self.codedeploy_application = self._codedeploy_application()
4949
self.codedeploy_iam_role = self._codedeploy_iam_role()
5050

51-
def add(self, logical_id, deployment_preference_dict):
51+
def add(self, logical_id, deployment_preference_dict, condition=None):
5252
"""
5353
Add this deployment preference to the collection
5454
5555
:raise ValueError if an existing logical id already exists in the _resource_preferences
5656
:param logical_id: logical id of the resource where this deployment preference applies
5757
:param deployment_preference_dict: the input SAM template deployment preference mapping
58+
:param condition: the condition (if it exists) on the serverless function
5859
"""
5960
if logical_id in self._resource_preferences:
6061
raise ValueError(
@@ -63,7 +64,9 @@ def add(self, logical_id, deployment_preference_dict):
6364
)
6465
)
6566

66-
self._resource_preferences[logical_id] = DeploymentPreference.from_dict(logical_id, deployment_preference_dict)
67+
self._resource_preferences[logical_id] = DeploymentPreference.from_dict(
68+
logical_id, deployment_preference_dict, condition
69+
)
6770

6871
def get(self, logical_id):
6972
"""
@@ -85,6 +88,13 @@ def can_skip_service_role(self):
8588
"""
8689
return all(preference.role or not preference.enabled for preference in self._resource_preferences.values())
8790

91+
def needs_resource_condition(self):
92+
"""
93+
If all preferences have a condition, all code deploy resources need to be conditionally created
94+
:return: True, if a condition needs to be created
95+
"""
96+
return all(preference.condition for preference in self._resource_preferences.values())
97+
8898
def enabled_logical_ids(self):
8999
"""
90100
:return: only the logical id's for the deployment preferences in this collection which are enabled
@@ -156,6 +166,9 @@ def deployment_group(self, function_logical_id):
156166
if deployment_preference.trigger_configurations:
157167
deployment_group.TriggerConfigurations = deployment_preference.trigger_configurations
158168

169+
if deployment_preference.condition:
170+
deployment_group.set_resource_attribute("Condition", deployment_preference.condition)
171+
159172
return deployment_group
160173

161174
def _convert_alarms(self, preference_alarms):

samtranslator/model/sam_resources.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def to_cloudformation(self, **kwargs):
185185
lambda_alias,
186186
intrinsics_resolver,
187187
mappings_resolver,
188+
self.get_passthrough_resource_attributes(),
188189
)
189190
event_invoke_policies = []
190191
if self.EventInvokeConfig:
@@ -827,7 +828,7 @@ def _construct_alias(self, name, function, version):
827828
return alias
828829

829830
def _validate_deployment_preference_and_add_update_policy(
830-
self, deployment_preference_collection, lambda_alias, intrinsics_resolver, mappings_resolver
831+
self, deployment_preference_collection, lambda_alias, intrinsics_resolver, mappings_resolver, condition
831832
):
832833
if "Enabled" in self.DeploymentPreference:
833834
# resolve intrinsics and mappings for Type
@@ -843,10 +844,15 @@ def _validate_deployment_preference_and_add_update_policy(
843844
preference_type = mappings_resolver.resolve_parameter_refs(preference_type)
844845
self.DeploymentPreference["Type"] = preference_type
845846

847+
if condition:
848+
condition = condition.get("Condition")
849+
846850
if deployment_preference_collection is None:
847851
raise ValueError("deployment_preference_collection required for parsing the deployment preference")
848852

849-
deployment_preference_collection.add(self.logical_id, self.DeploymentPreference)
853+
deployment_preference_collection.add(
854+
self.logical_id, self.DeploymentPreference, condition,
855+
)
850856

851857
if deployment_preference_collection.get(self.logical_id).enabled:
852858
if self.AutoPublishAlias is None:

tests/translator/model/preferences/test_deployment_preference.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def setUp(self):
1616
"TriggerTargetArn": {"Ref": "MySNSTopic"},
1717
"TriggerName": "TestTrigger",
1818
}
19+
self.condition = "condition"
1920
self.expected_deployment_preference = DeploymentPreference(
2021
self.deployment_type,
2122
self.pre_traffic_hook,
@@ -24,6 +25,7 @@ def setUp(self):
2425
True,
2526
self.role,
2627
self.trigger_configurations,
28+
self.condition,
2729
)
2830

2931
def test_from_dict_with_intrinsic_function_type(self):
@@ -37,6 +39,7 @@ def test_from_dict_with_intrinsic_function_type(self):
3739
True,
3840
self.role,
3941
self.trigger_configurations,
42+
self.condition,
4043
)
4144

4245
deployment_preference_yaml_dict = dict()
@@ -49,7 +52,7 @@ def test_from_dict_with_intrinsic_function_type(self):
4952
deployment_preference_yaml_dict["Role"] = self.role
5053
deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations
5154
deployment_preference_from_yaml_dict = DeploymentPreference.from_dict(
52-
"logical_id", deployment_preference_yaml_dict
55+
"logical_id", deployment_preference_yaml_dict, self.condition
5356
)
5457

5558
self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
@@ -65,13 +68,13 @@ def test_from_dict(self):
6568
deployment_preference_yaml_dict["Role"] = self.role
6669
deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations
6770
deployment_preference_from_yaml_dict = DeploymentPreference.from_dict(
68-
"logical_id", deployment_preference_yaml_dict
71+
"logical_id", deployment_preference_yaml_dict, self.condition
6972
)
7073

7174
self.assertEqual(self.expected_deployment_preference, deployment_preference_from_yaml_dict)
7275

7376
def test_from_dict_with_disabled_preference_does_not_require_other_parameters(self):
74-
expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None)
77+
expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None)
7578

7679
deployment_preference_yaml_dict = dict()
7780
deployment_preference_yaml_dict["Enabled"] = False
@@ -82,7 +85,7 @@ def test_from_dict_with_disabled_preference_does_not_require_other_parameters(se
8285
self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
8386

8487
def test_from_dict_with_string_disabled_preference_does_not_require_other_parameters(self):
85-
expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None)
88+
expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None)
8689

8790
deployment_preference_yaml_dict = dict()
8891
deployment_preference_yaml_dict["Enabled"] = "False"
@@ -93,7 +96,7 @@ def test_from_dict_with_string_disabled_preference_does_not_require_other_parame
9396
self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
9497

9598
def test_from_dict_with_lowercase_string_disabled_preference_does_not_require_other_parameters(self):
96-
expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None)
99+
expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None)
97100

98101
deployment_preference_yaml_dict = dict()
99102
deployment_preference_yaml_dict["Enabled"] = "false"

tests/translator/test_function_resources.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ def test_sam_function_with_deployment_preference(self, get_resolved_alias_name_m
144144
resources = sam_func.to_cloudformation(**kwargs)
145145

146146
deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id)
147-
deployment_preference_collection.add.assert_called_once_with(self.sam_func.logical_id, deploy_preference_dict)
147+
deployment_preference_collection.add.assert_called_once_with(
148+
self.sam_func.logical_id, deploy_preference_dict, None
149+
)
148150

149151
aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type]
150152

@@ -222,7 +224,7 @@ def test_sam_function_with_disabled_deployment_preference_does_not_add_update_po
222224

223225
resources = sam_func.to_cloudformation(**kwargs)
224226

225-
preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict)
227+
preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict, None)
226228
preference_collection.get.assert_called_once_with(sam_func.logical_id)
227229
self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled)
228230
aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type]
@@ -318,7 +320,9 @@ def test_sam_function_with_deployment_preference_intrinsic_ref_enabled_boolean_p
318320
resources = sam.to_cloudformation(**kwargs)
319321

320322
deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id)
321-
deployment_preference_collection.add.assert_called_once_with(self.sam_func.logical_id, deploy_preference_dict)
323+
deployment_preference_collection.add.assert_called_once_with(
324+
self.sam_func.logical_id, deploy_preference_dict, None
325+
)
322326
self.intrinsics_resolver_mock.resolve_parameter_refs.assert_any_call(enabled)
323327

324328
aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type]

0 commit comments

Comments
 (0)