#google-cloud #gcp #api-client #google-api

google-cloud-artifact-registry

Google Cloud Platform Artifact Registry client library

8 releases (breaking)

0.7.1 Sep 22, 2024
0.7.0 Sep 2, 2024
0.6.0 Jul 12, 2024
0.5.0 Jun 27, 2024
0.1.0 Nov 21, 2023

#2082 in Web programming

Download history 32/week @ 2024-07-29 290/week @ 2024-08-05 161/week @ 2024-08-12 706/week @ 2024-08-19 635/week @ 2024-08-26 805/week @ 2024-09-02 979/week @ 2024-09-09 848/week @ 2024-09-16 930/week @ 2024-09-23 926/week @ 2024-09-30 416/week @ 2024-10-07 474/week @ 2024-10-14 231/week @ 2024-10-21 381/week @ 2024-10-28 530/week @ 2024-11-04 395/week @ 2024-11-11

1,538 downloads per month

MIT license

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 Client library.

crates.io

Installation

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

Quickstart

Authentication

There are two ways to create a client that is authenticated against the google cloud.

Automatically

The function with_auth() will try and read the credentials from a file specified in the environment variable GOOGLE_APPLICATION_CREDENTIALS, GOOGLE_APPLICATION_CREDENTIALS_JSON or from a metadata server.

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

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

Manually

When you can't use the gcloud authentication but you have a different way to get your credentials (e.g a different environment variable) you can parse your own version of the 'credentials-file' and use it like that:

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);
}

Usage

Repository operations

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 images operations

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())
        }
    }
}

Dependencies

~14–28MB
~531K SLoC