#google-cloud #gcp #google-api #artifacts-registry

google-cloud-artifact-registry

Google Cloud Platform Artifact Registry 客户端库

6 个版本 (重大变更)

0.6.0 2024年7月12日
0.5.0 2024年6月27日
0.4.0 2024年5月7日
0.3.0 2024年4月18日
0.1.0 2023年11月21日

#1994 in 网页编程

Download history 610/week @ 2024-04-13 523/week @ 2024-04-20 484/week @ 2024-04-27 205/week @ 2024-05-04 591/week @ 2024-05-11 285/week @ 2024-05-18 488/week @ 2024-05-25 1024/week @ 2024-06-01 460/week @ 2024-06-08 626/week @ 2024-06-15 317/week @ 2024-06-22 897/week @ 2024-06-29 991/week @ 2024-07-06 365/week @ 2024-07-13 151/week @ 2024-07-20 33/week @ 2024-07-27

1,802 每月下载量

MIT 许可证

8MB
139K SLoC

Bazel 112K SLoC // 0.1% comments Rust 27K SLoC // 0.0% comments Shell 181 SLoC // 0.3% comments Go 148 SLoC // 0.2% comments Forge Config 1 SLoC // 0.8% comments

google-cloud-artifact-registry

Google Cloud Platform Artifact Registry 客户端库。

crates.io

安装

[dependencies]
google-cloud-artifact-registry = "version"

快速入门

身份验证

有两种方式可以创建一个对 Google Cloud 进行身份验证的客户端。

自动

函数 with_auth() 将尝试从环境变量 GOOGLE_APPLICATION_CREDENTIALSGOOGLE_APPLICATION_CREDENTIALS_JSON 或元数据服务器中读取凭据。

use google_cloud_artifact_registry::client::{Client, ClientConfig};

async fn run() {
    let config = ClientConfig::default().with_auth().await.unwrap();
    let client = Client::new(config);
}

手动

当您无法使用 gcloud 身份验证但您有其他方式获取凭据(例如不同的环境变量)时,您可以解析自己的 'credentials-file' 版本并像那样使用它

use google_cloud_auth::credentials::CredentialsFile;
// or google_artifact_registry::client::google_cloud_auth::credentials::CredentialsFile
use google_cloud_artifact_registry::client::{Client, ClientConfig};

async fn run(cred: CredentialsFile) {
   let config = ClientConfig::default().with_credentials(cred).await.unwrap();
   let client = Client::new(config);
}

用法

仓库操作

use std::collections::HashMap;
use prost_types::FieldMask;
use google_cloud_artifact_registry::client::{Client, ClientConfig};
use google_cloud_googleapis::devtools::artifact_registry::v1::{CreateRepositoryRequest, DeleteRepositoryRequest, GetRepositoryRequest, ListRepositoriesRequest, Repository, UpdateRepositoryRequest};
use google_cloud_googleapis::devtools::artifact_registry::v1::repository::Format;
use google_cloud_googleapis::iam::v1::{GetIamPolicyRequest, Policy, SetIamPolicyRequest, TestIamPermissionsRequest};

async fn run(config: ClientConfig) {

    // Create client
    let mut client = Client::new(config).await.unwrap();

    // Repository
    // create
    match client
        .create_repository(
            CreateRepositoryRequest {
                parent: "projects/qovery-gcp-tests/locations/europe-west9".to_string(),
                repository_id: "repository-for-documentation".to_string(),
                repository: Some(Repository {
                    name: "repository-for-documentation".to_string(),
                    format: Format::Docker.into(),
                    description: "Example repository for documentation".to_string(),
                    labels: HashMap::from_iter(vec![
                        ("a_label".to_string(), "a_label_value".to_string()),
                        ("another_label".to_string(), "another_label_value".to_string()),
                    ]),
                    ..Default::default()
                }),
            },
            None,
        )
        .await
    {
        Ok(mut r) => println!("Created repository {:?}", r.wait(None).await.unwrap()),
        Err(err) => panic!("err: {:?}", err),
    };

    // update
    match client
       .update_repository(
           UpdateRepositoryRequest {
                repository: Some(Repository {
                    name: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                        .to_string(),
                    description: "updated description".to_string(),
                    labels: HashMap::from_iter(vec![(
                        "yet_another_label".to_string(),
                        "yet_another_label_value".to_string(),
                    )]),
                    ..Default::default()
                }),
                update_mask: Some(FieldMask {
                    paths: vec!["description".to_string(), "labels".to_string()],
                }),
            },
            None,
        )
        .await
    {
        Ok(r) => println!("Updated repository {:?}", r),
        Err(err) => panic!("err: {:?}", err),
    };

    // list
    match client
        .list_repositories(
            ListRepositoriesRequest {
                parent: "projects/qovery-gcp-tests/locations/europe-west9".to_string(),
                page_size: 100,
                page_token: "".to_string(),
            },
            None,
        )
        .await
    {
        Ok(response) => {
            println!("List repositories");
            for r in response.repositories {
                println!("- {:?}", r);
            }
        }
        Err(err) => panic!("err: {:?}", err),
    }

    // get
    match client
        .get_repository(
            GetRepositoryRequest {
                name: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                    .to_string(),
           },
            None,
        )
        .await
    {
        Ok(r) => println!("Get repository {:?}", r),
        Err(err) => panic!("err: {:?}", err),
    }

    // delete
    match client
        .delete_repository(
            DeleteRepositoryRequest {
                name: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                    .to_string(),
            },
            None,
        )
        .await
    {
        Ok(r) => println!("Delete repository `repository-for-documentation`"),
        Err(err) => panic!("err: {:?}", err),
    }

    // get repository IAM policy
    match client
        .get_iam_policy(
            GetIamPolicyRequest {
                resource: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                    .to_string(),
                ..Default::default()
            },
            None,
        )
        .await
    {
        Ok(policy) => println!("Get IAM Policy for `repository-for-documentation` {:?}", policy),
        Err(err) => panic!("err: {:?}", err),
    }

    // update repository IAM policy
    match client
        .set_iam_policy(
            SetIamPolicyRequest {
                resource: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                    .to_string(),
                policy: Some(Policy {
                    version: 3,
                    ..Default::default()
                }),
                update_mask: Some(FieldMask {
                    paths: vec!["policy.version".to_string()],
                }),
            },
            None,
        )
        .await
    {
        Ok(policy) => println!("Update IAM Policy for `repository-for-documentation` {:?}", policy),
        Err(err) => panic!("err: {:?}", err),
    }

// test IAM repository IAM policy
    match client
        .test_iam_permissions(
            TestIamPermissionsRequest {
                resource: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                    .to_string(),
                ..Default::default()
            },
            None,
        )
        .await
    {
        Ok(permissions) => {
            println!("Test permissions for `repository-for-documentation`, permissions:");
            for p in permissions {
                println!("- Permission: {}", p);
            }
        }
        Err(err) => panic!("err: {:?}", err),
    }

}

Docker 镜像操作

use google_cloud_artifact_registry::client::{Client, ClientConfig};
use google_cloud_googleapis::devtools::artifact_registry::v1::{GetDockerImageRequest, ListDockerImagesRequest};

async fn run(config: ClientConfig)  {

    // Create client.
    let mut client = Client::new(config).await.unwrap();

    // Docker images
    // list
    match client
        .list_docker_images(
            ListDockerImagesRequest {
                parent: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation"
                    .to_string(),
                ..Default::default()
            },
            None,
        )
        .await
    {
        Ok(response) => {
            println!("Docker images for repository `repository-for-documentation`: ");
            for image in response.docker_images {
                println!("- Image: {:?}", image);
            }
        }
        Err(e) => {
            println!("Error: {}", e);
            println!("Error details: {:?}", e.metadata())
        }
    }

    // get
    let result = client.get_docker_image(GetDockerImageRequest {
        name: "projects/qovery-gcp-tests/locations/europe-west9/repositories/repository-for-documentation/dockerImages/quickstart-image@sha256:2571d3a406da0ecafff96a9c707bc2eba954352dabc85dd918af2e3ec40c263a".to_string(),
    }, None).await;

    match result {
        Ok(d) => {
            println!("Image: {:?}", d);
        }
        Err(e) => {
            println!("Error: {}", e);
            println!("Error details: {:?}", e.metadata())
        }
    }
}

依赖关系

~14–28MB
~522K SLoC