Skip to content

feat(feature_flags): add in feature_flags RE_* actions and update tests/docs #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from typing import Any, Dict, List, Optional, Union, cast

from . import schema
Expand Down Expand Up @@ -48,6 +49,12 @@ def _match_by_action(action: str, condition_value: Any, context_value: Any) -> b
schema.RuleAction.ENDSWITH.value: lambda a, b: a.endswith(b),
schema.RuleAction.IN.value: lambda a, b: a in b,
schema.RuleAction.NOT_IN.value: lambda a, b: a not in b,
schema.RuleAction.RE_MATCH.value: lambda a, b: bool(re.match(b, a)),
schema.RuleAction.RE_MATCH_IGNORECASE.value: lambda a, b: bool(re.match(b, a, flags=re.IGNORECASE)),
schema.RuleAction.RE_FULLMATCH.value: lambda a, b: bool(re.fullmatch(b, a)),
schema.RuleAction.RE_FULLMATCH_IGNORECASE.value: lambda a, b: bool(re.fullmatch(b, a, flags=re.IGNORECASE)),
schema.RuleAction.RE_SEARCH.value: lambda a, b: bool(re.search(b, a)),
schema.RuleAction.RE_SEARCH_IGNORECASE.value: lambda a, b: bool(re.search(b, a, flags=re.IGNORECASE)),
}

try:
Expand Down
6 changes: 6 additions & 0 deletions aws_lambda_powertools/utilities/feature_flags/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class RuleAction(str, Enum):
ENDSWITH = "ENDSWITH"
IN = "IN"
NOT_IN = "NOT_IN"
RE_MATCH = "RE_MATCH"
RE_MATCH_IGNORECASE = "RE_MATCH_IGNORECASE"
RE_FULLMATCH = "RE_FULLMATCH"
RE_FULLMATCH_IGNORECASE = "RE_FULLMATCH_IGNORECASE"
RE_SEARCH = "RE_SEARCH"
RE_SEARCH_IGNORECASE = "RE_SEARCH_IGNORECASE"


class SchemaValidator(BaseValidator):
Expand Down
4 changes: 2 additions & 2 deletions docs/utilities/feature_flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,9 @@ The `conditions` block is a list of conditions that contain `action`, `key`, and
}
```

The `action` configuration can have 5 different values: `EQUALS`, `STARTSWITH`, `ENDSWITH`, `IN`, `NOT_IN`.
The `action` configuration can have 5 different values: `EQUALS`, `STARTSWITH`, `ENDSWITH`, `IN`, `NOT_IN`, `RE_MATCH`, `RE_MATCH_IGNORECASE`, `RE_FULLMATCH`, `RE_FULLMATCH_IGNORECASE`, `RE_SEARCH`, `RE_SEARCH_IGNORECASE`.

The `key` and `value` will be compared to the input from the context parameter.
The `key` and `value` will be compared to the input from the context parameter. The `value` should be the regular expression pattern to use when the `action` is any of the `RE_*` actions listed above.

**For multiple conditions**, we will evaluate the list of conditions as a logical `AND`, so all conditions needs to match to return `when_match` value.

Expand Down
99 changes: 99 additions & 0 deletions tests/functional/feature_flags/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,102 @@ def test_get_feature_toggle_propagates_access_denied_error(mocker, config):
# THEN raise StoreClientError error
with pytest.raises(StoreClientError, match="AccessDeniedException") as err:
feature_flags.evaluate(name="Foo", default=False)


def test_re_match_rule_does_match(mocker, config):
default_value = False
expected_value = True
email_address_val = "[email protected]"
email_address_re_val = ".*admin.*@company.com$"
mocked_app_config_schema = {
"my_feature": {
"default": default_value,
"rules": {
"email address ends with @company.com and has 'admin' in the mailbox (case insensitive)": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.RE_MATCH_IGNORECASE.value,
"key": "email_address",
"value": email_address_re_val,
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(
name="my_feature",
context={
"email_address": email_address_val,
},
default=True,
)
assert toggle == expected_value


def test_re_search_rule_does_match(mocker, config):
default_value = False
expected_value = True
email_address_val = "[email protected]"
email_address_re_val = "@company.com$"
mocked_app_config_schema = {
"my_feature": {
"default": default_value,
"rules": {
"email address ends with @company.com (case insensitive)": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.RE_SEARCH_IGNORECASE.value,
"key": "email_address",
"value": email_address_re_val,
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(
name="my_feature",
context={
"email_address": email_address_val,
},
default=True,
)
assert toggle == expected_value


def test_re_fullmatch_rule_does_not_match(mocker, config):
default_value = False
expected_value = True
email_address_val = "[email protected]"
email_address_re_val = ".*admin.*@competitor.com$"
mocked_app_config_schema = {
"my_feature": {
"default": default_value,
"rules": {
"email address ends with @company.com and has 'admin' in the mailbox (case insensitive)": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.RE_FULLMATCH_IGNORECASE.value,
"key": "email_address",
"value": email_address_re_val,
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(
name="my_feature",
context={
"email_address": email_address_val,
},
default=True,
)
assert toggle == default_value