
    r                         d Z ddlmZ ddlmZ ddlmZ ddl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  G d dej*                        Z G d dej.                        Zy)zThe Delete command.    )absolute_import)division)unicode_literalsN)appengine_api_client)service_util)version_util)base)
exceptions)log)
console_io)resource_printer)textc                       e Zd ZdZy)VersionsDeleteErrorz(Errors occurring when deleting versions.N)__name__
__module____qualname____doc__     "lib/surface/app/versions/delete.pyr   r   #   s    0r   r   c                   .    e Zd ZdZddiZed        Zd Zy)DeletezoDelete a specified version.

  You cannot delete a version of a service that is currently receiving traffic.
  EXAMPLESa            To delete a specific version of a specific service, run:

            $ {command} --service=myService v1

          To delete a named version across all services, run:

            $ {command} v1

          To delete multiple versions of a specific service, run:

            $ {command} --service=myService v1 v2

          To delete multiple named versions across all services, run:

            $ {command} v1 v2
          c                 T    | j                  ddd       | j                  ddd       y )	Nversions+zCThe versions to delete (optionally filtered by the --service flag).)nargshelpz	--servicez-sz:If specified, only delete versions from the given service.)r   )add_argument)parsers    r   ArgszDelete.ArgsB   s9    

#M  P
T/  1r   c                 <   t        j                  | j                               }|j                         }|j                  r)|D cg c]  }|j
                  |j                  k(  s|  }}|j                  |      }t        t        j                  ||j                  |j                        t              }g }t        |      D ]  }t        |D 	cg c]  }	|	j                  |j
                  k(  s|	  c}	      }
t        |D 	cg c]  }	|	j                  |j
                  k(  s|	  c}	      }|
|k(  so|
dkD  su|j
                  dk(  rt        d      |j                  |       t        j                  |      D ]-  }|j                  |j
                  k(  s|j!                  |       /  |D ]L  }|j"                  st        dj%                  |j
                  |j"                  dz  |j                               |rt'        j(                  t        |      d      }t+        j,                  d	j%                  |             t/        j0                  |d
t*        j2                         t5        j6                  dj%                  |      d       t9        j:                  ||       |r?d}t/        j0                  ||t*        j2                         t5        j6                  d       n|st+        j,                  d       t        j<                  ||       y c c}w c c}	w c c}	w )N)keyr   defaultzXThe default service (module) may not be deleted, and must comprise at least one version.a-  Version [{version}] is currently serving {allocation:.2f}% of traffic for service [{service}].

Please move all traffic away via one of the following methods:
 - deploying a new version with the `--promote` argument
 - running `gcloud app services set-traffic`
 - running `gcloud app versions migrate`d   )version
allocationservicer)   zBRequested deletion of all existing versions for the following {0}:list)outz^
You cannot delete all versions of a service. Would you like to delete the entire {0} instead?T)prompt_stringcancel_on_noz.list[title="Deleting the following versions:"])r-   zNo matching versions found.)r   GetApiClientForTrackReleaseTrackListServicesr)   idListVersionssortedr   GetMatchingVersionsr   strlenr   appendcopyremovetraffic_splitformatr   	Pluralizer   warningr   Printstatusr   PromptContinuer   DeleteServicesDeleteVersions)selfargsclientservicessall_versionsr   services_to_deleter)   vservice_versionsversions_to_deleter'   wordfmts                  r   Runz
Delete.RunJ   s   !66t7H7H7JKF""$H ||%>X)=!Xh>&&x0Ll66|7;}}7;||EJMOH (#"
>laii7::&=1l
>@8O8aqyyGJJ7N8OP	/	/4Dq4H::"#/ 
 
#
#G
,yy*G__

*OOG$ + $  			!7
 8>v

"0036 8> 8)	* 		*  ^^C 23Y?d	kk
N6$< /SZZH+,2F4LtM !!&*<=<cXs

;T2121u ? ?Os$   L#LL
,L
 L
L
N)r   r   r   r   detailed_helpstaticmethodr"   rO   r   r   r   r   r   (   s2      -( 1 1A2r   r   )r   
__future__r   r   r   r8   googlecloudsdk.api_lib.appr   r   r   googlecloudsdk.callioper	   googlecloudsdk.corer
   r   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   Errorr   DeleteCommandr   r   r   r   <module>r[      sW      &  '  ; 3 3 ( * # 2 9 )*** 
c2T c2r   