| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  | from flask_restful import Resource, reqparse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from controllers.console import api | 
					
						
							|  |  |  | from controllers.console.app.error import TracingConfigCheckError, TracingConfigIsExist, TracingConfigNotExist | 
					
						
							|  |  |  | from controllers.console.setup import setup_required | 
					
						
							|  |  |  | from controllers.console.wraps import account_initialization_required | 
					
						
							|  |  |  | from libs.login import login_required | 
					
						
							|  |  |  | from services.ops_service import OpsService | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TraceAppConfigApi(Resource): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Manage trace app configurations | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def get(self, app_id): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("tracing_provider", type=str, required=True, location="args") | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             trace_config = OpsService.get_tracing_app_config(app_id=app_id, tracing_provider=args["tracing_provider"]) | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |             if not trace_config: | 
					
						
							|  |  |  |                 return {"has_not_configured": True} | 
					
						
							|  |  |  |             return trace_config | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def post(self, app_id): | 
					
						
							|  |  |  |         """Create a new trace app configuration""" | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("tracing_provider", type=str, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("tracing_config", type=dict, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result = OpsService.create_tracing_app_config( | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 app_id=app_id, tracing_provider=args["tracing_provider"], tracing_config=args["tracing_config"] | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |             if not result: | 
					
						
							|  |  |  |                 raise TracingConfigIsExist() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             if result.get("error"): | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |                 raise TracingConfigCheckError() | 
					
						
							|  |  |  |             return result | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def patch(self, app_id): | 
					
						
							|  |  |  |         """Update an existing trace app configuration""" | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("tracing_provider", type=str, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("tracing_config", type=dict, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result = OpsService.update_tracing_app_config( | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 app_id=app_id, tracing_provider=args["tracing_provider"], tracing_config=args["tracing_config"] | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |             if not result: | 
					
						
							|  |  |  |                 raise TracingConfigNotExist() | 
					
						
							|  |  |  |             return {"result": "success"} | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def delete(self, app_id): | 
					
						
							|  |  |  |         """Delete an existing trace app configuration""" | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("tracing_provider", type=str, required=True, location="args") | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             result = OpsService.delete_tracing_app_config(app_id=app_id, tracing_provider=args["tracing_provider"]) | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |             if not result: | 
					
						
							|  |  |  |                 raise TracingConfigNotExist() | 
					
						
							|  |  |  |             return {"result": "success"} | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(TraceAppConfigApi, "/apps/<uuid:app_id>/trace-config") |