yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
233 stars 85 forks source link

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: GAX(InvalidEmulatorHOST(""))', src/main.rs:15:45 #124

Closed lasagar closed 1 year ago

lasagar commented 1 year ago

thread 'main' panicked at 'called Result::unwrap() on an Err value: GAX(InvalidEmulatorHOST(""))', src/main.rs:15:45. How do i resolve this? I ran the code as is

yoshidan commented 1 year ago

if you are going to use emulator

if you are not going to use emulator google_cloud_default::WithAutoExt and call with_auth for ClientConfig is required https://github.com/yoshidan/google-cloud-rust-example/blob/547aa0f2e7ac81a09dc130617c841ba9bf4526ed/webapi/src/main.rs#L20

lasagar commented 1 year ago

I am using Google-cloud-pubsub, can you point me to that documentation with the info using client config

On Mon, Mar 27, 2023 at 7:28 PM Naohiro Yoshida @.***> wrote:

if you are going to use emulator

  • Set OS env var SPANNER_EMULATOR_HOST to use spanner emulator.
  • Set OS env var PUBSUB_EMULATOR_HOST to use pubsub emulator

if you are not going to use emulator google_cloud_default::WithAutoExt and call with_auth for ClientConfig is required

https://github.com/yoshidan/google-cloud-rust-example/blob/547aa0f2e7ac81a09dc130617c841ba9bf4526ed/webapi/src/main.rs#L20

— Reply to this email directly, view it on GitHub https://github.com/yoshidan/google-cloud-rust/issues/124#issuecomment-1486115599, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK7L5BFXZSH53HUQMEZ5TW6JEFNANCNFSM6AAAAAAWJ34KII . You are receiving this because you authored the thread.Message ID: @.***>

yoshidan commented 1 year ago

Here is the google-cloud-pubsub readme. You must use google-cloud-default together as described in the readme. https://github.com/yoshidan/google-cloud-rust/tree/main/pubsub

I am sorry but the method of using emulator is not described in the documentation and should be clearly stated. google-cloud-pubsub reads environment variables at the following location https://github.com/yoshidan/google-cloud-rust/blob/1043d7019daed0c7c4a0c178a3fbdddf98f14015/pubsub/src/client.rs#L32

lasagar commented 1 year ago

Hey, Thank you. How do I publish/subscribe to pubsub without using emulator. The problem i am facing is not sure how to pass the correct projectid. Following is the error. Can you pls help me with it. Thank you

Out put of Clientconfig where the project_id is none. ClientConfig { pool_size: Some( 4, ), project_id: None, environment: GoogleCloud( , ), endpoint: "pubsub.googleapis.com", }

Error Message

thread 'main' panicked at 'called Result::unwrap() on an Err value: ProjectIdNotFound', src/main.rs:16:45 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

yoshidan commented 1 year ago

It looks that no credential file is found.

You must set google credential file into os env 'GOOGLE_APPLICATION_CREDENTIALS' before calling with_auth. ClientConfig.default().with_auth() reads the project_id from GOOGLE_APPLICATION_CREDENTIALS outside the GCP without emulators.

export GOOGLE_APPLICATION_CREDENTIALS=/path/your_credentials.json
let config = ClientConfig::default().with_auth().await.unwrap();
let client = Client::new(config).await.unwrap();
lasagar commented 1 year ago

Thank you, I had set the GOOGLE_APPLICATION_CREDENTIALS, still the same. When I print the config, its showing me the project_id is None.

ClientConfig { pool_size: Some( 4, ), project_id: None, environment: GoogleCloud( , ), endpoint: "pubsub.googleapis.com", } thread 'main' panicked at 'called Result::unwrap() on an Err value: ProjectIdNotFound', src/main.rs:16:45

lasagar commented 1 year ago

Also in the client.rs, I noticed that its looking for PUBSUB_EMULATOR_HOST instead of GOOGLE_APPLICATION_CREDENTIALS.

/// ClientConfigs created by default will prefer to use PUBSUB_EMULATOR_HOST impl Default for ClientConfig { fn default() -> Self { let emulator = var("PUBSUB_EMULATOR_HOST").ok(); let default_project_id = emulator.asref().map(|| "local-project".to_string()); Self { pool_size: Some(4), environment: match emulator { Some(v) => Environment::Emulator(v), None => Environment::GoogleCloud(Box::new(NopeTokenSourceProvider {})), }, project_id: default_project_id, endpoint: PUBSUB.to_string(), } } }

Is my understanding correct? Pls suggest so that i can test the publish message and subscribe message later

yoshidan commented 1 year ago

ClientConfig::default() is configured to use emulator, but gets the project_id from credentials by calling google-cloud-default's with_auth method.

https://github.com/yoshidan/google-cloud-rust/blob/1043d7019daed0c7c4a0c178a3fbdddf98f14015/default/src/lib.rs#L32

If the project_id is empty even though with_auth is called, then the project_id in the credentials file is empty. I do not know what kind of credentials file you are using, but it is possible to specify the project_id directly in ClientConfig.

let mut config = ClientConfig::default().with_auth()
config.project_id = Some("your_project_id".to_string());
lasagar commented 1 year ago

Thank you, tried hard coding now and it worked.

On Wed, Mar 29, 2023 at 5:19 PM Naohiro Yoshida @.***> wrote:

ClientConfig::default() is configured to use emulator, but gets the project_id from credentials by calling google-cloud-default's with_auth method.

https://github.com/yoshidan/google-cloud-rust/blob/1043d7019daed0c7c4a0c178a3fbdddf98f14015/default/src/lib.rs#L32

If the project_id is empty even though with_auth is called, then the project_id in the credentials file is empty. I do not know what kind of credentials file you are using, but it is possible to specify the project_id directly in ClientConfig.

let mut config = ClientConfig::default().with_auth() config.project_id = Some("your_project_id".to_string());

— Reply to this email directly, view it on GitHub https://github.com/yoshidan/google-cloud-rust/issues/124#issuecomment-1489512566, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK7L3ZN44VIHGZAGHX6XDW6TGPXANCNFSM6AAAAAAWJ34KII . You are receiving this because you authored the thread.Message ID: @.***>

open-schnick commented 1 year ago

If you are still having trouble the documentation was updated in #126

drewgatchell commented 1 year ago

If the project_id is empty even though with_auth is called, then the project_id in the credentials file is empty.

This happens with gcloud auth login --update-adc

Credentials File ends up as:

{
  "client_id": <client_id>,
  "client_secret": <client_secret>,
  "refresh_token": <refresh_token>,
  "type": "authorized_user"
}
yoshidan commented 1 year ago

If the credentials does not include the project_id, the project_id must explicitly set the project_id.

let mut pubsub_config = google_cloud_pubsub::client::ClientConfig::default().with_auth().await?;
pubsub_config.project_id = Some(xxx);