bios_iam/console_interface/api/
iam_ci_system_api.rs

1use bios_basic::process::task_processor::TaskProcessor;
2use bios_basic::rbum::helper::rbum_scope_helper::check_without_owner_and_unsafe_fill_ctx;
3use tardis::serde_json::Value;
4use tardis::web::context_extractor::TardisContextExtractor;
5use tardis::web::poem::Request;
6use tardis::web::poem_openapi;
7use tardis::web::poem_openapi::param::{Path, Query};
8use tardis::web::poem_openapi::payload::Json;
9use tardis::web::web_resp::{TardisApiResult, TardisResp, Void};
10
11use crate::iam_constants::{self, IAM_AVATAR};
12#[derive(Clone, Default)]
13pub struct IamCiSystemApi;
14
15/// Interface Console System API
16/// 接口控制台系统API
17///
18/// Use commas to separate multiple task ids
19/// 使用逗号分隔多个任务id
20#[poem_openapi::OpenApi(prefix_path = "/ci/system", tag = "bios_basic::ApiTag::Interface")]
21impl IamCiSystemApi {
22    /// Check if the task is finished
23    /// 检查任务是否完成
24    #[oai(path = "/task/check/:task_ids", method = "get")]
25    async fn check_finished(&self, cache_key: Query<String>, task_ids: Path<String>) -> TardisApiResult<bool> {
26        let funs = iam_constants::get_tardis_inst();
27        let task_ids = task_ids.0.split(',');
28        for task_id in task_ids {
29            let task_id = task_id.parse().map_err(|_| funs.err().format_error("system", "task", "task id format error", "406-iam-task-id-format"))?;
30            let is_finished = TaskProcessor::check_status(&cache_key.0, task_id, &funs.cache()).await?;
31            if !is_finished {
32                return TardisResp::ok(false);
33            }
34        }
35        TardisResp::ok(true)
36    }
37
38    /// Execute Task
39    /// 执行任务
40    #[oai(path = "/task/execute", method = "put")]
41    async fn execute_task(&self, cache_key: Query<String>, task_id: Query<u64>, mut ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<u64> {
42        let funs = iam_constants::get_tardis_inst();
43        check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
44        let task_id = TaskProcessor::execute_task_without_fun(
45            &cache_key.0,
46            task_id.0,
47            &funs.cache(),
48            IAM_AVATAR.to_owned(),
49            Some(vec![format!("account/{}", ctx.0.owner)]),
50        )
51        .await?;
52        TardisResp::ok(task_id)
53    }
54
55    /// Stop Task
56    /// 停止任务
57    #[oai(path = "/task/execute/stop/:task_ids", method = "delete")]
58    async fn stop_task(&self, cache_key: Query<String>, task_ids: Path<String>, mut ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Void> {
59        let funs = iam_constants::get_tardis_inst();
60        check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
61        let task_ids = task_ids.0.split(',');
62        for task_id in task_ids {
63            let task_id = task_id.parse().map_err(|_| funs.err().format_error("system", "task", "task id format error", "406-iam-task-id-format"))?;
64            TaskProcessor::stop_task_with_event(&cache_key.0, task_id, &funs.cache(), IAM_AVATAR.to_owned(), Some(vec![format!("account/{}", ctx.0.owner)])).await?;
65        }
66        TardisResp::ok(Void {})
67    }
68
69    /// Set Task Process Data
70    /// 设置任务处理数据
71    #[oai(path = "/task/process/:task_id", method = "put")]
72    async fn set_task_process_data(
73        &self,
74        cache_key: Query<String>,
75        task_id: Path<u64>,
76        data: Json<Value>,
77        mut ctx: TardisContextExtractor,
78        request: &Request,
79    ) -> TardisApiResult<Void> {
80        let funs = iam_constants::get_tardis_inst();
81        check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
82        TaskProcessor::set_process_data_with_event(
83            &cache_key.0,
84            task_id.0,
85            data.0,
86            &funs.cache(),
87            IAM_AVATAR.to_owned(),
88            Some(vec![format!("account/{}", ctx.0.owner)]),
89        )
90        .await?;
91        TardisResp::ok(Void {})
92    }
93
94    /// Get Task Process Data
95    /// 获取任务处理数据
96    #[oai(path = "/task/process/:task_id", method = "get")]
97    async fn get_task_process_data(&self, cache_key: Query<String>, task_id: Path<u64>, mut ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Value> {
98        let funs = iam_constants::get_tardis_inst();
99        check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
100        let data = TaskProcessor::get_process_data(&cache_key.0, task_id.0, &funs.cache()).await?;
101        TardisResp::ok(data)
102    }
103}