Cloud Shell에서 Rust 설정

Cloud Shell은 예시를 실행하고 Google Cloud API를 테스트할 수 있는 브라우저 기반 명령줄 환경을 제공합니다. 이 문서에서는 Cloud Shell에서 Rust용 Cloud 클라이언트 라이브러리를 사용하여 Rust 도구 모음을 구성하고 Secret Manager 샘플을 실행하는 방법을 보여줍니다.

시작하기 전에

이 문서의 명령어를 실행하기 전에 결제가 사용 설정된 Google Cloud 프로젝트를 선택하거나 만듭니다.

Cloud Shell 시작

다음 단계를 완료하여 프로젝트를 선택하고 Cloud Shell 세션을 엽니다.

  1. Google Cloud 콘솔에서 Google Cloud 프로젝트를 선택하거나 만듭니다.

    프로젝트 선택기로 이동

  2. Google Cloud 콘솔 상단에서 Cloud Shell 활성화를 클릭하여 셸 세션을 시작합니다. 메시지가 표시되면 Cloud Shell에서 사용자 인증 정보를 사용하여 Google Cloud API 호출을 할 수 있도록 승인합니다.

Rust 구성

Cloud Shell에는 rustup가 사전 설치되어 있습니다. 다음 단계를 완료하여 Rust 도구 모음을 구성하고 확인합니다.

  1. 안정적인 Rust 도구 모음을 기본값으로 구성합니다.

    rustup default stable
    
  2. Cargo 및 Rust 설치를 확인합니다.

    cargo --version
    

Cloud Shell에 클라이언트 라이브러리 설치

API를 사용 설정하고, 종속 항목을 추가하고, 샘플 프로그램을 실행하려면 다음 단계를 완료하세요.

  1. 프로젝트에서 Secret Manager API를 사용 설정합니다.

    gcloud services enable secretmanager.googleapis.com
    
  2. 바이너리 크레이트를 만들고 해당 디렉터리로 변경합니다.

    cargo new my-project
    cd my-project
    
  3. Secret Manager 클라이언트 라이브러리, google-cloud-gax, tokio를 프로젝트에 추가합니다.

    cargo add google-cloud-secretmanager-v1 google-cloud-gax
    cargo add tokio --features macros,rt-multi-thread
    
  4. src/main.rs의 내용을 다음 코드로 바꿉니다.

    #[tokio::main]
    async fn main() -> Result<(), Box<dyn std::error::Error>> {
        use google_cloud_gax::paginator::ItemPaginator as _;
        use google_cloud_secretmanager_v1::client::SecretManagerService;
        let project_id = std::env::args().nth(1).unwrap();
        let client = SecretManagerService::builder().build().await?;
    
        let mut items = client
            .list_secrets()
            .set_parent(format!("projects/{project_id}"))
            .by_item();
        while let Some(item) = items.next().await {
            println!("{}", item?.name);
        }
        Ok(())
    }

  5. Google Cloud 프로젝트 ID를 인수로 전달하여 프로그램을 실행합니다.

    PROJECT_ID=$(gcloud config get project)
    cargo run -- "${PROJECT_ID}"
    

    프로그램은 프로젝트의 보안 비밀 이름을 출력합니다. 프로젝트에 아직 보안 비밀이 없으면 보안 비밀을 만들고 명령어를 다시 실행하여 출력에 표시합니다.

삭제

이 문서에서 사용한 리소스에 대한 비용이 Google Cloud 계정에 청구되지 않게 하려면 만든 테스트 보안 비밀을 삭제합니다. 완료되면 Cloud Shell 홈 디렉터리에서 my-project 디렉터리를 삭제합니다.