Exemplo 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
Exemplo n.º 2
0
def GetRedirectionEnablementReport(project):
    """Prints a redirection enablement report and returns mis-configured repos.

  This checks all the GCR repositories in the supplied project and checks if
  they each have a repository in Artifact Registry create to be the redirection
  target. It prints a report as it validates.

  Args:
    project: The project to validate

  Returns:
    A list of the GCR repos that do not have a redirection repo configured in
    Artifact Registry.
  """
    locations = ar_requests.ListLocations(project, 100)

    # Sorting is performed so that Python2 & 3 agree on the order of API calls
    # in scenario tests.
    gcr_repos = GetExistingGCRBuckets(
        sorted(_GCR_BUCKETS.values(), key=lambda x: x["repository"]), project)

    failed_repos = []
    repo_report = []
    report_line = []
    con = console_attr.GetConsoleAttr()

    # For each gcr repo in a location that our environment supports,
    # is there an associated repo in AR?
    for gcr_repo in gcr_repos:
        if gcr_repo["location"] in locations:
            report_line = [gcr_repo["repository"], gcr_repo["location"]]
            ar_repo_name = "projects/{}/locations/{}/repositories/{}".format(
                project, gcr_repo["location"], gcr_repo["repository"])
            try:
                ar_repo = ar_requests.GetRepository(ar_repo_name)
                report_line.append(con.Colorize(ar_repo.name, "green"))
            except apitools_exceptions.HttpNotFoundError:
                report_line.append(con.Colorize("NOT FOUND", "red"))
                failed_repos.append(gcr_repo)
            repo_report.append(report_line)

    log.status.Print("Redirection enablement report:\n")
    printer = resource_printer.Printer("table", out=log.status)
    printer.AddHeading([
        con.Emphasize("Container Registry Host", bold=True),
        con.Emphasize("Location", bold=True),
        con.Emphasize("Artifact Registry Repository", bold=True)
    ])
    for line in repo_report:
        printer.AddRecord(line)
    printer.Finish()
    log.status.Print()
    return failed_repos
Exemplo n.º 3
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
Exemplo n.º 4
0
def GetLocationList(args):
    return ar_requests.ListLocations(GetProject(args), args.page_size)
Exemplo n.º 5
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)))
Exemplo n.º 6
0
def GetLocationList(args):
    return ar_requests.ListLocations(GetProject(args))