Skip to content

Commit 082e9b2

Browse files
committed
feat(Strategy): Add Team ID based strategy
Plus some cleanup.
1 parent 8bd0631 commit 082e9b2

File tree

8 files changed

+70
-9
lines changed

8 files changed

+70
-9
lines changed

FeatureToggle/__init__.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
from UnleashClient.utils import LOGGER
1010

1111

12+
def split_and_strip(parameters: str):
13+
return [
14+
x.strip() for x in parameters.split(',')
15+
]
16+
17+
1218
class FeatureToggles:
1319
__client = None
1420
__url = None
@@ -208,6 +214,29 @@ def is_enabled_for_expert(feature_name: str,
208214
return FeatureToggles.__get_unleash_client().is_enabled(feature_name,
209215
context)
210216

217+
@staticmethod
218+
def is_enabled_for_team(feature_name: str,
219+
team_id: Optional[int] = None):
220+
"""
221+
Util method to check whether given feature is enabled or not
222+
Args:
223+
feature_name(str): feature name
224+
team_id(Optional[str]): list of team IDs
225+
Returns:
226+
(bool): True if feature is enabled else False
227+
"""
228+
feature_name = FeatureToggles.__get_full_feature_name(feature_name)
229+
230+
context = {}
231+
if team_id:
232+
context['team_ids'] = team_id
233+
234+
return (
235+
FeatureToggles
236+
.__get_unleash_client()
237+
.is_enabled(feature_name, context)
238+
)
239+
211240
@staticmethod
212241
def fetch_feature_toggles():
213242
"""
@@ -247,6 +276,7 @@ def fetch_feature_toggles():
247276
partner_names = []
248277
business_via_names = []
249278
expert_emails = []
279+
team_ids = []
250280

251281
if cas_name == FeatureToggles.__cas_name and environment == FeatureToggles.__environment:
252282
# Strip CAS and ENV name from feature name
@@ -260,14 +290,15 @@ def fetch_feature_toggles():
260290
strategy_name = strategy.get('name', '')
261291
parameters = strategy.get('parameters', {})
262292
if strategy_name == 'EnableForPartners':
263-
partner_names = parameters.get('partner_names', '').replace(', ', ',').split(',')
264-
293+
partner_names = split_and_strip(parameters.get('partner_names', ''))
265294
elif strategy_name == 'EnableForBusinesses':
266-
business_via_names = parameters.get('business_via_names', '').replace(', ', ',').split(',')
295+
business_via_names = split_and_strip(parameters.get('business_via_names', ''))
267296
elif strategy_name == 'EnableForDomains':
268-
domain_names = parameters.get('domain_names', '').replace(', ', ',').split(',')
297+
domain_names = split_and_strip(parameters.get('domain_names', ''))
269298
elif strategy_name == 'EnableForExperts':
270-
expert_emails = parameters.get('expert_emails', '').replace(', ', ',').split(',')
299+
expert_emails = split_and_strip(parameters.get('expert_emails', ''))
300+
elif strategy_name == 'EnableForTeams':
301+
team_ids = split_and_strip(parameters.get('team_ids', ''))
271302

272303
# Keep updating this list for new strategies which gets added
273304

@@ -276,8 +307,9 @@ def fetch_feature_toggles():
276307
response[full_feature_name]['business_via_names'] = business_via_names
277308
response[full_feature_name]['domain_names'] = domain_names
278309
response[full_feature_name]['expert_emails'] = expert_emails
310+
response[full_feature_name]['team_ids'] = team_ids
279311
except Exception as err:
280312
# Handle this exception from where this util gets called
281-
raise Exception(f'Error occured while parsing the response: {str(err)}')
313+
raise Exception(f'An error occurred while parsing the response: {str(err)}')
282314

283-
return response
315+
return response

UnleashClient/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
GradualRolloutSessionId, GradualRolloutUserId, UserWithId, RemoteAddress, FlexibleRollout, \
88
EnableForDomains, EnableForBusinesses, EnableForPartners, EnableForExperts
99
from UnleashClient import constants as consts
10+
from UnleashClient.strategies.EnableForTeamStrategy import EnableForTeams
1011
from UnleashClient.utils import LOGGER
1112
from UnleashClient.loader import load_features
1213
from UnleashClient.deprecation_warnings import strategy_v2xx_deprecation_check, default_value_warning
1314

1415

1516
# pylint: disable=dangerous-default-value
16-
class UnleashClient():
17+
class UnleashClient:
1718
"""
1819
Client implementation.
1920
"""
@@ -85,7 +86,8 @@ def __init__(self,
8586
"EnableForDomains": EnableForDomains,
8687
"EnableForExperts": EnableForExperts,
8788
"EnableForPartners": EnableForPartners,
88-
"EnableForBusinesses": EnableForBusinesses
89+
"EnableForBusinesses": EnableForBusinesses,
90+
"EnableForTeams": EnableForTeams
8991
}
9092

9193
if custom_strategies:

UnleashClient/strategies/EnableForBusinessStrategy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from UnleashClient.strategies import Strategy
22

3+
34
class EnableForBusinesses(Strategy):
45
def load_provisioning(self) -> list:
56
return [

UnleashClient/strategies/EnableForDomainStrategy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from UnleashClient.strategies import Strategy
22

3+
34
class EnableForDomains(Strategy):
45
def load_provisioning(self) -> list:
56
return [x.strip() for x in self.parameters["domain_names"].split(',')]

UnleashClient/strategies/EnableForExpertStrategy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from UnleashClient.strategies import Strategy
22

3+
34
class EnableForExperts(Strategy):
45
def load_provisioning(self) -> list:
56
return [x.strip() for x in self.parameters["expert_emails"].split(',')]

UnleashClient/strategies/EnableForPartnerStrategy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
from UnleashClient.strategies import Strategy
33

4+
45
class EnableForPartners(Strategy):
56
def load_provisioning(self) -> list:
67
return [
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from UnleashClient.strategies import Strategy
2+
3+
4+
class EnableForTeams(Strategy):
5+
def load_provisioning(self) -> list:
6+
return [
7+
x.strip() for x in self.parameters["team_ids"].split(',')
8+
]
9+
10+
def apply(self, context: dict = None) -> bool:
11+
"""
12+
Check if feature is enabled for given team or not
13+
14+
Args:
15+
context(dict): team IDs provided as context
16+
"""
17+
default_value = False
18+
19+
if "team_ids" in context.keys():
20+
default_value = context["team_ids"] in self.parsed_provisioning
21+
22+
return default_value

UnleashClient/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
LOGGER = logging.getLogger(__name__)
66

7+
78
def normalized_hash(identifier: str,
89
activation_group: str,
910
normalizer: int = 100) -> int:

0 commit comments

Comments
 (0)