|
| 1 | +use gcloud_sdk::{google_rest_apis::servicecontrol_v1, GoogleEnvironment, GoogleRestApi}; |
| 2 | + |
| 3 | +#[derive(thiserror::Error, Debug)] |
| 4 | +enum Error { |
| 5 | + #[error(transparent)] |
| 6 | + Gcloud(#[from] gcloud_sdk::error::Error), |
| 7 | + #[error(transparent)] |
| 8 | + ServiceCheck( |
| 9 | + #[from] |
| 10 | + servicecontrol_v1::Error< |
| 11 | + servicecontrol_v1::services_api::ServicecontrolPeriodServicesPeriodCheckError, |
| 12 | + >, |
| 13 | + ), |
| 14 | + #[error("{}", format_check_errors(.0))] |
| 15 | + ServiceCheckErrors(Vec<servicecontrol_v1::CheckError>), |
| 16 | +} |
| 17 | + |
| 18 | +fn format_check_errors(errs: &[servicecontrol_v1::CheckError]) -> String { |
| 19 | + errs.iter() |
| 20 | + .filter_map(|e| { |
| 21 | + if let Some(ref code) = e.code { |
| 22 | + let c = serde_json::to_string(code) |
| 23 | + .unwrap_or_else(|_| "ERROR_CODE_UNSPECIFIED".to_string()); |
| 24 | + if let Some(ref detail) = e.detail { |
| 25 | + Some(format!("{c}: {detail}")) |
| 26 | + } else { |
| 27 | + Some(c) |
| 28 | + } |
| 29 | + } else { |
| 30 | + None |
| 31 | + } |
| 32 | + }) |
| 33 | + .collect::<Vec<_>>() |
| 34 | + .join(", ") |
| 35 | +} |
| 36 | + |
| 37 | +async fn services_check(client: GoogleRestApi, service_name: impl ToString) -> Result<(), Error> { |
| 38 | + let cfg = client.create_google_servicecontrol_v1_config().await?; |
| 39 | + |
| 40 | + let response = servicecontrol_v1::services_api::servicecontrol_services_check( |
| 41 | + &cfg, |
| 42 | + servicecontrol_v1::services_api::ServicecontrolPeriodServicesPeriodCheckParams { |
| 43 | + service_name: service_name.to_string(), |
| 44 | + check_request: Some(servicecontrol_v1::CheckRequest { |
| 45 | + operation: Some(Box::new(servicecontrol_v1::Operation { |
| 46 | + start_time: Some(chrono::Utc::now().to_rfc3339()), |
| 47 | + operation_id: Some(uuid::Uuid::new_v4().to_string()), |
| 48 | + operation_name: Some("Whatever".to_string()), |
| 49 | + consumer_id: GoogleEnvironment::detect_google_project_id() |
| 50 | + .await |
| 51 | + .map(|id| format!("project:{id}")), |
| 52 | + ..Default::default() |
| 53 | + })), |
| 54 | + ..Default::default() |
| 55 | + }), |
| 56 | + ..Default::default() |
| 57 | + }, |
| 58 | + ) |
| 59 | + .await?; |
| 60 | + |
| 61 | + if let Some(errs) = response.check_errors { |
| 62 | + Err(Error::ServiceCheckErrors(errs)) |
| 63 | + } else { |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[tokio::main] |
| 69 | +async fn main() -> Result<(), Error> { |
| 70 | + let client = GoogleRestApi::new().await?; |
| 71 | + services_check(client, "sandbox-lustre.sandbox.googleapis.com").await?; |
| 72 | + |
| 73 | + Ok(()) |
| 74 | +} |
0 commit comments