The Cloud Client Libraries for Rust use client structs as the main interface to Google Cloud services, with methods corresponding to each RPC offered by the service. Before calling a service, you initialize its client.
This document shows you how to initialize a Secret Manager client with its default configuration and call an RPC. The same pattern applies to all other Google Cloud services.
Before you begin
Before running the code in this document, complete the following prerequisites:
- Follow Create and access a secret using Secret Manager to set up a Google Cloud project and enable the Secret Manager API.
- Configure Application Default Credentials for your local environment. For more information, see Authenticate with client libraries.
Dependencies
Add the required crates to your Cargo.toml file:
cargo add google-cloud-secretmanager-v1 google-cloud-gax anyhow
cargo add tokio --features macros,rt-multi-thread
Initialize a client and make an RPC
To initialize a client, call builder on the client struct to obtain a
ClientBuilder and then call build. Complete the following
steps to initialize a client with default settings and call an RPC:
Import the
SecretManagerServiceclient type:Build the client with its default configuration:
Use the initialized client to call
list_locations, which lists the locations supported by Secret Manager for your project:
The output resembles the following:
projects/PROJECT_NUMBER/locations/europe-west8
projects/PROJECT_NUMBER/locations/europe-west9
projects/PROJECT_NUMBER/locations/us-east5
In this output, PROJECT_NUMBER is the numeric project
number for your Google Cloud project.
Complete code
The following example combines client initialization and the list_locations
call into a runnable program: