Skip to content

Commit

Permalink
Add ServiceControl V1 API (#189)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Pashev <[email protected]>
  • Loading branch information
ip1981 authored Feb 28, 2025
1 parent 1e26a2e commit a433e7e
Show file tree
Hide file tree
Showing 68 changed files with 5,197 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ members = [
"examples/id-token",
"examples/iam-client",
"examples/artifact-registry-client",
"examples/servicecontrol-client",
]
14 changes: 14 additions & 0 deletions examples/servicecontrol-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "servicecontrol-client"
version = "0.1.0"
edition = "2021"

[dependencies]
chrono = "0.4.39"
gcloud-sdk = { path = "./../../gcloud-sdk", default-features = false, features = ["google-rest-servicecontrol-v1", "tls-webpki-roots"] }
serde_json = "1.0.138"
thiserror = "2.0.11"
tokio = { version = "1.20", features = ["full"] }
uuid = { version = "1.8", default-features = false, features = ["v4"] }


77 changes: 77 additions & 0 deletions examples/servicecontrol-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use gcloud_sdk::{google_rest_apis::servicecontrol_v1, GoogleEnvironment, GoogleRestApi};

#[derive(thiserror::Error, Debug)]
enum Error {
#[error(transparent)]
Gcloud(#[from] gcloud_sdk::error::Error),
#[error(transparent)]
ServiceCheck(
#[from]
servicecontrol_v1::Error<
servicecontrol_v1::services_api::ServicecontrolPeriodServicesPeriodCheckError,
>,
),
#[error("{}", format_check_errors(.0))]
ServiceCheckErrors(Vec<servicecontrol_v1::CheckError>),
}

fn format_check_errors(errs: &[servicecontrol_v1::CheckError]) -> String {
errs.iter()
.filter_map(|e| {
if let Some(ref code) = e.code {
let c = serde_json::to_string(code)
.unwrap_or_else(|_| "ERROR_CODE_UNSPECIFIED".to_string());
if let Some(ref detail) = e.detail {
Some(format!("{c}: {detail}"))
} else {
Some(c)
}
} else {
None
}
})
.collect::<Vec<_>>()
.join(", ")
}

async fn services_check(
client: GoogleRestApi,
service_name: impl ToString,
) -> Result<(), Error> {
let cfg = client.create_google_servicecontrol_v1_config().await?;

let response = servicecontrol_v1::services_api::servicecontrol_services_check(
&cfg,
servicecontrol_v1::services_api::ServicecontrolPeriodServicesPeriodCheckParams {
service_name: service_name.to_string(),
check_request: Some(servicecontrol_v1::CheckRequest {
operation: Some(Box::new(servicecontrol_v1::Operation {
start_time: Some(chrono::Utc::now().to_rfc3339()),
operation_id: Some(uuid::Uuid::new_v4().to_string()),
operation_name: Some("Whatever".to_string()),
consumer_id: GoogleEnvironment::detect_google_project_id()
.await
.map(|id| format!("project:{id}")),
..Default::default()
})),
..Default::default()
}),
..Default::default()
},
)
.await?;

if let Some(errs) = response.check_errors {
Err(Error::ServiceCheckErrors(errs))
} else {
Ok(())
}
}

#[tokio::main]
async fn main() -> Result<(), Error> {
let client = GoogleRestApi::new().await?;
services_check(client, "sandbox-lustre.sandbox.googleapis.com").await?;

Ok(())
}
Loading

0 comments on commit a433e7e

Please sign in to comment.