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    /// Returns the response body and the raw `ETag` header value.
55    pub async fn get_plugin_config(&self, id: &Uuid) -> Result<(PluginConfigResponse, String)> {
56        self.get_with_etag(&crate::paths::plugin_configs::by_id(id))
57            .await
58    }
59
60    /// Update an existing plugin configuration.  `etag` must be the value returned by a prior
61    /// `get_plugin_config` call — the server requires `If-Match` for optimistic locking.
62    pub async fn update_plugin_config(
63        &self,
64        id: &Uuid,
65        req: &UpdatePluginConfigRequest,
66        etag: &str,
67    ) -> Result<(PluginConfigResponse, String)> {
68        self.put_json_with_etag(&crate::paths::plugin_configs::by_id(id), req, etag)
69            .await
70    }
71
72    /// Delete a plugin configuration.
73    pub async fn delete_plugin_config(&self, id: &Uuid) -> Result<()> {
74        self.delete(&crate::paths::plugin_configs::by_id(id)).await
75    }
76
77    /// Perform a batch action on multiple plugin configurations.
78    ///
79    /// Supported actions: `delete`.
80    pub async fn batch_plugin_configs(
81        &self,
82        req: &BatchActionRequest,
83    ) -> Result<BatchActionResponse> {
84        self.post_json(crate::paths::plugin_configs::BATCH, req)
85            .await
86    }
87
88    /// Test a plugin configuration without saving it.
89    ///
90    /// Validates the configuration and, depending on the plugin type, either
91    /// performs a controller-side connectivity check or routes the request to
92    /// an agent for host-side validation.
93    pub async fn test_plugin_config(
94        &self,
95        req: &crate::types_impl::plugin_config_test::TestPluginConfigRequest,
96    ) -> Result<crate::types_impl::plugin_config_test::TestPluginConfigResponse> {
97        self.post_json(crate::paths::plugin_configs::TEST, req)
98            .await
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use crate::shared_types_impl::plugin_ids;
105    use crate::types_impl::pagination::PaginationParams;
106    use crate::types_impl::plugin_configs::{CreatePluginConfigRequest, UpdatePluginConfigRequest};
107
108    #[test]
109    fn create_plugin_config_request_serialization() {
110        let req = CreatePluginConfigRequest {
111            name: "GitHub Releases".to_string(),
112            plugin_type: plugin_ids::RELEASES_GITHUB.clone(),
113            config: serde_json::json!({"tag_strip_prefix": "v", "include_prereleases": false}),
114            enabled: true,
115        };
116        let json = serde_json::to_value(&req).expect("serialize");
117        assert_eq!(json["name"], "GitHub Releases");
118        assert_eq!(json["plugin_type"], "releases.github");
119        assert_eq!(json["config"]["tag_strip_prefix"], "v");
120        assert_eq!(json["enabled"], true);
121    }
122
123    #[test]
124    fn update_plugin_config_request_serialization() {
125        let req = UpdatePluginConfigRequest {
126            name: Some("Updated Config".to_string()),
127            config: Some(serde_json::json!({"key": "value"})),
128            enabled: Some(false),
129        };
130        let json = serde_json::to_value(&req).expect("serialize");
131        assert_eq!(json["name"], "Updated Config");
132        assert_eq!(json["config"]["key"], "value");
133        assert_eq!(json["enabled"], false);
134    }
135
136    #[test]
137    fn pagination_params_for_plugin_configs() {
138        let params = PaginationParams {
139            page: Some(1),
140            per_page: Some(25),
141        };
142        let qs = serde_urlencoded::to_string(&params).expect("serialize");
143        assert!(qs.contains("page=1"));
144        assert!(qs.contains("per_page=25"));
145    }
146}