1use crate::datadog;
5use flate2::{
6 write::{GzEncoder, ZlibEncoder},
7 Compression,
8};
9use reqwest::header::{HeaderMap, HeaderValue};
10use serde::{Deserialize, Serialize};
11use std::io::Write;
12
13#[non_exhaustive]
15#[derive(Clone, Default, Debug)]
16pub struct GetHostTotalsOptionalParams {
17 pub from: Option<i64>,
19}
20
21impl GetHostTotalsOptionalParams {
22 pub fn from(mut self, value: i64) -> Self {
24 self.from = Some(value);
25 self
26 }
27}
28
29#[non_exhaustive]
31#[derive(Clone, Default, Debug)]
32pub struct ListHostsOptionalParams {
33 pub filter: Option<String>,
35 pub sort_field: Option<String>,
37 pub sort_dir: Option<String>,
39 pub start: Option<i64>,
41 pub count: Option<i64>,
43 pub from: Option<i64>,
45 pub include_muted_hosts_data: Option<bool>,
47 pub include_hosts_metadata: Option<bool>,
49}
50
51impl ListHostsOptionalParams {
52 pub fn filter(mut self, value: String) -> Self {
54 self.filter = Some(value);
55 self
56 }
57 pub fn sort_field(mut self, value: String) -> Self {
59 self.sort_field = Some(value);
60 self
61 }
62 pub fn sort_dir(mut self, value: String) -> Self {
64 self.sort_dir = Some(value);
65 self
66 }
67 pub fn start(mut self, value: i64) -> Self {
69 self.start = Some(value);
70 self
71 }
72 pub fn count(mut self, value: i64) -> Self {
74 self.count = Some(value);
75 self
76 }
77 pub fn from(mut self, value: i64) -> Self {
79 self.from = Some(value);
80 self
81 }
82 pub fn include_muted_hosts_data(mut self, value: bool) -> Self {
84 self.include_muted_hosts_data = Some(value);
85 self
86 }
87 pub fn include_hosts_metadata(mut self, value: bool) -> Self {
89 self.include_hosts_metadata = Some(value);
90 self
91 }
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(untagged)]
97pub enum GetHostTotalsError {
98 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
99 UnknownValue(serde_json::Value),
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104#[serde(untagged)]
105pub enum ListHostsError {
106 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
107 UnknownValue(serde_json::Value),
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(untagged)]
113pub enum MuteHostError {
114 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
115 UnknownValue(serde_json::Value),
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120#[serde(untagged)]
121pub enum UnmuteHostError {
122 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
123 UnknownValue(serde_json::Value),
124}
125
126#[derive(Debug, Clone)]
128pub struct HostsAPI {
129 config: datadog::Configuration,
130 client: reqwest_middleware::ClientWithMiddleware,
131}
132
133impl Default for HostsAPI {
134 fn default() -> Self {
135 Self::with_config(datadog::Configuration::default())
136 }
137}
138
139impl HostsAPI {
140 pub fn new() -> Self {
141 Self::default()
142 }
143 pub fn with_config(config: datadog::Configuration) -> Self {
144 let mut reqwest_client_builder = reqwest::Client::builder();
145
146 if let Some(proxy_url) = &config.proxy_url {
147 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
148 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
149 }
150
151 let mut middleware_client_builder =
152 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
153
154 if config.enable_retry {
155 struct RetryableStatus;
156 impl reqwest_retry::RetryableStrategy for RetryableStatus {
157 fn handle(
158 &self,
159 res: &Result<reqwest::Response, reqwest_middleware::Error>,
160 ) -> Option<reqwest_retry::Retryable> {
161 match res {
162 Ok(success) => reqwest_retry::default_on_request_success(success),
163 Err(_) => None,
164 }
165 }
166 }
167 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
168 .build_with_max_retries(config.max_retries);
169
170 let retry_middleware =
171 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
172 backoff_policy,
173 RetryableStatus,
174 );
175
176 middleware_client_builder = middleware_client_builder.with(retry_middleware);
177 }
178
179 let client = middleware_client_builder.build();
180
181 Self { config, client }
182 }
183
184 pub fn with_client_and_config(
185 config: datadog::Configuration,
186 client: reqwest_middleware::ClientWithMiddleware,
187 ) -> Self {
188 Self { config, client }
189 }
190
191 pub async fn get_host_totals(
194 &self,
195 params: GetHostTotalsOptionalParams,
196 ) -> Result<crate::datadogV1::model::HostTotals, datadog::Error<GetHostTotalsError>> {
197 match self.get_host_totals_with_http_info(params).await {
198 Ok(response_content) => {
199 if let Some(e) = response_content.entity {
200 Ok(e)
201 } else {
202 Err(datadog::Error::Serde(serde::de::Error::custom(
203 "response content was None",
204 )))
205 }
206 }
207 Err(err) => Err(err),
208 }
209 }
210
211 pub async fn get_host_totals_with_http_info(
214 &self,
215 params: GetHostTotalsOptionalParams,
216 ) -> Result<
217 datadog::ResponseContent<crate::datadogV1::model::HostTotals>,
218 datadog::Error<GetHostTotalsError>,
219 > {
220 let local_configuration = &self.config;
221 let operation_id = "v1.get_host_totals";
222
223 let from = params.from;
225
226 let local_client = &self.client;
227
228 let local_uri_str = format!(
229 "{}/api/v1/hosts/totals",
230 local_configuration.get_operation_host(operation_id)
231 );
232 let mut local_req_builder =
233 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
234
235 if let Some(ref local_query_param) = from {
236 local_req_builder =
237 local_req_builder.query(&[("from", &local_query_param.to_string())]);
238 };
239
240 let mut headers = HeaderMap::new();
242 headers.insert("Accept", HeaderValue::from_static("application/json"));
243
244 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
246 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
247 Err(e) => {
248 log::warn!("Failed to parse user agent header: {e}, falling back to default");
249 headers.insert(
250 reqwest::header::USER_AGENT,
251 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
252 )
253 }
254 };
255
256 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
258 headers.insert(
259 "DD-API-KEY",
260 HeaderValue::from_str(local_key.key.as_str())
261 .expect("failed to parse DD-API-KEY header"),
262 );
263 };
264 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
265 headers.insert(
266 "DD-APPLICATION-KEY",
267 HeaderValue::from_str(local_key.key.as_str())
268 .expect("failed to parse DD-APPLICATION-KEY header"),
269 );
270 };
271
272 local_req_builder = local_req_builder.headers(headers);
273 let local_req = local_req_builder.build()?;
274 log::debug!("request content: {:?}", local_req.body());
275 let local_resp = local_client.execute(local_req).await?;
276
277 let local_status = local_resp.status();
278 let local_content = local_resp.text().await?;
279 log::debug!("response content: {}", local_content);
280
281 if !local_status.is_client_error() && !local_status.is_server_error() {
282 match serde_json::from_str::<crate::datadogV1::model::HostTotals>(&local_content) {
283 Ok(e) => {
284 return Ok(datadog::ResponseContent {
285 status: local_status,
286 content: local_content,
287 entity: Some(e),
288 })
289 }
290 Err(e) => return Err(datadog::Error::Serde(e)),
291 };
292 } else {
293 let local_entity: Option<GetHostTotalsError> =
294 serde_json::from_str(&local_content).ok();
295 let local_error = datadog::ResponseContent {
296 status: local_status,
297 content: local_content,
298 entity: local_entity,
299 };
300 Err(datadog::Error::ResponseError(local_error))
301 }
302 }
303
304 pub async fn list_hosts(
311 &self,
312 params: ListHostsOptionalParams,
313 ) -> Result<crate::datadogV1::model::HostListResponse, datadog::Error<ListHostsError>> {
314 match self.list_hosts_with_http_info(params).await {
315 Ok(response_content) => {
316 if let Some(e) = response_content.entity {
317 Ok(e)
318 } else {
319 Err(datadog::Error::Serde(serde::de::Error::custom(
320 "response content was None",
321 )))
322 }
323 }
324 Err(err) => Err(err),
325 }
326 }
327
328 pub async fn list_hosts_with_http_info(
335 &self,
336 params: ListHostsOptionalParams,
337 ) -> Result<
338 datadog::ResponseContent<crate::datadogV1::model::HostListResponse>,
339 datadog::Error<ListHostsError>,
340 > {
341 let local_configuration = &self.config;
342 let operation_id = "v1.list_hosts";
343
344 let filter = params.filter;
346 let sort_field = params.sort_field;
347 let sort_dir = params.sort_dir;
348 let start = params.start;
349 let count = params.count;
350 let from = params.from;
351 let include_muted_hosts_data = params.include_muted_hosts_data;
352 let include_hosts_metadata = params.include_hosts_metadata;
353
354 let local_client = &self.client;
355
356 let local_uri_str = format!(
357 "{}/api/v1/hosts",
358 local_configuration.get_operation_host(operation_id)
359 );
360 let mut local_req_builder =
361 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
362
363 if let Some(ref local_query_param) = filter {
364 local_req_builder =
365 local_req_builder.query(&[("filter", &local_query_param.to_string())]);
366 };
367 if let Some(ref local_query_param) = sort_field {
368 local_req_builder =
369 local_req_builder.query(&[("sort_field", &local_query_param.to_string())]);
370 };
371 if let Some(ref local_query_param) = sort_dir {
372 local_req_builder =
373 local_req_builder.query(&[("sort_dir", &local_query_param.to_string())]);
374 };
375 if let Some(ref local_query_param) = start {
376 local_req_builder =
377 local_req_builder.query(&[("start", &local_query_param.to_string())]);
378 };
379 if let Some(ref local_query_param) = count {
380 local_req_builder =
381 local_req_builder.query(&[("count", &local_query_param.to_string())]);
382 };
383 if let Some(ref local_query_param) = from {
384 local_req_builder =
385 local_req_builder.query(&[("from", &local_query_param.to_string())]);
386 };
387 if let Some(ref local_query_param) = include_muted_hosts_data {
388 local_req_builder = local_req_builder
389 .query(&[("include_muted_hosts_data", &local_query_param.to_string())]);
390 };
391 if let Some(ref local_query_param) = include_hosts_metadata {
392 local_req_builder = local_req_builder
393 .query(&[("include_hosts_metadata", &local_query_param.to_string())]);
394 };
395
396 let mut headers = HeaderMap::new();
398 headers.insert("Accept", HeaderValue::from_static("application/json"));
399
400 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
402 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
403 Err(e) => {
404 log::warn!("Failed to parse user agent header: {e}, falling back to default");
405 headers.insert(
406 reqwest::header::USER_AGENT,
407 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
408 )
409 }
410 };
411
412 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
414 headers.insert(
415 "DD-API-KEY",
416 HeaderValue::from_str(local_key.key.as_str())
417 .expect("failed to parse DD-API-KEY header"),
418 );
419 };
420 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
421 headers.insert(
422 "DD-APPLICATION-KEY",
423 HeaderValue::from_str(local_key.key.as_str())
424 .expect("failed to parse DD-APPLICATION-KEY header"),
425 );
426 };
427
428 local_req_builder = local_req_builder.headers(headers);
429 let local_req = local_req_builder.build()?;
430 log::debug!("request content: {:?}", local_req.body());
431 let local_resp = local_client.execute(local_req).await?;
432
433 let local_status = local_resp.status();
434 let local_content = local_resp.text().await?;
435 log::debug!("response content: {}", local_content);
436
437 if !local_status.is_client_error() && !local_status.is_server_error() {
438 match serde_json::from_str::<crate::datadogV1::model::HostListResponse>(&local_content)
439 {
440 Ok(e) => {
441 return Ok(datadog::ResponseContent {
442 status: local_status,
443 content: local_content,
444 entity: Some(e),
445 })
446 }
447 Err(e) => return Err(datadog::Error::Serde(e)),
448 };
449 } else {
450 let local_entity: Option<ListHostsError> = serde_json::from_str(&local_content).ok();
451 let local_error = datadog::ResponseContent {
452 status: local_status,
453 content: local_content,
454 entity: local_entity,
455 };
456 Err(datadog::Error::ResponseError(local_error))
457 }
458 }
459
460 pub async fn mute_host(
462 &self,
463 host_name: String,
464 body: crate::datadogV1::model::HostMuteSettings,
465 ) -> Result<crate::datadogV1::model::HostMuteResponse, datadog::Error<MuteHostError>> {
466 match self.mute_host_with_http_info(host_name, body).await {
467 Ok(response_content) => {
468 if let Some(e) = response_content.entity {
469 Ok(e)
470 } else {
471 Err(datadog::Error::Serde(serde::de::Error::custom(
472 "response content was None",
473 )))
474 }
475 }
476 Err(err) => Err(err),
477 }
478 }
479
480 pub async fn mute_host_with_http_info(
482 &self,
483 host_name: String,
484 body: crate::datadogV1::model::HostMuteSettings,
485 ) -> Result<
486 datadog::ResponseContent<crate::datadogV1::model::HostMuteResponse>,
487 datadog::Error<MuteHostError>,
488 > {
489 let local_configuration = &self.config;
490 let operation_id = "v1.mute_host";
491
492 let local_client = &self.client;
493
494 let local_uri_str = format!(
495 "{}/api/v1/host/{host_name}/mute",
496 local_configuration.get_operation_host(operation_id),
497 host_name = datadog::urlencode(host_name)
498 );
499 let mut local_req_builder =
500 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
501
502 let mut headers = HeaderMap::new();
504 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
505 headers.insert("Accept", HeaderValue::from_static("application/json"));
506
507 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
509 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
510 Err(e) => {
511 log::warn!("Failed to parse user agent header: {e}, falling back to default");
512 headers.insert(
513 reqwest::header::USER_AGENT,
514 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
515 )
516 }
517 };
518
519 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
521 headers.insert(
522 "DD-API-KEY",
523 HeaderValue::from_str(local_key.key.as_str())
524 .expect("failed to parse DD-API-KEY header"),
525 );
526 };
527 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
528 headers.insert(
529 "DD-APPLICATION-KEY",
530 HeaderValue::from_str(local_key.key.as_str())
531 .expect("failed to parse DD-APPLICATION-KEY header"),
532 );
533 };
534
535 let output = Vec::new();
537 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
538 if body.serialize(&mut ser).is_ok() {
539 if let Some(content_encoding) = headers.get("Content-Encoding") {
540 match content_encoding.to_str().unwrap_or_default() {
541 "gzip" => {
542 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
543 let _ = enc.write_all(ser.into_inner().as_slice());
544 match enc.finish() {
545 Ok(buf) => {
546 local_req_builder = local_req_builder.body(buf);
547 }
548 Err(e) => return Err(datadog::Error::Io(e)),
549 }
550 }
551 "deflate" => {
552 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
553 let _ = enc.write_all(ser.into_inner().as_slice());
554 match enc.finish() {
555 Ok(buf) => {
556 local_req_builder = local_req_builder.body(buf);
557 }
558 Err(e) => return Err(datadog::Error::Io(e)),
559 }
560 }
561 "zstd1" => {
562 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
563 let _ = enc.write_all(ser.into_inner().as_slice());
564 match enc.finish() {
565 Ok(buf) => {
566 local_req_builder = local_req_builder.body(buf);
567 }
568 Err(e) => return Err(datadog::Error::Io(e)),
569 }
570 }
571 _ => {
572 local_req_builder = local_req_builder.body(ser.into_inner());
573 }
574 }
575 } else {
576 local_req_builder = local_req_builder.body(ser.into_inner());
577 }
578 }
579
580 local_req_builder = local_req_builder.headers(headers);
581 let local_req = local_req_builder.build()?;
582 log::debug!("request content: {:?}", local_req.body());
583 let local_resp = local_client.execute(local_req).await?;
584
585 let local_status = local_resp.status();
586 let local_content = local_resp.text().await?;
587 log::debug!("response content: {}", local_content);
588
589 if !local_status.is_client_error() && !local_status.is_server_error() {
590 match serde_json::from_str::<crate::datadogV1::model::HostMuteResponse>(&local_content)
591 {
592 Ok(e) => {
593 return Ok(datadog::ResponseContent {
594 status: local_status,
595 content: local_content,
596 entity: Some(e),
597 })
598 }
599 Err(e) => return Err(datadog::Error::Serde(e)),
600 };
601 } else {
602 let local_entity: Option<MuteHostError> = serde_json::from_str(&local_content).ok();
603 let local_error = datadog::ResponseContent {
604 status: local_status,
605 content: local_content,
606 entity: local_entity,
607 };
608 Err(datadog::Error::ResponseError(local_error))
609 }
610 }
611
612 pub async fn unmute_host(
614 &self,
615 host_name: String,
616 ) -> Result<crate::datadogV1::model::HostMuteResponse, datadog::Error<UnmuteHostError>> {
617 match self.unmute_host_with_http_info(host_name).await {
618 Ok(response_content) => {
619 if let Some(e) = response_content.entity {
620 Ok(e)
621 } else {
622 Err(datadog::Error::Serde(serde::de::Error::custom(
623 "response content was None",
624 )))
625 }
626 }
627 Err(err) => Err(err),
628 }
629 }
630
631 pub async fn unmute_host_with_http_info(
633 &self,
634 host_name: String,
635 ) -> Result<
636 datadog::ResponseContent<crate::datadogV1::model::HostMuteResponse>,
637 datadog::Error<UnmuteHostError>,
638 > {
639 let local_configuration = &self.config;
640 let operation_id = "v1.unmute_host";
641
642 let local_client = &self.client;
643
644 let local_uri_str = format!(
645 "{}/api/v1/host/{host_name}/unmute",
646 local_configuration.get_operation_host(operation_id),
647 host_name = datadog::urlencode(host_name)
648 );
649 let mut local_req_builder =
650 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
651
652 let mut headers = HeaderMap::new();
654 headers.insert("Accept", HeaderValue::from_static("application/json"));
655
656 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
658 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
659 Err(e) => {
660 log::warn!("Failed to parse user agent header: {e}, falling back to default");
661 headers.insert(
662 reqwest::header::USER_AGENT,
663 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
664 )
665 }
666 };
667
668 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
670 headers.insert(
671 "DD-API-KEY",
672 HeaderValue::from_str(local_key.key.as_str())
673 .expect("failed to parse DD-API-KEY header"),
674 );
675 };
676 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
677 headers.insert(
678 "DD-APPLICATION-KEY",
679 HeaderValue::from_str(local_key.key.as_str())
680 .expect("failed to parse DD-APPLICATION-KEY header"),
681 );
682 };
683
684 local_req_builder = local_req_builder.headers(headers);
685 let local_req = local_req_builder.build()?;
686 log::debug!("request content: {:?}", local_req.body());
687 let local_resp = local_client.execute(local_req).await?;
688
689 let local_status = local_resp.status();
690 let local_content = local_resp.text().await?;
691 log::debug!("response content: {}", local_content);
692
693 if !local_status.is_client_error() && !local_status.is_server_error() {
694 match serde_json::from_str::<crate::datadogV1::model::HostMuteResponse>(&local_content)
695 {
696 Ok(e) => {
697 return Ok(datadog::ResponseContent {
698 status: local_status,
699 content: local_content,
700 entity: Some(e),
701 })
702 }
703 Err(e) => return Err(datadog::Error::Serde(e)),
704 };
705 } else {
706 let local_entity: Option<UnmuteHostError> = serde_json::from_str(&local_content).ok();
707 let local_error = datadog::ResponseContent {
708 status: local_status,
709 content: local_content,
710 entity: local_entity,
711 };
712 Err(datadog::Error::ResponseError(local_error))
713 }
714 }
715}