def _ParseDockerImage(img_str, err_msg):
    """Validates and parses an image string into a DockerImage.

  Args:
    img_str: str, User input docker formatted string.
    err_msg: str, Error message to return to user.

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

  Returns:
    A DockerImage, and a DockerTag or a DockerVersion.
  """
    try:
        docker_repo = _ParseInput(img_str)
    except ar_exceptions.InvalidInputValueError:
        raise ar_exceptions.InvalidInputValueError(_INVALID_DOCKER_IMAGE_ERROR)

    ar_util.ValidateLocation(docker_repo.location, docker_repo.project)

    img_by_digest_match = re.match(DOCKER_IMG_BY_DIGEST_REGEX, img_str)
    if img_by_digest_match:
        docker_img = DockerImage(docker_repo, img_by_digest_match.group("img"))
        return docker_img, DockerVersion(docker_img,
                                         img_by_digest_match.group("digest"))
    img_by_tag_match = re.match(DOCKER_IMG_BY_TAG_REGEX, img_str)
    if img_by_tag_match:
        docker_img = DockerImage(docker_repo, img_by_tag_match.group("img"))
        return docker_img, DockerTag(docker_img, img_by_tag_match.group("tag"))
    whole_img_match = re.match(DOCKER_IMG_REGEX, img_str)
    if whole_img_match:
        return DockerImage(docker_repo,
                           whole_img_match.group("img").strip("/")), None
    raise ar_exceptions.InvalidInputValueError(err_msg)
Example #2
0
def DeleteDockerTag(args):
  """Deletes a Docker tag."""
  img, tag = _ParseDockerTag(args.DOCKER_TAG)

  ar_util.ValidateLocation(img.docker_repo.location, img.docker_repo.project)
  _ValidateDockerRepo(img.docker_repo.GetRepositoryName())

  console_io.PromptContinue(
      message="You are about to delete tag [{}]".format(tag.GetDockerString()),
      cancel_on_no=True)
  ar_requests.DeleteTag(ar_requests.GetClient(), ar_requests.GetMessages(),
                        tag.GetTagName())
  log.status.Print("Deleted tag [{}].".format(tag.GetDockerString()))
def _GetDefaultResources():
    """Gets default config values for project, location, and repository."""
    project = properties.VALUES.core.project.Get()
    location = properties.VALUES.artifacts.location.Get()
    ar_util.ValidateLocation(location, project)
    repo = properties.VALUES.artifacts.repository.Get()
    if not project or not location or not repo:
        raise ar_exceptions.InvalidInputValueError(
            _INVALID_DEFAULT_DOCKER_STRING_ERROR.format(**{
                "project": project,
                "location": location,
                "repo": repo,
            }))
    return DockerRepo(project, location, repo)
def _ParseDockerImagePath(img_path):
    """Validates and parses an image path into a DockerImage or a DockerRepo."""
    if not img_path:
        return _GetDefaultResources()

    resource_val_list = list(filter(None, img_path.split("/")))
    try:
        docker_repo = _ParseInput(img_path)
    except ar_exceptions.InvalidInputValueError:
        raise ar_exceptions.InvalidInputValueError(_INVALID_IMAGE_PATH_ERROR)

    ar_util.ValidateLocation(docker_repo.location, docker_repo.project)

    if len(resource_val_list) == 3:
        return docker_repo
    elif len(resource_val_list) > 3:
        return DockerImage(docker_repo, "/".join(resource_val_list[3:]))
    raise ar_exceptions.InvalidInputValueError(_INVALID_IMAGE_PATH_ERROR)