datadog_api_client/datadogV2/api/
api_csm_coverage_analysis.rs

1// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2// This product includes software developed at Datadog (https://www.datadoghq.com/).
3// Copyright 2019-Present Datadog, Inc.
4use crate::datadog;
5use reqwest::header::{HeaderMap, HeaderValue};
6use serde::{Deserialize, Serialize};
7
8/// GetCSMCloudAccountsCoverageAnalysisError is a struct for typed errors of method [`CSMCoverageAnalysisAPI::get_csm_cloud_accounts_coverage_analysis`]
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(untagged)]
11pub enum GetCSMCloudAccountsCoverageAnalysisError {
12    APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
13    UnknownValue(serde_json::Value),
14}
15
16/// GetCSMHostsAndContainersCoverageAnalysisError is a struct for typed errors of method [`CSMCoverageAnalysisAPI::get_csm_hosts_and_containers_coverage_analysis`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum GetCSMHostsAndContainersCoverageAnalysisError {
20    APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
21    UnknownValue(serde_json::Value),
22}
23
24/// GetCSMServerlessCoverageAnalysisError is a struct for typed errors of method [`CSMCoverageAnalysisAPI::get_csm_serverless_coverage_analysis`]
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum GetCSMServerlessCoverageAnalysisError {
28    APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
29    UnknownValue(serde_json::Value),
30}
31
32/// Datadog Cloud Security Management (CSM) delivers real-time threat detection
33/// and continuous configuration audits across your entire cloud infrastructure,
34/// all in a unified view for seamless collaboration and faster remediation.
35/// Go to <https://docs.datadoghq.com/security/cloud_security_management> to learn more.
36#[derive(Debug, Clone)]
37pub struct CSMCoverageAnalysisAPI {
38    config: datadog::Configuration,
39    client: reqwest_middleware::ClientWithMiddleware,
40}
41
42impl Default for CSMCoverageAnalysisAPI {
43    fn default() -> Self {
44        Self::with_config(datadog::Configuration::default())
45    }
46}
47
48impl CSMCoverageAnalysisAPI {
49    pub fn new() -> Self {
50        Self::default()
51    }
52    pub fn with_config(config: datadog::Configuration) -> Self {
53        let mut reqwest_client_builder = reqwest::Client::builder();
54
55        if let Some(proxy_url) = &config.proxy_url {
56            let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
57            reqwest_client_builder = reqwest_client_builder.proxy(proxy);
58        }
59
60        let mut middleware_client_builder =
61            reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
62
63        if config.enable_retry {
64            struct RetryableStatus;
65            impl reqwest_retry::RetryableStrategy for RetryableStatus {
66                fn handle(
67                    &self,
68                    res: &Result<reqwest::Response, reqwest_middleware::Error>,
69                ) -> Option<reqwest_retry::Retryable> {
70                    match res {
71                        Ok(success) => reqwest_retry::default_on_request_success(success),
72                        Err(_) => None,
73                    }
74                }
75            }
76            let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
77                .build_with_max_retries(config.max_retries);
78
79            let retry_middleware =
80                reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
81                    backoff_policy,
82                    RetryableStatus,
83                );
84
85            middleware_client_builder = middleware_client_builder.with(retry_middleware);
86        }
87
88        let client = middleware_client_builder.build();
89
90        Self { config, client }
91    }
92
93    pub fn with_client_and_config(
94        config: datadog::Configuration,
95        client: reqwest_middleware::ClientWithMiddleware,
96    ) -> Self {
97        Self { config, client }
98    }
99
100    /// Get the CSM Coverage Analysis of your Cloud Accounts.
101    /// This is calculated based on the number of your Cloud Accounts that are
102    /// scanned for security issues.
103    pub async fn get_csm_cloud_accounts_coverage_analysis(
104        &self,
105    ) -> Result<
106        crate::datadogV2::model::CsmCloudAccountsCoverageAnalysisResponse,
107        datadog::Error<GetCSMCloudAccountsCoverageAnalysisError>,
108    > {
109        match self
110            .get_csm_cloud_accounts_coverage_analysis_with_http_info()
111            .await
112        {
113            Ok(response_content) => {
114                if let Some(e) = response_content.entity {
115                    Ok(e)
116                } else {
117                    Err(datadog::Error::Serde(serde::de::Error::custom(
118                        "response content was None",
119                    )))
120                }
121            }
122            Err(err) => Err(err),
123        }
124    }
125
126    /// Get the CSM Coverage Analysis of your Cloud Accounts.
127    /// This is calculated based on the number of your Cloud Accounts that are
128    /// scanned for security issues.
129    pub async fn get_csm_cloud_accounts_coverage_analysis_with_http_info(
130        &self,
131    ) -> Result<
132        datadog::ResponseContent<crate::datadogV2::model::CsmCloudAccountsCoverageAnalysisResponse>,
133        datadog::Error<GetCSMCloudAccountsCoverageAnalysisError>,
134    > {
135        let local_configuration = &self.config;
136        let operation_id = "v2.get_csm_cloud_accounts_coverage_analysis";
137
138        let local_client = &self.client;
139
140        let local_uri_str = format!(
141            "{}/api/v2/csm/onboarding/coverage_analysis/cloud_accounts",
142            local_configuration.get_operation_host(operation_id)
143        );
144        let mut local_req_builder =
145            local_client.request(reqwest::Method::GET, local_uri_str.as_str());
146
147        // build headers
148        let mut headers = HeaderMap::new();
149        headers.insert("Accept", HeaderValue::from_static("application/json"));
150
151        // build user agent
152        match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
153            Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
154            Err(e) => {
155                log::warn!("Failed to parse user agent header: {e}, falling back to default");
156                headers.insert(
157                    reqwest::header::USER_AGENT,
158                    HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
159                )
160            }
161        };
162
163        // build auth
164        if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
165            headers.insert(
166                "DD-API-KEY",
167                HeaderValue::from_str(local_key.key.as_str())
168                    .expect("failed to parse DD-API-KEY header"),
169            );
170        };
171        if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
172            headers.insert(
173                "DD-APPLICATION-KEY",
174                HeaderValue::from_str(local_key.key.as_str())
175                    .expect("failed to parse DD-APPLICATION-KEY header"),
176            );
177        };
178
179        local_req_builder = local_req_builder.headers(headers);
180        let local_req = local_req_builder.build()?;
181        log::debug!("request content: {:?}", local_req.body());
182        let local_resp = local_client.execute(local_req).await?;
183
184        let local_status = local_resp.status();
185        let local_content = local_resp.text().await?;
186        log::debug!("response content: {}", local_content);
187
188        if !local_status.is_client_error() && !local_status.is_server_error() {
189            match serde_json::from_str::<
190                crate::datadogV2::model::CsmCloudAccountsCoverageAnalysisResponse,
191            >(&local_content)
192            {
193                Ok(e) => {
194                    return Ok(datadog::ResponseContent {
195                        status: local_status,
196                        content: local_content,
197                        entity: Some(e),
198                    })
199                }
200                Err(e) => return Err(datadog::Error::Serde(e)),
201            };
202        } else {
203            let local_entity: Option<GetCSMCloudAccountsCoverageAnalysisError> =
204                serde_json::from_str(&local_content).ok();
205            let local_error = datadog::ResponseContent {
206                status: local_status,
207                content: local_content,
208                entity: local_entity,
209            };
210            Err(datadog::Error::ResponseError(local_error))
211        }
212    }
213
214    /// Get the CSM Coverage Analysis of your Hosts and Containers.
215    /// This is calculated based on the number of agents running on your Hosts
216    /// and Containers with CSM feature(s) enabled.
217    pub async fn get_csm_hosts_and_containers_coverage_analysis(
218        &self,
219    ) -> Result<
220        crate::datadogV2::model::CsmHostsAndContainersCoverageAnalysisResponse,
221        datadog::Error<GetCSMHostsAndContainersCoverageAnalysisError>,
222    > {
223        match self
224            .get_csm_hosts_and_containers_coverage_analysis_with_http_info()
225            .await
226        {
227            Ok(response_content) => {
228                if let Some(e) = response_content.entity {
229                    Ok(e)
230                } else {
231                    Err(datadog::Error::Serde(serde::de::Error::custom(
232                        "response content was None",
233                    )))
234                }
235            }
236            Err(err) => Err(err),
237        }
238    }
239
240    /// Get the CSM Coverage Analysis of your Hosts and Containers.
241    /// This is calculated based on the number of agents running on your Hosts
242    /// and Containers with CSM feature(s) enabled.
243    pub async fn get_csm_hosts_and_containers_coverage_analysis_with_http_info(
244        &self,
245    ) -> Result<
246        datadog::ResponseContent<
247            crate::datadogV2::model::CsmHostsAndContainersCoverageAnalysisResponse,
248        >,
249        datadog::Error<GetCSMHostsAndContainersCoverageAnalysisError>,
250    > {
251        let local_configuration = &self.config;
252        let operation_id = "v2.get_csm_hosts_and_containers_coverage_analysis";
253
254        let local_client = &self.client;
255
256        let local_uri_str = format!(
257            "{}/api/v2/csm/onboarding/coverage_analysis/hosts_and_containers",
258            local_configuration.get_operation_host(operation_id)
259        );
260        let mut local_req_builder =
261            local_client.request(reqwest::Method::GET, local_uri_str.as_str());
262
263        // build headers
264        let mut headers = HeaderMap::new();
265        headers.insert("Accept", HeaderValue::from_static("application/json"));
266
267        // build user agent
268        match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
269            Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
270            Err(e) => {
271                log::warn!("Failed to parse user agent header: {e}, falling back to default");
272                headers.insert(
273                    reqwest::header::USER_AGENT,
274                    HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
275                )
276            }
277        };
278
279        // build auth
280        if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
281            headers.insert(
282                "DD-API-KEY",
283                HeaderValue::from_str(local_key.key.as_str())
284                    .expect("failed to parse DD-API-KEY header"),
285            );
286        };
287        if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
288            headers.insert(
289                "DD-APPLICATION-KEY",
290                HeaderValue::from_str(local_key.key.as_str())
291                    .expect("failed to parse DD-APPLICATION-KEY header"),
292            );
293        };
294
295        local_req_builder = local_req_builder.headers(headers);
296        let local_req = local_req_builder.build()?;
297        log::debug!("request content: {:?}", local_req.body());
298        let local_resp = local_client.execute(local_req).await?;
299
300        let local_status = local_resp.status();
301        let local_content = local_resp.text().await?;
302        log::debug!("response content: {}", local_content);
303
304        if !local_status.is_client_error() && !local_status.is_server_error() {
305            match serde_json::from_str::<
306                crate::datadogV2::model::CsmHostsAndContainersCoverageAnalysisResponse,
307            >(&local_content)
308            {
309                Ok(e) => {
310                    return Ok(datadog::ResponseContent {
311                        status: local_status,
312                        content: local_content,
313                        entity: Some(e),
314                    })
315                }
316                Err(e) => return Err(datadog::Error::Serde(e)),
317            };
318        } else {
319            let local_entity: Option<GetCSMHostsAndContainersCoverageAnalysisError> =
320                serde_json::from_str(&local_content).ok();
321            let local_error = datadog::ResponseContent {
322                status: local_status,
323                content: local_content,
324                entity: local_entity,
325            };
326            Err(datadog::Error::ResponseError(local_error))
327        }
328    }
329
330    /// Get the CSM Coverage Analysis of your Serverless Resources.
331    /// This is calculated based on the number of agents running on your Serverless
332    /// Resources with CSM feature(s) enabled.
333    pub async fn get_csm_serverless_coverage_analysis(
334        &self,
335    ) -> Result<
336        crate::datadogV2::model::CsmServerlessCoverageAnalysisResponse,
337        datadog::Error<GetCSMServerlessCoverageAnalysisError>,
338    > {
339        match self
340            .get_csm_serverless_coverage_analysis_with_http_info()
341            .await
342        {
343            Ok(response_content) => {
344                if let Some(e) = response_content.entity {
345                    Ok(e)
346                } else {
347                    Err(datadog::Error::Serde(serde::de::Error::custom(
348                        "response content was None",
349                    )))
350                }
351            }
352            Err(err) => Err(err),
353        }
354    }
355
356    /// Get the CSM Coverage Analysis of your Serverless Resources.
357    /// This is calculated based on the number of agents running on your Serverless
358    /// Resources with CSM feature(s) enabled.
359    pub async fn get_csm_serverless_coverage_analysis_with_http_info(
360        &self,
361    ) -> Result<
362        datadog::ResponseContent<crate::datadogV2::model::CsmServerlessCoverageAnalysisResponse>,
363        datadog::Error<GetCSMServerlessCoverageAnalysisError>,
364    > {
365        let local_configuration = &self.config;
366        let operation_id = "v2.get_csm_serverless_coverage_analysis";
367
368        let local_client = &self.client;
369
370        let local_uri_str = format!(
371            "{}/api/v2/csm/onboarding/coverage_analysis/serverless",
372            local_configuration.get_operation_host(operation_id)
373        );
374        let mut local_req_builder =
375            local_client.request(reqwest::Method::GET, local_uri_str.as_str());
376
377        // build headers
378        let mut headers = HeaderMap::new();
379        headers.insert("Accept", HeaderValue::from_static("application/json"));
380
381        // build user agent
382        match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
383            Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
384            Err(e) => {
385                log::warn!("Failed to parse user agent header: {e}, falling back to default");
386                headers.insert(
387                    reqwest::header::USER_AGENT,
388                    HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
389                )
390            }
391        };
392
393        // build auth
394        if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
395            headers.insert(
396                "DD-API-KEY",
397                HeaderValue::from_str(local_key.key.as_str())
398                    .expect("failed to parse DD-API-KEY header"),
399            );
400        };
401        if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
402            headers.insert(
403                "DD-APPLICATION-KEY",
404                HeaderValue::from_str(local_key.key.as_str())
405                    .expect("failed to parse DD-APPLICATION-KEY header"),
406            );
407        };
408
409        local_req_builder = local_req_builder.headers(headers);
410        let local_req = local_req_builder.build()?;
411        log::debug!("request content: {:?}", local_req.body());
412        let local_resp = local_client.execute(local_req).await?;
413
414        let local_status = local_resp.status();
415        let local_content = local_resp.text().await?;
416        log::debug!("response content: {}", local_content);
417
418        if !local_status.is_client_error() && !local_status.is_server_error() {
419            match serde_json::from_str::<
420                crate::datadogV2::model::CsmServerlessCoverageAnalysisResponse,
421            >(&local_content)
422            {
423                Ok(e) => {
424                    return Ok(datadog::ResponseContent {
425                        status: local_status,
426                        content: local_content,
427                        entity: Some(e),
428                    })
429                }
430                Err(e) => return Err(datadog::Error::Serde(e)),
431            };
432        } else {
433            let local_entity: Option<GetCSMServerlessCoverageAnalysisError> =
434                serde_json::from_str(&local_content).ok();
435            let local_error = datadog::ResponseContent {
436                status: local_status,
437                content: local_content,
438                entity: local_entity,
439            };
440            Err(datadog::Error::ResponseError(local_error))
441        }
442    }
443}