Ejemplo n.º 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
Ejemplo n.º 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