forked from DataDog/pup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_deletion.rs
More file actions
108 lines (99 loc) · 3.51 KB
/
Copy pathdata_deletion.rs
File metadata and controls
108 lines (99 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use anyhow::Result;
use datadog_api_client::datadogV2::api_data_deletion::{
DataDeletionAPI, GetDataDeletionRequestsOptionalParams,
};
use crate::config::Config;
use crate::formatter;
use crate::util;
fn make_api(cfg: &Config) -> DataDeletionAPI {
crate::make_api!(DataDeletionAPI, cfg)
}
pub async fn requests_list(
cfg: &Config,
product: Option<String>,
query: Option<String>,
status: Option<String>,
) -> Result<()> {
let api = make_api(cfg);
let mut params = GetDataDeletionRequestsOptionalParams::default();
if let Some(p) = product {
params = params.product(p);
}
if let Some(q) = query {
params = params.query(q);
}
if let Some(s) = status {
params = params.status(s);
}
let resp = api
.get_data_deletion_requests(params)
.await
.map_err(|e| anyhow::anyhow!("failed to list data deletion requests: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn requests_create(cfg: &Config, product: &str, file: &str) -> Result<()> {
let body: datadog_api_client::datadogV2::model::CreateDataDeletionRequestBody =
util::read_json_file(file)?;
let api = make_api(cfg);
let resp = api
.create_data_deletion_request(product.to_string(), body)
.await
.map_err(|e| anyhow::anyhow!("failed to create data deletion request: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn requests_cancel(cfg: &Config, request_id: &str) -> Result<()> {
let api = make_api(cfg);
let resp = api
.cancel_data_deletion_request(request_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to cancel data deletion request: {e:?}"))?;
formatter::output(cfg, &resp)
}
#[cfg(test)]
mod tests {
use crate::test_support::*;
#[tokio::test]
async fn test_data_deletion_requests_list() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut server = mockito::Server::new_async().await;
let cfg = test_config(&server.url());
let _mock = mock_any(&mut server, "GET", r#"{"data":[]}"#).await;
let result = super::requests_list(&cfg, None, None, None).await;
assert!(
result.is_ok(),
"data deletion requests list failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_data_deletion_requests_list_with_filters() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut server = mockito::Server::new_async().await;
let cfg = test_config(&server.url());
let mock = server
.mock("GET", mockito::Matcher::Any)
.match_query(mockito::Matcher::AllOf(vec![
mockito::Matcher::UrlEncoded("product".into(), "logs".into()),
mockito::Matcher::UrlEncoded("status".into(), "pending".into()),
]))
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"data":[]}"#)
.create_async()
.await;
let result =
super::requests_list(&cfg, Some("logs".into()), None, Some("pending".into())).await;
assert!(
result.is_ok(),
"data deletion requests list with filters failed: {:?}",
result.err()
);
mock.assert_async().await;
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
}