9797)
9898from snuba .web .rpc import RPCEndpoint , list_all_endpoint_names , run_rpc_handler
9999from snuba .web .rpc .storage_routing .routing_strategies .storage_routing import (
100+ CBRS_HASH ,
100101 BaseRoutingStrategy ,
101102)
102103from snuba .web .views import dataset_query
103- from snuba .query .allocation_policies import CAPMAN_HASH
104104
105105logger = structlog .get_logger ().bind (module = __name__ )
106106
@@ -990,7 +990,8 @@ def _add_policy_data(
990990 for policy in policies :
991991 data .append (
992992 {
993- "policy_name" : policy .config_key (),
993+ "type" : "allocation_policy" ,
994+ "name" : policy .config_key (),
994995 "configs" : policy .get_current_configs (),
995996 "optional_config_definitions" : policy .get_optional_config_definitions_json (),
996997 "query_type" : query_type ,
@@ -1024,7 +1025,7 @@ def get_allocation_policy_configs_of_routing_strategy(strategy_name: str) -> Res
10241025
10251026 policies = BaseRoutingStrategy .get_from_name (
10261027 strategy_name
1027- ).get_allocation_policies ()
1028+ )() .get_allocation_policies ()
10281029
10291030 data : list [dict [str , Any ]] = []
10301031 _add_policy_data (policies , "select" , data )
@@ -1041,13 +1042,15 @@ def get_routing_strategy_configs(strategy_name: str) -> Response:
10411042
10421043 configs = BaseRoutingStrategy .get_from_name (strategy_name )().get_configurations ()
10431044 serialized_configs = [
1044- cast (RoutingStrategyConfig , config ).to_definition_dict () for config in configs .values ()
1045+ cast (RoutingStrategyConfig , config ).to_config_dict ()
1046+ for config in configs .values ()
10451047 ]
1046- print ("isithere" )
1048+ print ("isithereRACHELITSHERE" , serialized_configs )
10471049 return Response (
10481050 json .dumps (serialized_configs ), 200 , {"Content-Type" : "application/json" }
10491051 )
10501052
1053+
10511054@application .route ("/routing_strategy_config" , methods = ["POST" , "DELETE" ])
10521055@check_tool_perms (tools = [AdminTools .CAPACITY_BASED_ROUTING_SYSTEM ])
10531056def set_routing_strategy_config () -> Response :
@@ -1056,7 +1059,7 @@ def set_routing_strategy_config() -> Response:
10561059 user = request .headers .get (USER_HEADER_KEY )
10571060
10581061 try :
1059- print ("datafsdfsdf" , data )
1062+ print ("datafsdfsdf" , data , data [ "strategy" ] )
10601063 strategy , key = (data ["strategy" ], data ["key" ])
10611064 params = data .get ("params" , {})
10621065 print ("params" , params )
@@ -1068,10 +1071,12 @@ def set_routing_strategy_config() -> Response:
10681071 assert key != "" , "Key cannot be empty string"
10691072
10701073 strategies = list (BaseRoutingStrategy .all_names ())
1074+ print ("strategies" , strategies )
10711075 strategy = next (
10721076 (s for s in strategies if s == strategy ),
10731077 None ,
10741078 )
1079+ print ("strategyyyy" , strategy )
10751080 assert strategy is not None , "Strategy not found"
10761081
10771082 except (KeyError , AssertionError ) as exc :
@@ -1087,9 +1092,94 @@ def set_routing_strategy_config() -> Response:
10871092 return Response ("" , 200 )
10881093
10891094
1090- @application .route ("/allocation_policy_config" , methods = ["POST" , "DELETE" ])
1095+ @application .route ("/allocation_policy_config_for_strategy" , methods = ["POST" , "DELETE" ])
1096+ @check_tool_perms (tools = [AdminTools .CAPACITY_MANAGEMENT ])
1097+ def set_allocation_policy_config_for_strategy () -> Response :
1098+ data = json .loads (request .data )
1099+ user = request .headers .get (USER_HEADER_KEY )
1100+
1101+ try :
1102+ strategy , key , policy_name = (data ["strategy" ], data ["key" ], data ["policy" ])
1103+ print ("dkfjlskjflks" , data )
1104+
1105+ params = data .get ("params" , {})
1106+
1107+ assert isinstance (strategy , str ), "Invalid strategy"
1108+ assert isinstance (key , str ), "Invalid key"
1109+ assert isinstance (params , dict ), "Invalid params"
1110+ assert key != "" , "Key cannot be empty string"
1111+ assert isinstance (policy_name , str ), "Invalid policy name"
1112+ strategy = BaseRoutingStrategy .get_from_name (strategy )
1113+ assert strategy is not None , "Strategy not found"
1114+ policies = (
1115+ strategy ().get_allocation_policies ()
1116+ + strategy ().get_delete_allocation_policies ()
1117+ )
1118+ policy = next (
1119+ (p for p in policies if p .config_key () == policy_name ),
1120+ None ,
1121+ )
1122+ assert policy is not None , "Policy not found on strategy"
1123+
1124+ except (KeyError , AssertionError ) as exc :
1125+ return Response (
1126+ json .dumps ({"error" : f"Invalid config: { str (exc )} " }),
1127+ 400 ,
1128+ {"Content-Type" : "application/json" },
1129+ )
1130+
1131+ if request .method == "DELETE" :
1132+ policy .delete_config_value (
1133+ config_key = key , hash = CBRS_HASH , params = params , user = user
1134+ )
1135+ audit_log .record (
1136+ user or "" ,
1137+ AuditLogAction .ALLOCATION_POLICY_DELETE ,
1138+ {
1139+ "storage" : strategy ,
1140+ "policy" : policy .config_key (),
1141+ "key" : key ,
1142+ }, # todo: make audit log handle strategy in addition to storage
1143+ notify = True ,
1144+ )
1145+ return Response ("" , 200 )
1146+ elif request .method == "POST" :
1147+ try :
1148+ value = data ["value" ]
1149+ assert isinstance (value , str ), "Invalid value"
1150+ policy .set_config_value (
1151+ config_key = key , value = value , params = params , user = user
1152+ )
1153+ audit_log .record (
1154+ user or "" ,
1155+ AuditLogAction .ALLOCATION_POLICY_UPDATE ,
1156+ {
1157+ "storage" : strategy , # todo: make audit log handle strategy in addition to storage
1158+ "policy" : policy .config_key (),
1159+ "key" : key ,
1160+ "value" : value ,
1161+ "params" : str (params ),
1162+ },
1163+ notify = True ,
1164+ )
1165+ return Response ("" , 200 )
1166+ except (KeyError , AssertionError ) as exc :
1167+ return Response (
1168+ json .dumps ({"error" : f"Invalid config: { str (exc )} " }),
1169+ 400 ,
1170+ {"Content-Type" : "application/json" },
1171+ )
1172+ else :
1173+ return Response (
1174+ json .dumps ({"error" : "Method not allowed" }),
1175+ 405 ,
1176+ {"Content-Type" : "application/json" },
1177+ )
1178+
1179+
1180+ @application .route ("/allocation_policy_config_for_storage" , methods = ["POST" , "DELETE" ])
10911181@check_tool_perms (tools = [AdminTools .CAPACITY_MANAGEMENT ])
1092- def set_allocation_policy_config () -> Response :
1182+ def set_allocation_policy_config_for_storage () -> Response :
10931183 data = json .loads (request .data )
10941184 user = request .headers .get (USER_HEADER_KEY )
10951185
@@ -1115,15 +1205,16 @@ def set_allocation_policy_config() -> Response:
11151205 assert policy is not None , "Policy not found on storage"
11161206
11171207 except (KeyError , AssertionError ) as exc :
1118- print ("4444444" )
11191208 return Response (
11201209 json .dumps ({"error" : f"Invalid config: { str (exc )} " }),
11211210 400 ,
11221211 {"Content-Type" : "application/json" },
11231212 )
11241213
11251214 if request .method == "DELETE" :
1126- policy .delete_config_value (config_key = key , hash = CAPMAN_HASH , params = params , user = user )
1215+ policy .delete_config_value (
1216+ config_key = key , hash = CAPMAN_HASH , params = params , user = user
1217+ )
11271218 audit_log .record (
11281219 user or "" ,
11291220 AuditLogAction .ALLOCATION_POLICY_DELETE ,
@@ -1152,7 +1243,6 @@ def set_allocation_policy_config() -> Response:
11521243 )
11531244 return Response ("" , 200 )
11541245 except (KeyError , AssertionError ) as exc :
1155- print ("5555555" )
11561246 return Response (
11571247 json .dumps ({"error" : f"Invalid config: { str (exc )} " }),
11581248 400 ,
0 commit comments