Example #1
0
def ListRepositories(args):
    """Lists repositories in a given project.

  If no location value is specified, list repositories across all locations.

  Args:
    args: User input arguments.

  Returns:
    List of repositories.
  """
    project = GetProject(args)
    location = args.location or properties.VALUES.artifacts.location.Get()
    location_list = ar_requests.ListLocations(project)
    if location and location.lower(
    ) not in location_list and location != "all":
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                location, ", ".join(location_list)))

    loc_paths = []
    if location and location != "all":
        log.status.Print(
            "Listing items under project {}, location {}.\n".format(
                project, location))
        loc_paths.append("projects/{}/locations/{}".format(project, location))
        buckets = [_GCR_BUCKETS[location]] if location in _GCR_BUCKETS else []
    else:
        log.status.Print(
            "Listing items under project {}, across all locations.\n".format(
                project))
        loc_paths.extend([
            "projects/{}/locations/{}".format(project, loc)
            for loc in location_list
        ])
        buckets = _GCR_BUCKETS.values()

    pool_size = len(loc_paths) if loc_paths else 1
    pool = parallel.GetPool(pool_size)
    page_size = args.page_size
    try:
        pool.Start()
        results = pool.Map(
            lambda x: ar_requests.ListRepositories(x, page_size=page_size),
            loc_paths)
    except parallel.MultiError as e:
        error_set = set(err.content for err in e.errors)
        msg = "\n".join(error_set)
        raise ar_exceptions.ArtifactRegistryError(msg)
    finally:
        pool.Join()

    repos = []
    for sublist in results:
        repos.extend([repo for repo in sublist])
    repos.sort(key=lambda x: x.name.split("/")[-1])

    return repos, buckets, project
Example #2
0
def ListRepositories(args):
    """List repositories in a given project.

  If no location value is specified, list repositories across all locations.

  Args:
    args: User input arguments.

  Returns:
    List of repositories.
  """
    project = GetProject(args)
    location = args.location or properties.VALUES.artifacts.location.Get()
    if location and not IsValidLocation(location) and location != "all":
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                location, ", ".join(_VALID_LOCATIONS)))

    loc_paths = []
    if location and location != "all":
        log.status.Print(
            "Listing items under project {}, location {}.\n".format(
                project, location))
        loc_paths.append("projects/{}/locations/{}".format(project, location))
        buckets = [_GCR_BUCKETS[location]] if location in _GCR_BUCKETS else []
    else:
        log.status.Print(
            "Listing items under project {}, across all locations.\n".format(
                project))
        loc_paths.extend([
            "projects/{}/locations/{}".format(project, loc)
            for loc in _VALID_LOCATIONS
        ])
        buckets = _GCR_BUCKETS.values()

    client = ar_requests.GetClient()
    messages = ar_requests.GetMessages()
    repos = []
    for loc in loc_paths:
        repos.extend(ar_requests.ListRepositories(client, messages, loc))
    gcr_repos = _GetGCRRepos(buckets, project)
    if gcr_repos:
        repos.extend(gcr_repos)
        log.status.Print(
            "Note: To perform actions on the Container Registry repositories "
            "listed below please use 'gcloud container images'.\n")

    return repos
Example #3
0
def AppendRepoDataToRequest(repo_ref, repo_args, request):
    """Adds repository data to CreateRepositoryRequest."""
    if not _IsValidRepoName(repo_ref.repositoriesId):
        raise ar_exceptions.InvalidInputValueError(_INVALID_REPO_NAME_ERROR)
    if not _IsValidLocation(repo_args.location):
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                repo_args.location, ", ".join(_VALID_LOCATIONS)))
    messages = _GetMessagesForResource(repo_ref)
    repo = messages.Repository(
        name=repo_ref.RelativeName(),
        description=repo_args.description,
        format=messages.Repository.FormatValueValuesEnum(
            repo_args.repository_format.upper()))
    request.repository = repo
    request.repositoryId = repo_ref.repositoriesId
    return request
Example #4
0
def _GetLocationAndRepoPath(args, repo_format):
    """Get resource values and validate user input."""
    repo = _GetRequiredRepoValue(args)
    project = _GetRequiredProjectValue(args)
    location = _GetRequiredLocationValue(args)
    repo_path = project + "/" + repo
    location_list = ar_requests.ListLocations(project)
    if location.lower() not in location_list:
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                location, ", ".join(location_list)))
    repo = ar_requests.GetRepository(
        "projects/{}/locations/{}/repositories/{}".format(
            project, location, repo))
    if repo.format != repo_format:
        raise ar_exceptions.InvalidInputValueError(
            "Invalid repository type {}. Valid type is {}.".format(
                repo.format, repo_format))
    return location, repo_path
Example #5
0
def _ParseInput(input_str):
    """Parses user input into project, location, and repository values.

  Args:
    input_str: str, user input. Ex: us-docker.pkg.dev/my-proj/my-repo/my-img

  Raises:
    ar_exceptions.InvalidInputValueError if user input is invalid.
    ar_exceptions.UnsupportedLocationError if provided location is invalid.

  Returns:
    A DockerRepo.
  """
    matches = re.match(DOCKER_REPO_REGEX, input_str)
    if not matches:
        raise ar_exceptions.InvalidInputValueError()
    location = matches.group("location")
    if not util.IsValidLocation(location):
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                location, ", ".join(util.GetLocationList())))
    return DockerRepo(matches.group("project"), location,
                      matches.group("repo"))
Example #6
0
def ValidateLocation(location, project_id):
    location_list = ar_requests.ListLocations(project_id)
    if location.lower() not in location_list:
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                location, ", ".join(location_list)))
Example #7
0
def ValidateLocation(unused_ref, args, req):
    if not IsValidLocation(GetLocation(args)):
        raise ar_exceptions.UnsupportedLocationError(
            "{} is not a valid location. Valid locations are [{}].".format(
                args.location, ", ".join(_VALID_LOCATIONS)))
    return req