Skip to main content

uptrakit_openapi_client/
plugin_configs.rs

1use crate::Result;
2use crate::UptrakitClient;
3use crate::types_impl::batch_actions::{BatchActionRequest, BatchActionResponse};
4use crate::types_impl::pagination::{PaginatedResponse, PaginationParams};
5use crate::types_impl::plugin_configs::{
6    CreatePluginConfigRequest, PluginConfigResponse, PluginTypeInfo, UpdatePluginConfigRequest,
7};
8use uuid::Uuid;
9
10impl UptrakitClient {
11    /// List all known plugin types with their display names and capabilities.
12    ///
13    /// Returns static registry metadata. Use this to populate plugin-type
14    /// selectors rather than hard-coding plugin type strings.
15    pub async fn list_plugin_types(&self) -> Result<Vec<PluginTypeInfo>> {
16        self.get(crate::paths::plugin_configs::PLUGIN_TYPES).await
17    }
18
19    /// Create a new plugin configuration.
20    pub async fn create_plugin_config(
21        &self,
22        req: &CreatePluginConfigRequest,
23    ) -> Result<PluginConfigResponse> {
24        self.post_json(crate::paths::plugin_configs::BASE, req)
25            .await
26    }
27
28    /// List plugin configurations with pagination.
29    pub async fn list_plugin_configs(
30        &self,
31        params: &PaginationParams,
32    ) -> Result<PaginatedResponse<PluginConfigResponse>> {
33        self.get_with_query(crate::paths::plugin_configs::BASE, params)
34            .await
35    }
36
37    /// Fetch all plugin configurations across all pages.
38    ///
39    /// Automatically iterates through every page at [`MAX_PER_PAGE`] items per
40    /// request. Use [`list_plugin_configs`] for manual pagination control.
41    ///
42    /// [`MAX_PER_PAGE`]: uptrakit_web_api_types::pagination::MAX_PER_PAGE
43    /// [`list_plugin_configs`]: Self::list_plugin_configs
44    pub async fn list_all_plugin_configs(&self) -> Result<Vec<PluginConfigResponse>> {
45        let base = PaginationParams {
46            page: None,
47            per_page: None,
48        };
49        self.fetch_all_pages(crate::paths::plugin_configs::BASE, &base)
50            .await
51    }
52
53    /// Get a single plugin configuration by ID.
54    pub async fn get_plugin_config(&self, id: &Uuid) -> Result<PluginConfigResponse> {
55        self.get(&crate::paths::plugin_configs::by_id(id)).await
56    }
57
58    /// Update an existing plugin configuration.
59    pub async fn update_plugin_config(
60        &self,
61        id: &Uuid,
62        req: &UpdatePluginConfigRequest,
63    ) -> Result<PluginConfigResponse> {
64        self.put_json(&crate::paths::plugin_configs::by_id(id), req)
65            .await
66    }
67
68    /// Delete a plugin configuration.
69    pub async fn delete_plugin_config(&self, id: &Uuid) -> Result<()> {
70        self.delete(&crate::paths::plugin_configs::by_id(id)).await
71    }
72
73    /// Perform a batch action on multiple plugin configurations.
74    ///
75    /// Supported actions: `delete`.
76    pub async fn batch_plugin_configs(
77        &self,
78        req: &BatchActionRequest,
79    ) -> Result<BatchActionResponse> {
80        self.post_json(crate::paths::plugin_configs::BATCH, req)
81            .await
82    }
83
84    /// Test a plugin configuration without saving it.
85    ///
86    /// Validates the configuration and, depending on the plugin type, either
87    /// performs a controller-side connectivity check or routes the request to
88    /// an agent for host-side validation.
89    pub async fn test_plugin_config(
90        &self,
91        req: &crate::types_impl::plugin_config_test::TestPluginConfigRequest,
92    ) -> Result<crate::types_impl::plugin_config_test::TestPluginConfigResponse> {
93        self.post_json(crate::paths::plugin_configs::TEST, req)
94            .await
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use crate::shared_types_impl::plugin_ids;
101    use crate::types_impl::pagination::PaginationParams;
102    use crate::types_impl::plugin_configs::{CreatePluginConfigRequest, UpdatePluginConfigRequest};
103
104    #[test]
105    fn create_plugin_config_request_serialization() {
106        let req = CreatePluginConfigRequest {
107            name: "GitHub Releases".to_string(),
108            plugin_type: plugin_ids::RELEASES_GITHUB.clone(),
109            config: serde_json::json!({"tag_strip_prefix": "v", "include_prereleases": false}),
110            enabled: true,
111        };
112        let json = serde_json::to_value(&req).expect("serialize");
113        assert_eq!(json["name"], "GitHub Releases");
114        assert_eq!(json["plugin_type"], "releases_github");
115        assert_eq!(json["config"]["tag_strip_prefix"], "v");
116        assert_eq!(json["enabled"], true);
117    }
118
119    #[test]
120    fn update_plugin_config_request_serialization() {
121        let req = UpdatePluginConfigRequest {
122            name: Some("Updated Config".to_string()),
123            config: Some(serde_json::json!({"key": "value"})),
124            enabled: Some(false),
125        };
126        let json = serde_json::to_value(&req).expect("serialize");
127        assert_eq!(json["name"], "Updated Config");
128        assert_eq!(json["config"]["key"], "value");
129        assert_eq!(json["enabled"], false);
130    }
131
132    #[test]
133    fn pagination_params_for_plugin_configs() {
134        let params = PaginationParams {
135            page: Some(1),
136            per_page: Some(25),
137        };
138        let qs = serde_urlencoded::to_string(&params).expect("serialize");
139        assert!(qs.contains("page=1"));
140        assert!(qs.contains("per_page=25"));
141    }
142}