cx_sdk/client/
extensions.rs

1// Copyright 2024 Coralogix Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::str::FromStr;
16
17use crate::{
18    auth::AuthContext,
19    error::{
20        Result,
21        SdkApiError,
22    },
23    metadata::CallProperties,
24    util::make_request_with_metadata,
25};
26
27pub use crate::com::coralogixapis::actions::v2::{
28    Action,
29    SourceType,
30};
31
32use cx_api::proto::com::coralogix::extensions::v1::{
33    DeployExtensionRequest,
34    DeployExtensionResponse,
35    ExtensionDeployment,
36    GetAllExtensionsRequest,
37    GetAllExtensionsResponse,
38    GetDeployedExtensionsRequest,
39    GetDeployedExtensionsResponse,
40    GetExtensionRequest,
41    GetExtensionResponse,
42    UndeployExtensionRequest,
43    UndeployExtensionResponse,
44    UpdateExtensionRequest,
45    UpdateExtensionResponse,
46    extension_deployment_service_client::ExtensionDeploymentServiceClient,
47    extension_service_client::ExtensionServiceClient,
48};
49use tokio::sync::Mutex;
50use tonic::{
51    metadata::MetadataMap,
52    transport::{
53        Channel,
54        ClientTlsConfig,
55        Endpoint,
56    },
57};
58
59pub use crate::com::coralogix::extensions::v1::{
60    Extension,
61    TargetDomain,
62    get_all_extensions_request::Filter as ExtensionFilter,
63};
64
65use crate::CoralogixRegion;
66
67const EXTENSIONS_FEATURE_GROUP_ID: &str = "extensions";
68
69/// The Actions API client.
70/// Read more at <https://coralogix.com/docs/coralogix-action-extension/>
71pub struct ExtensionsClient {
72    teams_level_metadata_map: MetadataMap,
73    extensions_service_client: Mutex<ExtensionServiceClient<Channel>>,
74    extensions_deployment_service_client: Mutex<ExtensionDeploymentServiceClient<Channel>>,
75}
76
77impl ExtensionsClient {
78    /// Creates a new client for the Extensions API.
79    ///
80    /// # Arguments
81    /// * `auth_context` - The [`AuthContext`] to use for authentication.
82    /// * `region` - The [`CoralogixRegion`] to connect to.
83    pub fn new(auth_context: AuthContext, region: CoralogixRegion) -> Result<Self> {
84        let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
85            .tls_config(ClientTlsConfig::new().with_native_roots())?
86            .connect_lazy();
87        let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
88        Ok(Self {
89            teams_level_metadata_map: request_metadata.to_metadata_map(),
90            extensions_service_client: Mutex::new(
91                ExtensionServiceClient::new(channel.clone())
92                    .max_decoding_message_size(50 * 1024 * 1024)
93                    .max_encoding_message_size(50 * 1024 * 1024),
94            ),
95            extensions_deployment_service_client: Mutex::new(
96                ExtensionDeploymentServiceClient::new(channel)
97                    .max_decoding_message_size(50 * 1024 * 1024)
98                    .max_encoding_message_size(50 * 1024 * 1024),
99            ),
100        })
101    }
102
103    /// Get all extensions
104    ///
105    /// # Arguments
106    /// * `filter` - The [`ExtensionFilter`] used to filter the extensions.
107    /// * `include_hidden_extensions` - Whether to include hidden extensions.
108    pub async fn get_all(
109        &self,
110        filter: Option<ExtensionFilter>,
111        include_hidden_extensions: bool,
112    ) -> Result<GetAllExtensionsResponse> {
113        let request = make_request_with_metadata(
114            GetAllExtensionsRequest {
115                filter,
116                include_hidden_extensions: Some(include_hidden_extensions),
117            },
118            &self.teams_level_metadata_map,
119        );
120        Ok(self
121            .extensions_service_client
122            .lock()
123            .await
124            .get_all_extensions(request)
125            .await
126            .map_err(|status| SdkApiError {
127                status,
128                endpoint: "/com.coralogix.extensions.v1.ExtensionService/GetAllExtensions".into(),
129                feature_group: EXTENSIONS_FEATURE_GROUP_ID.into(),
130            })?
131            .into_inner())
132    }
133
134    /// Get an extension by ID
135    ///
136    /// # Arguments
137    /// * `id` - The ID of the extension to get.
138    pub async fn get(&self, id: String) -> Result<GetExtensionResponse> {
139        let request = make_request_with_metadata(
140            GetExtensionRequest {
141                id: Some(id),
142                include_dashboard_binaries: None,
143                include_testing_revision: None,
144            },
145            &self.teams_level_metadata_map,
146        );
147        //let client = self.service_client.lock().await.descri
148        Ok(self
149            .extensions_service_client
150            .lock()
151            .await
152            .get_extension(request)
153            .await
154            .map_err(|status| SdkApiError {
155                status,
156                endpoint: "/com.coralogix.extensions.v1.ExtensionService/GetExtension".into(),
157                feature_group: EXTENSIONS_FEATURE_GROUP_ID.into(),
158            })?
159            .into_inner())
160    }
161
162    /// Get all deplyoyed extensions
163    pub async fn get_deployed(&self) -> Result<GetDeployedExtensionsResponse> {
164        let request = make_request_with_metadata(
165            GetDeployedExtensionsRequest {},
166            &self.teams_level_metadata_map,
167        );
168        Ok(self
169            .extensions_deployment_service_client
170            .lock()
171            .await
172            .get_deployed_extensions(request)
173            .await
174            .map_err(|status| SdkApiError {
175                status,
176                endpoint:
177                    "/com.coralogix.extensions.v1.ExtensionDeploymentService/GetDeployedExtensions"
178                        .into(),
179                feature_group: EXTENSIONS_FEATURE_GROUP_ID.into(),
180            })?
181            .into_inner())
182    }
183
184    /// Deploy an extension
185    ///
186    /// # Arguments
187    /// * `id` - The ID of the extension to deploy.
188    /// * `version` - The version of the extension to deploy.
189    /// * `applications` - The applications to deploy the extension to.
190    /// * `subsystems` - The subsystems to deploy the extension to.
191    /// * `item_ids` - The item IDs to deploy the extension to.
192    /// * `extension_deployment` - The extension deployment to deploy.
193    /// * `extension_deployment` - The extension deployment to deploy.
194    /// * `extension_deployment` - The extension deployment to deploy.
195    pub async fn deploy(
196        &self,
197        id: String,
198        version: String,
199        applications: Vec<String>,
200        subsystems: Vec<String>,
201        item_ids: Vec<String>,
202        extension_deployment: Option<ExtensionDeployment>,
203    ) -> Result<DeployExtensionResponse> {
204        let request = make_request_with_metadata(
205            DeployExtensionRequest {
206                id: Some(id),
207                version: Some(version),
208                item_ids,
209                applications,
210                subsystems,
211                extension_deployment,
212            },
213            &self.teams_level_metadata_map,
214        );
215        Ok(self
216            .extensions_deployment_service_client
217            .lock()
218            .await
219            .deploy_extension(request)
220            .await
221            .map_err(|status| SdkApiError {
222                status,
223                endpoint: "/com.coralogix.extensions.v1.ExtensionDeploymentService/DeployExtension"
224                    .into(),
225                feature_group: EXTENSIONS_FEATURE_GROUP_ID.into(),
226            })?
227            .into_inner())
228    }
229
230    /// Undeploy an extension
231    ///
232    /// # Arguments
233    /// * `id` - The ID of the extension to undeploy.
234    /// * `kept_extension_items` - The extension items to keep.
235    pub async fn undeploy(
236        &self,
237        id: String,
238        kept_extension_items: Vec<String>,
239    ) -> Result<UndeployExtensionResponse> {
240        let request = make_request_with_metadata(
241            UndeployExtensionRequest {
242                id: Some(id),
243                kept_extension_items,
244            },
245            &self.teams_level_metadata_map,
246        );
247        Ok(self
248            .extensions_deployment_service_client
249            .lock()
250            .await
251            .undeploy_extension(request)
252            .await
253            .map_err(|status| SdkApiError {
254                status,
255                endpoint:
256                    "/com.coralogix.extensions.v1.ExtensionDeploymentService/UndeployExtension"
257                        .into(),
258                feature_group: EXTENSIONS_FEATURE_GROUP_ID.into(),
259            })?
260            .into_inner())
261    }
262
263    /// Update a deployed extension
264    ///
265    /// # Arguments
266    /// * `id` - The ID of the extension to update.
267    /// * `version` - The version of the extension to update.
268    /// * `item_ids` - The item IDs to update the extension to.
269    /// * `applications` - The applications to update the extension to.
270    /// * `subsystems` - The subsystems to update the extension to.
271    /// * `extension_deployment` - The extension deployment to update.
272    pub async fn update(
273        &self,
274        id: String,
275        version: String,
276        item_ids: Vec<String>,
277        applications: Vec<String>,
278        subsystems: Vec<String>,
279        extension_deployment: Option<ExtensionDeployment>,
280    ) -> Result<UpdateExtensionResponse> {
281        let request = make_request_with_metadata(
282            UpdateExtensionRequest {
283                id: Some(id),
284                version: Some(version),
285                item_ids,
286                applications,
287                subsystems,
288                extension_deployment,
289            },
290            &self.teams_level_metadata_map,
291        );
292        Ok(self
293            .extensions_deployment_service_client
294            .lock()
295            .await
296            .update_extension(request)
297            .await
298            .map_err(|status| SdkApiError {
299                status,
300                endpoint:
301                    "/com.coralogix.extensions.v1.ExtensionDeploymentService/UpdateExtensions"
302                        .into(),
303                feature_group: EXTENSIONS_FEATURE_GROUP_ID.into(),
304            })?
305            .into_inner())
306    }
307}