-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does this work with compute endpoint? #106
Comments
Hey, some of the Google APIs are not publicly available through gRPC. So, if you want to work with it you need to use REST API. I don't think it is included in this project yet, so PR is welcome: |
It seems Open API spec is available: |
@abdolence I attempted to generate but get this error for the CLI
|
I'll have a look if this is solvable or the error is in open-api generator. (Of course fallback option is to use only REST client with Google Authentication and build models manually/externally - so it will be just like Reqwest + Google Authentication without models and interface generated). |
I was also thinking if the grpc request/response structs also had serde support I could use that with a pretty simple request client. |
I don't think prost does that and they also have some references to additional gRPC types from Google (such as timestamps) |
Could the prost-serde help? This is my current solution, I was able to get serde structs from another crate use eyre::Result;
use gcp_auth::{AuthenticationManager, Token};
use google_compute1::api::InstanceGroupAggregatedList;
use reqwest::Client;
pub struct GoogleEnvironment {
pub project_id: String,
}
impl GoogleEnvironment {
pub async fn try_new() -> Result<Self> {
let for_env = std::env::var("GCP_PROJECT")
.ok()
.or_else(|| std::env::var("PROJECT_ID").ok())
.or_else(|| std::env::var("GCP_PROJECT_ID").ok());
if let Some(project_id) = for_env {
return Ok(Self { project_id });
}
if !google_cloud_metadata::on_gce().await {
return Err(eyre::eyre!(
"Not on GCE and GPC_PROJECT | PROJECT_ID | GCP_PROJECT_ID not set"
));
}
let project_id = google_cloud_metadata::project_id().await;
Ok(Self { project_id })
}
}
pub struct Auth {
pub scopes: &'static [&'static str],
pub env: GoogleEnvironment,
pub manager: AuthenticationManager,
}
impl Auth {
pub async fn try_new() -> Result<Self> {
let manager = AuthenticationManager::new().await?;
let scopes = &["https://www.googleapis.com/auth/cloud-platform"];
let env = GoogleEnvironment::try_new().await?;
Ok(Self {
scopes,
env,
manager,
})
}
pub async fn get_token(&self) -> Result<Token> {
let token = self.manager.get_token(self.scopes).await?;
Ok(token)
}
}
pub struct ComputeEngine {
auth: Auth,
client: Client,
base_url: String,
}
impl ComputeEngine {
pub async fn try_new() -> Result<Self> {
let auth = Auth::try_new().await?;
let project = &auth.env.project_id;
let base_url = format!("https://compute.googleapis.com/compute/v1/projects/{project}");
Ok(Self {
client: Client::new(),
auth,
base_url,
})
}
pub async fn list_gpu_instances(&self) -> Result<()> {
let url = format!("{}/aggregated/instanceGroups", self.base_url,);
let token = self.auth.get_token().await?;
let resp = self
.client
.get(&url)
.query(&[("filter", r#"name eq ".*gpu*""#)])
.bearer_auth(token.as_str())
.send()
.await?;
let instances: InstanceGroupAggregatedList = resp.json().await?;
// let instances = resp.text().await?;
println!("{:#?}", instances);
Ok(())
}
} |
Ok, I managed to fix Open API generator upgrading it to the next version and providing additional parameter. let google_project_id = gcloud_sdk::GoogleEnvironment::detect_google_project_id().await
.expect("No Google Project ID detected. Please specify it explicitly using env variable: PROJECT_ID");
let google_rest_client = gcloud_sdk::GoogleRestApi::new().await.unwrap();
let response = gcloud_sdk::google_rest_apis::compute_v1::instances_api::compute_instances_list(
&google_rest_client.create_google_compute_v1_config().await.unwrap(),
gcloud_sdk::google_rest_apis::compute_v1::instances_api::ComputePeriodInstancesPeriodListParams {
project: google_project_id.to_string(),
zone: "us-central1-a".to_string(),
dollar_xgafv: None,
access_token: None,
alt: None,
callback: None,
fields: None,
key: None,
oauth_token: None,
pretty_print: None,
quota_user: None,
upload_protocol: None,
upload_type: None,
user_ip: None,
filter: None,
max_results: None,
order_by: None,
page_token: None,
return_partial_success: None,
}
).await.unwrap(); Unfortunately in the new version of Open API generator, they removed Help with the testing would be appreciated (you can refer to a git feature branch in your deps directly) |
Thats great! Let me try it out now. I'll let you know how it goes. |
Tested it works, thanks! |
Let me contemplate a bit about what to do with |
@abdolence thanks! This is working well my only problem is it adds a lot of compile time. Would it be possible to add more features to only select the parts of the compute API that I want? |
@praveenperera the compute engine's spec is huge (~3 Mb), and it generated a lot of stuff as I can see. The problem with the feature toggles is that this crate already has around 400+ features, and I had to ask the maintainers of If it is unbearable it needs some kind of workarounds, like extracting CE into separate crate with its own features and using just REST client. |
Ya it's huge not sure what to do about it. On a release build on my M1 Studio (Apple M1 Max) It adds about 30 seconds to the build time. |
I usually run release builds on pipelines. They often requires a lot of time. |
I'm trying to get this to work with the compute endpoint
But I get
The text was updated successfully, but these errors were encountered: