Skip to content

Commit 6ec9a8f

Browse files
author
Ran Isenberg
committed
rename restriction to condition
1 parent dffbe7c commit 6ec9a8f

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

aws_lambda_powertools/utilities/feature_toggles/configuration_store.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(
3232
self._schema_validator = schema.SchemaValidator(self._logger)
3333
self._conf_store = AppConfigProvider(environment=environment, application=service, config=config)
3434

35-
def _match_by_action(self, action: str, restriction_value: Any, context_value: Any) -> bool:
35+
def _match_by_action(self, action: str, CONDITION_VALUE: Any, context_value: Any) -> bool:
3636
if not context_value:
3737
return False
3838
mapping_by_action = {
@@ -44,29 +44,29 @@ def _match_by_action(self, action: str, restriction_value: Any, context_value: A
4444

4545
try:
4646
func = mapping_by_action.get(action, lambda a, b: False)
47-
return func(context_value, restriction_value)
47+
return func(context_value, CONDITION_VALUE)
4848
except Exception as exc:
4949
self._logger.error(f"caught exception while matching action, action={action}, exception={str(exc)}")
5050
return False
5151

5252
def _is_rule_matched(self, feature_name: str, rule: Dict[str, Any], rules_context: Dict[str, Any]) -> bool:
5353
rule_name = rule.get(schema.RULE_NAME_KEY, "")
5454
rule_default_value = rule.get(schema.RULE_DEFAULT_VALUE)
55-
restrictions: Dict[str, str] = rule.get(schema.RESTRICTIONS_KEY)
55+
conditions: Dict[str, str] = rule.get(schema.CONDITIONS_KEY)
5656

57-
for restriction in restrictions:
58-
context_value = rules_context.get(restriction.get(schema.RESTRICTION_KEY))
57+
for condition in conditions:
58+
context_value = rules_context.get(condition.get(schema.CONDITION_KEY))
5959
if not self._match_by_action(
60-
restriction.get(schema.RESTRICTION_ACTION),
61-
restriction.get(schema.RESTRICTION_VALUE),
60+
condition.get(schema.CONDITION_ACTION),
61+
condition.get(schema.CONDITION_VALUE),
6262
context_value,
6363
):
6464
logger.debug(
6565
f"rule did not match action, rule_name={rule_name}, rule_default_value={rule_default_value}, feature_name={feature_name}, context_value={str(context_value)}" # noqa: E501
6666
)
67-
# context doesn't match restriction
67+
# context doesn't match condition
6868
return False
69-
# if we got here, all restrictions match
69+
# if we got here, all conditions match
7070
logger.debug(
7171
f"rule matched, rule_name={rule_name}, rule_default_value={rule_default_value}, feature_name={feature_name}" # noqa: E501
7272
)

aws_lambda_powertools/utilities/feature_toggles/schema.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
FEATURES_KEY = "features"
77
RULES_KEY = "rules"
88
FEATURE_DEFAULT_VAL_KEY = "feature_default_value"
9-
RESTRICTIONS_KEY = "restrictions"
9+
CONDITIONS_KEY = "conditions"
1010
RULE_NAME_KEY = "rule_name"
1111
RULE_DEFAULT_VALUE = "value_when_applies"
12-
RESTRICTION_KEY = "key"
13-
RESTRICTION_VALUE = "value"
14-
RESTRICTION_ACTION = "action"
12+
CONDITION_KEY = "key"
13+
CONDITION_VALUE = "value"
14+
CONDITION_ACTION = "action"
1515

1616

1717
class ACTION(str, Enum):
@@ -29,18 +29,18 @@ def _raise_conf_exc(self, error_str: str) -> None:
2929
self._logger.error(error_str)
3030
raise ConfigurationException(error_str)
3131

32-
def _validate_restriction(self, rule_name: str, restriction: Dict[str, str]) -> None:
33-
if not restriction or not isinstance(restriction, dict):
34-
self._raise_conf_exc(f"invalid restriction type, not a dictionary, rule_name={rule_name}")
35-
action = restriction.get(RESTRICTION_ACTION, "")
32+
def _validate_condition(self, rule_name: str, condition: Dict[str, str]) -> None:
33+
if not condition or not isinstance(condition, dict):
34+
self._raise_conf_exc(f"invalid condition type, not a dictionary, rule_name={rule_name}")
35+
action = condition.get(CONDITION_ACTION, "")
3636
if action not in [ACTION.EQUALS.value, ACTION.STARTSWITH.value, ACTION.ENDSWITH.value, ACTION.CONTAINS.value]:
3737
self._raise_conf_exc(f"invalid action value, rule_name={rule_name}, action={action}")
38-
key = restriction.get(RESTRICTION_KEY, "")
38+
key = condition.get(CONDITION_KEY, "")
3939
if not key or not isinstance(key, str):
4040
self._raise_conf_exc(f"invalid key value, key has to be a non empty string, rule_name={rule_name}")
41-
value = restriction.get(RESTRICTION_VALUE, "")
41+
value = condition.get(CONDITION_VALUE, "")
4242
if not value:
43-
self._raise_conf_exc(f"missing restriction value, rule_name={rule_name}")
43+
self._raise_conf_exc(f"missing condition value, rule_name={rule_name}")
4444

4545
def _validate_rule(self, feature_name: str, rule: Dict[str, Any]) -> None:
4646
if not rule or not isinstance(rule, dict):
@@ -51,12 +51,12 @@ def _validate_rule(self, feature_name: str, rule: Dict[str, Any]) -> None:
5151
rule_default_value = rule.get(RULE_DEFAULT_VALUE)
5252
if rule_default_value is None or not isinstance(rule_default_value, bool):
5353
self._raise_conf_exc(f"invalid rule_default_value, rule_name={rule_name}")
54-
restrictions = rule.get(RESTRICTIONS_KEY, {})
55-
if not restrictions or not isinstance(restrictions, list):
56-
self._raise_conf_exc(f"invalid restrictions, rule_name={rule_name}")
57-
# validate restrictions
58-
for restriction in restrictions:
59-
self._validate_restriction(rule_name, restriction)
54+
conditions = rule.get(CONDITIONS_KEY, {})
55+
if not conditions or not isinstance(conditions, list):
56+
self._raise_conf_exc(f"invalid condition, rule_name={rule_name}")
57+
# validate conditions
58+
for condition in conditions:
59+
self._validate_condition(rule_name, condition)
6060

6161
def _validate_feature(self, feature_name: str, feature_dict_def: Dict[str, Any]) -> None:
6262
if not feature_dict_def or not isinstance(feature_dict_def, dict):

tests/functional/feature_toggles/test_feature_toggles.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_toggles_rule_does_not_match(mocker, config):
3737
{
3838
"rule_name": "tenant id equals 345345435",
3939
"value_when_applies": False,
40-
"restrictions": [
40+
"conditions": [
4141
{
4242
"action": ACTION.EQUALS.value,
4343
"key": "tenant_id",
@@ -57,7 +57,7 @@ def test_toggles_rule_does_not_match(mocker, config):
5757

5858
# this test checks that if you try to get a feature that doesn't exist in the schema,
5959
# you get the default value of False that was sent to the get_feature_toggle API
60-
def test_toggles_no_restrictions_feature_does_not_exist(mocker, config):
60+
def test_toggles_no_conditions_feature_does_not_exist(mocker, config):
6161
expected_value = False
6262
mocked_app_config_schema = {"features": {"my_fake_feature": {"feature_default_value": True}}}
6363

@@ -79,7 +79,7 @@ def test_toggles_no_rules(mocker, config):
7979

8080

8181
# check a case where the feature exists but the rule doesn't match so we revert to the default value of the feature
82-
def test_toggles_restrictions_no_match(mocker, config):
82+
def test_toggles_conditions_no_match(mocker, config):
8383
expected_value = True
8484
mocked_app_config_schema = {
8585
"features": {
@@ -89,7 +89,7 @@ def test_toggles_restrictions_no_match(mocker, config):
8989
{
9090
"rule_name": "tenant id equals 345345435",
9191
"value_when_applies": False,
92-
"restrictions": [
92+
"conditions": [
9393
{
9494
"action": ACTION.EQUALS.value,
9595
"key": "tenant_id",
@@ -110,8 +110,8 @@ def test_toggles_restrictions_no_match(mocker, config):
110110
assert toggle == expected_value
111111

112112

113-
# check that a rule can match when it has multiple restrictions, see rule name for further explanation
114-
def test_toggles_restrictions_rule_match_equal_multiple_restrictions(mocker, config):
113+
# check that a rule can match when it has multiple conditions, see rule name for further explanation
114+
def test_toggles_conditions_rule_match_equal_multiple_conditions(mocker, config):
115115
expected_value = False
116116
tenant_id_val = "6"
117117
username_val = "a"
@@ -123,9 +123,9 @@ def test_toggles_restrictions_rule_match_equal_multiple_restrictions(mocker, con
123123
{
124124
"rule_name": "tenant id equals 6 and username is a",
125125
"value_when_applies": expected_value,
126-
"restrictions": [
126+
"conditions": [
127127
{
128-
"action": ACTION.EQUALS.value, # this rule will match, it has multiple restrictions
128+
"action": ACTION.EQUALS.value, # this rule will match, it has multiple conditions
129129
"key": "tenant_id",
130130
"value": tenant_id_val,
131131
},
@@ -152,10 +152,10 @@ def test_toggles_restrictions_rule_match_equal_multiple_restrictions(mocker, con
152152
assert toggle == expected_value
153153

154154

155-
# check a case when rule doesn't match and it has multiple restrictions,
155+
# check a case when rule doesn't match and it has multiple conditions,
156156
# different tenant id causes the rule to not match.
157157
# default value of the feature in this case is True
158-
def test_toggles_restrictions_no_rule_match_equal_multiple_restrictions(mocker, config):
158+
def test_toggles_conditions_no_rule_match_equal_multiple_conditions(mocker, config):
159159
expected_val = True
160160
mocked_app_config_schema = {
161161
"features": {
@@ -165,7 +165,7 @@ def test_toggles_restrictions_no_rule_match_equal_multiple_restrictions(mocker,
165165
{
166166
"rule_name": "tenant id equals 645654 and username is a", # rule will not match
167167
"value_when_applies": False,
168-
"restrictions": [
168+
"conditions": [
169169
{
170170
"action": ACTION.EQUALS.value,
171171
"key": "tenant_id",
@@ -190,7 +190,7 @@ def test_toggles_restrictions_no_rule_match_equal_multiple_restrictions(mocker,
190190

191191

192192
# check rule match for multiple of action types
193-
def test_toggles_restrictions_rule_match_multiple_actions_multiple_rules_multiple_restrictions(mocker, config):
193+
def test_toggles_conditions_rule_match_multiple_actions_multiple_rules_multiple_conditions(mocker, config):
194194
expected_value_first_check = True
195195
expected_value_second_check = False
196196
expected_value_third_check = False
@@ -203,7 +203,7 @@ def test_toggles_restrictions_rule_match_multiple_actions_multiple_rules_multipl
203203
{
204204
"rule_name": "tenant id equals 6 and username startswith a",
205205
"value_when_applies": expected_value_first_check,
206-
"restrictions": [
206+
"conditions": [
207207
{
208208
"action": ACTION.EQUALS.value,
209209
"key": "tenant_id",
@@ -219,7 +219,7 @@ def test_toggles_restrictions_rule_match_multiple_actions_multiple_rules_multipl
219219
{
220220
"rule_name": "tenant id equals 4446 and username startswith a and endswith z",
221221
"value_when_applies": expected_value_second_check,
222-
"restrictions": [
222+
"conditions": [
223223
{
224224
"action": ACTION.EQUALS.value,
225225
"key": "tenant_id",
@@ -284,7 +284,7 @@ def test_toggles_match_rule_with_contains_action(mocker, config):
284284
{
285285
"rule_name": "tenant id is contained in [6,2] ",
286286
"value_when_applies": expected_value,
287-
"restrictions": [
287+
"conditions": [
288288
{
289289
"action": ACTION.CONTAINS.value,
290290
"key": "tenant_id",
@@ -315,7 +315,7 @@ def test_toggles_no_match_rule_with_contains_action(mocker, config):
315315
{
316316
"rule_name": "tenant id is contained in [6,2] ",
317317
"value_when_applies": True,
318-
"restrictions": [
318+
"conditions": [
319319
{
320320
"action": ACTION.CONTAINS.value,
321321
"key": "tenant_id",
@@ -346,7 +346,7 @@ def test_multiple_features_enabled(mocker, config):
346346
{
347347
"rule_name": "tenant id is contained in [6,2] ",
348348
"value_when_applies": True,
349-
"restrictions": [
349+
"conditions": [
350350
{
351351
"action": ACTION.CONTAINS.value,
352352
"key": "tenant_id",
@@ -378,7 +378,7 @@ def test_multiple_features_only_some_enabled(mocker, config):
378378
{
379379
"rule_name": "tenant id is contained in [6,2] ",
380380
"value_when_applies": True,
381-
"restrictions": [
381+
"conditions": [
382382
{
383383
"action": ACTION.CONTAINS.value,
384384
"key": "tenant_id",
@@ -400,7 +400,7 @@ def test_multiple_features_only_some_enabled(mocker, config):
400400
{
401401
"rule_name": "tenant id equals 7",
402402
"value_when_applies": False,
403-
"restrictions": [
403+
"conditions": [
404404
{
405405
"action": ACTION.EQUALS.value,
406406
"key": "tenant_id",

0 commit comments

Comments
 (0)