Search code examples
google-cloud-platformgoogle-container-registrygoogle-artifact-registry

google.api_core.exceptions.InvalidArgument: 400 invalid repository


I am trying to create a repository in Artifact registry of GCP using Python API by following below link.

https://cloud.google.com/python/docs/reference/artifactregistry/latest/google.cloud.artifactregistry_v1.services.artifact_registry.ArtifactRegistryClient#google_cloud_artifactregistry_v1_services_artifact_registry_ArtifactRegistryClient_create_repository

Below is the code I am using.

from google.cloud import artifactregistry_v1

def sample_create_repository():
    # Create a client

    print("Testing the code")

    client = artifactregistry_v1.ArtifactRegistryClient.from_service_account_file(filename='<path_to_sa.json>')

    # Initialize request argument(s)
    
    request = artifactregistry_v1.CreateRepositoryRequest(parent="projects/p1/locations/us",repository={'name':'projects/p1/locations/us/repositories/testrepo','format_':'DOCKER'})

    # Make the request
    operation = client.create_repository(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)

sample_create_repository()

I am getting below error for this code, Can anyone from the community suggest the correct format here,

    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 invalid repository: generic::unknown: invalid repository name: "projects/p1/locations/us/repositories/": invalid repository name: projects/p1/locations/us/repositories/

parent format I have taken this doc as reference - https://cloud.google.com/artifact-registry/docs/reference/rest

repository format I have taken this doc as reference - https://cloud.google.com/python/docs/reference/artifactregistry/latest/google.cloud.artifactregistry_v1.types.Repository


Solution

  • You need to provide the repository id in the CreateRepositoryRequest itself. Repository.Name is ignored for Create. (Most Google apis will behave similarly, see https://google.aip.dev/133#user-specified-ids)