
    q                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlZ G d dej.                        Zy)zDelete images command.    )absolute_import)division)unicode_literals)docker_name)docker_session)util)base)flags)
exceptions)log)
console_io)resource_printerNc                   H    e Zd ZdZdddZed        Zd Zd Zd Z	d	 Z
d
 Zy)DeletezDelete existing images.

  The container images delete command of gcloud deletes a specified
  image and tags in a specified repository. Repositories
  must be hosted by the Google Container Registry.
  z          The container images delete command deletes the specified image from
          the registry. All associated tags are also deleted.
      ah            Deletes the image as long as there aren't additional, unspecified tags
          referencing it:

            $ {command} <IMAGE_NAME>

          Deletes the image (and tags) from the input IMAGE_NAME:

            $ {command} <IMAGE_NAME> --force-delete-tags

          Deletes the image (and tags) from the input IMAGE_NAME, without
          additional prompting:

            $ {command} <IMAGE_NAME> --force-delete-tags --quiet

          To easily identify and delete untagged images in a project, first
          filter digests that lack tags:

            $ gcloud container images list-tags [HOSTNAME]/[PROJECT-ID]/[IMAGE]              --filter='-tags:*'  --format="get(digest)" --limit=$BIG_NUMBER

          Then, delete these tagless images without prompting by running:

            $ {command} [HOSTNAME]/[PROJECT-ID]/[IMAGE]@DIGEST --quiet

      )DESCRIPTIONEXAMPLESc                 \    t        j                  | d       | j                  dddd       y)	zRegister flags for this command.

    Args:
      parser: An argparse.ArgumentParser-like object. It is mocked out in order
        to capture some information, but behaves like an ArgumentParser.
    delete)verbz--force-delete-tags
store_trueFzIf there are tags pointing to an image to be deleted then they must all be specified explicitly, or this flag must be specified, for the command to succeed.)actiondefaulthelpN)r
   AddTagOrDigestPositionaladd_argument)parsers    &lib/surface/container/images/delete.pyArgszDelete.ArgsL   s5     
""69
*  ,    c           	         t        j                         }t        j                         5  | j                  |j                        \  }}|D ]9  }|j                  t        j                  t        j                  |                   ; t               }|D ]'  }|j                  t        j                  ||             ) |j                  |      }|rh|j                  s\t        j                  d       |D ]-  }t        j                  dt        j                  |      z          / t!        j"                  d      |rt        j$                  j'                  d       |D ]  }| j)                  ||        |rt        j$                  j'                  d       |D ]7  }t        j$                  j'                  dt        j                  |      z          9 t+        j,                  ddd       |j                  |       g }	|D ]:  }| j/                  ||       |	j1                  dt        j                  |      i       < |D ]:  }| j/                  ||       |	j1                  dt        j                  |      i       < |	cd	d	d	       S # 1 sw Y   y	xY w)
ab  This is what ts called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Raises:
      InvalidImageNameError: If the user specified an invalid image name.
    Returns:
      A list of the deleted docker_name.Tag and docker_name.Digest objects
    zTags:- zThis operation will implicitly delete the tags listed above. Please manually remove with the `untag` command or re-run with --force-delete-tags to confirm.zDigests:zOThis operation will delete the tags and images identified by the digests above.T)r   cancel_on_nonameN)r   HttpWrapExpectedDockerlessErrors_ProcessImageNamesimage_namesaddGetDigestFromNamesix	text_typesetupdateGetDockerTagsForDigest
differenceforce_delete_tagsr   errorr   ErrorstatusPrint_PrintDigestr   PromptContinue_DeleteDockerTagOrDigestappend)
selfargshttp_objdigestsexplicit_tagstagall_tagsdigestimplicit_tagsresults
             r   Runz
Delete.Run_   s    yy{H		*	*	,#66t7G7GHg} #D**3==+=>?  h&33FHEF  ))-8m	t55		' C
))D3==--
. !./ 	/ 


$&&(+  


!#

c 223  	 =) f#%%c84vs}}S123  &%%fh7vs}}V456  o 
-	,	,s   IJJc                 t   t               }t               }|D ]  }t        j                  |      }t        |t        j
                        r|j                  |       Dt        |t        j                        s_t        j                  |      st        j                  d|z          |j                  |        ||gS )Nz"Implicit ":latest" tag specified: )r,   r   GetDockerImageFromTagOrDigest
isinstancer   Digestr(   TagIsFullySpecifiedr   warning)r9   r'   r<   tags
image_name
docker_objs         r   r&   zDelete._ProcessImageNames   s    eG5D!
55jAj	J 2 2	3Jj+//2$$Z0
++:ZG
H " T?r   c                     t        j                  t        j                         ||       t	        j
                  |       y )N)credsr#   	transport)r   r   r   CredentialProviderr   DeletedResource)r9   tag_or_digestr;   s      r   r7   zDelete._DeleteDockerTagOrDigest   s/    %%'mxQ&r   c                     t         j                  j                  dt        j                  |      z          | j                  ||       y )Nr!   )r   r3   r4   r*   r+   _DisplayDigestTags)r9   r@   r;   s      r   r5   zDelete._PrintDigest   s2    JJTCMM&112FH-r   c                     t        j                  ||      }|sy d}t        j                  ||t        j
                         y )Nz list[title="  Associated tags:"])out)r   GetTagNamesForDigestr   r4   r   r3   )r9   r@   r;   tag_listfmts        r   rU   zDelete._DisplayDigestTags   s5    ((:H-C8Scjj9r   N)__name__
__module____qualname____doc__detailed_helpstaticmethodr   rC   r&   r7   r5   rU    r   r   r   r   !   sH    


!-F , ,$GR'
.:r   r   )r^   
__future__r   r   r   containerregistry.clientr   containerregistry.client.v2_2r   'googlecloudsdk.api_lib.container.imagesr   googlecloudsdk.callioper	   $googlecloudsdk.command_lib.containerr
   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   r*   DeleteCommandr   ra   r   r   <module>rl      sE     &  ' 0 8 8 ( 6 * # 2 9 
b:T b:r   