3
3
import json
4
4
import boto3
5
5
import logging
6
+ import hashlib
6
7
7
8
from botocore .config import Config
9
+ from samtranslator .feature_toggle .dialup import (
10
+ DisabledDialup ,
11
+ ToggleDialup ,
12
+ SimpleAccountPercentileDialup ,
13
+ )
8
14
9
15
my_path = os .path .dirname (os .path .abspath (__file__ ))
10
16
sys .path .insert (0 , my_path + "/.." )
@@ -18,50 +24,69 @@ class FeatureToggle:
18
24
SAM is executing or not.
19
25
"""
20
26
21
- def __init__ (self , config_provider ):
27
+ DIALUP_RESOLVER = {
28
+ "toggle" : ToggleDialup ,
29
+ "account-percentile" : SimpleAccountPercentileDialup ,
30
+ }
31
+
32
+ def __init__ (self , config_provider , stage , account_id , region ):
22
33
self .feature_config = config_provider .config
34
+ self .stage = stage
35
+ self .account_id = account_id
36
+ self .region = region
23
37
24
- def is_enabled_for_stage_in_region (self , feature_name , stage , region = "default" ):
38
+ def _get_dialup (self , region_config , feature_name ):
25
39
"""
26
- To check if feature is available for a particular stage or not.
27
- :param feature_name: name of feature
28
- :param stage: stage where SAM is running
29
- :param region: region in which SAM is running
30
- :return:
40
+ get the right dialup instance
41
+ if no dialup type is provided or the specified dialup is not supported,
42
+ an instance of DisabledDialup will be returned
43
+
44
+ :param region_config: region config
45
+ :param feature_name: feature_name
46
+ :return: an instance of
31
47
"""
32
- if feature_name not in self .feature_config :
33
- LOG .warning ("Feature '{}' not available in Feature Toggle Config." .format (feature_name ))
34
- return False
35
- stage_config = self .feature_config .get (feature_name , {}).get (stage , {})
36
- if not stage_config :
37
- LOG .info ("Stage '{}' not enabled for Feature '{}'." .format (stage , feature_name ))
38
- return False
39
- region_config = stage_config .get (region , {}) if region in stage_config else stage_config .get ("default" , {})
40
- is_enabled = region_config .get ("enabled" , False )
41
- LOG .info ("Feature '{}' is enabled: '{}'" .format (feature_name , is_enabled ))
42
- return is_enabled
48
+ dialup_type = region_config .get ("type" )
49
+ if dialup_type in FeatureToggle .DIALUP_RESOLVER :
50
+ return FeatureToggle .DIALUP_RESOLVER [dialup_type ](
51
+ region_config , account_id = self .account_id , feature_name = feature_name
52
+ )
53
+ LOG .warning ("Dialup type '{}' is None or is not supported." .format (dialup_type ))
54
+ return DisabledDialup (region_config )
43
55
44
- def is_enabled_for_account_in_region (self , feature_name , stage , account_id , region = "default" ):
56
+ def is_enabled (self , feature_name ):
45
57
"""
46
- To check if feature is available for a particular account or not.
58
+ To check if feature is available
59
+
47
60
:param feature_name: name of feature
48
- :param stage: stage where SAM is running
49
- :param account_id: account_id who is executing SAM template
50
- :param region: region in which SAM is running
51
- :return:
52
61
"""
53
62
if feature_name not in self .feature_config :
54
63
LOG .warning ("Feature '{}' not available in Feature Toggle Config." .format (feature_name ))
55
64
return False
65
+
66
+ stage = self .stage
67
+ region = self .region
68
+ account_id = self .account_id
69
+ if not stage or not region or not account_id :
70
+ LOG .warning (
71
+ "One or more of stage, region and account_id is not set. Feature '{}' not enabled." .format (feature_name )
72
+ )
73
+ return False
74
+
56
75
stage_config = self .feature_config .get (feature_name , {}).get (stage , {})
57
76
if not stage_config :
58
77
LOG .info ("Stage '{}' not enabled for Feature '{}'." .format (stage , feature_name ))
59
78
return False
60
- account_config = stage_config .get (account_id ) if account_id in stage_config else stage_config .get ("default" , {})
61
- region_config = (
62
- account_config .get (region , {}) if region in account_config else account_config .get ("default" , {})
63
- )
64
- is_enabled = region_config .get ("enabled" , False )
79
+
80
+ if account_id in stage_config :
81
+ account_config = stage_config [account_id ]
82
+ region_config = account_config [region ] if region in account_config else account_config .get ("default" , {})
83
+ else :
84
+ region_config = stage_config [region ] if region in stage_config else stage_config .get ("default" , {})
85
+
86
+ dialup = self ._get_dialup (region_config , feature_name = feature_name )
87
+ LOG .info ("Using Dialip {}" .format (dialup ))
88
+ is_enabled = dialup .is_enabled ()
89
+
65
90
LOG .info ("Feature '{}' is enabled: '{}'" .format (feature_name , is_enabled ))
66
91
return is_enabled
67
92
0 commit comments