Skip to content

Commit 058abdf

Browse files
committed
Add ServiceControl V1 API
Signed-off-by: Igor Pashev <pashev.igor@gmail.com>
1 parent 2fc3a0b commit 058abdf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+5198
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ members = [
1313
"examples/kms-client",
1414
"examples/id-token",
1515
"examples/iam-client",
16+
"examples/servicecontrol-client",
1617
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "servicecontrol-client"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
chrono = "0.4.39"
8+
gcloud-sdk = { path = "./../../gcloud-sdk", default-features = false, features = ["google-rest-servicecontrol-v1", "tls-webpki-roots"] }
9+
serde_json = "1.0.138"
10+
thiserror = "2.0.11"
11+
tokio = { version = "1.20", features = ["full"] }
12+
uuid = { version = "1.8", default-features = false, features = ["v4"] }
13+
14+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
// Forwarding the bearer.
38+
async fn services_check(
39+
client: GoogleRestApi,
40+
service_name: impl ToString,
41+
) -> Result<(), Error> {
42+
let cfg = client.create_google_servicecontrol_v1_config().await?;
43+
44+
let response = servicecontrol_v1::services_api::servicecontrol_services_check(
45+
&cfg,
46+
servicecontrol_v1::services_api::ServicecontrolPeriodServicesPeriodCheckParams {
47+
service_name: service_name.to_string(),
48+
check_request: Some(servicecontrol_v1::CheckRequest {
49+
operation: Some(Box::new(servicecontrol_v1::Operation {
50+
start_time: Some(chrono::Utc::now().to_rfc3339()),
51+
operation_id: Some(uuid::Uuid::new_v4().to_string()),
52+
operation_name: Some("Whatever".to_string()),
53+
consumer_id: GoogleEnvironment::detect_google_project_id()
54+
.await
55+
.map(|id| format!("project:{id}")),
56+
..Default::default()
57+
})),
58+
..Default::default()
59+
}),
60+
..Default::default()
61+
},
62+
)
63+
.await?;
64+
65+
if let Some(errs) = response.check_errors {
66+
Err(Error::ServiceCheckErrors(errs))
67+
} else {
68+
Ok(())
69+
}
70+
}
71+
72+
#[tokio::main]
73+
async fn main() -> Result<(), Error> {
74+
let client = GoogleRestApi::new().await?;
75+
services_check(client, "sandbox-lustre.sandbox.googleapis.com").await?;
76+
77+
Ok(())
78+
}

0 commit comments

Comments
 (0)