
    <                     `   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ZddlZ G d dej$                        Z e
j(                  e
j*                  j,                  e
j*                  j.                         G d de
j0                               Zy)zThe Migrate command.    )absolute_import)division)unicode_literals)appengine_api_client)operations_util)service_util)base)
exceptions)log)
console_ioNc                       e Zd ZdZy)VersionsMigrateErrorzErrors when migrating versions.N)__name__
__module____qualname____doc__     #lib/surface/app/versions/migrate.pyr   r   !   s    'r   r   c                   .    e Zd ZdZddiZed        Zd Zy)MigratezBMigrate traffic from one version to another for a set of services.EXAMPLESaw            This only works for automatically scaled Standard versions.
          To migrate from one version to another for all services where there
          is a version v2 and shut down the previous version, run:

            $ {command} v2

          To migrate from one version to another for a specific service, run:

            $ {command} v2 --service="s1"
          c                 R    | j                  dd       | j                  ddd       y )NversionzThe version to migrate to.)helpz	--servicez-sz;If specified, only migrate versions from the given service.)add_argument)parsers    r   ArgszMigrate.Args7   s2    
	(DE
T.  /r   c           	          t        j                  | j                               }j                  r|j	                  j                        }i }|j
                  r>|j
                  j                  j                  D ]  }|j                  ||j                  <    t        j                  |j                  |j                  |      g}n|j                         }|j                  |      }j                   |D ch c]  }|j                   c}vr_j                  r/t#        dj%                  j                  j                               t#        dj%                  j                               |D ch c](  }|j                  j                   k(  s|j                  * c}fd}	t'        t(        j*                  j-                  |	|            }
|
D ]}  }dj%                  |j                  |j                        }dj%                  |j                  j                         }t.        j0                  j3                  dj%                  ||              t5        j6                  d       i }t9        t;        |
D cg c]  }|j                   c}            D ]5  }j                   di}	 t=        j>                  |j@                  ||d	d
       7 |ridj%                  djG                  t'        |jI                                     djG                  t'        |jK                                           }t#        |      y c c}w c c}w c c}w # t<        jB                  $ r"}t)        jD                  |      ||<   Y d }~d }~ww xY w)Nz!Version [{0}/{1}] does not exist.zVersion [{0}] does not exist.c                     | j                   v xr8 | j                  xr* | j                  dkD  xr | j                  j                  k7  S )Nr   )servicetraffic_splitidr   )vargsservice_namess    r   WillBeMigratedz#Migrate.Run.<locals>.WillBeMigratedT   sE    ii=( ;Q__ ;oo!;&'dddll&:<r   z{0}/{1}z1Migrating all traffic from version [{0}] to [{1}]T)cancel_on_nog      ?ip)shard_bymigratez6Issues migrating all traffic of service(s): [{0}]

{1}z, z

)&r   GetApiClientForTrackReleaseTrackr!   GetServiceResourcesplitallocationsadditionalPropertiesvaluekeyr   Serviceprojectr#   ListServicesListVersionsr   r   formatlistsixmovesfilterr   statusPrintr   PromptContinuesortedsetr   CallAndCollectOpErrorsSetTrafficSplitMiscOperationError	text_typejoinkeysvalues)selfr%   clientr!   r"   r/   servicesall_versionsr$   r'   versions_to_migrater   
short_namepromoted_nameerrorsr0   errerror_stringr&   s    `                @r   RunzMigrate.Run>   s   !66t7H7H7JKF||))$,,7gm	]]..CCE%*[[-		
" D&&v~~wzz'46 7h $$&h&&x0L||,7,QADD,77	"#F#M#MLL$,,$( ) 	) ##B#I#ILL$  	(4M18LQYYMM<
 syy//MN&##GOOWZZ@j&&wEm	jj ((.%})67 ' 40F#2EF2EQqyy2EFGH\\3'k-..""G[4	 I 117))D$78 ++d6==?&;<2>  !.. G 8 N& G 00 ---,w-s0   6L90L>L>7M'$MM=M88M=N)r   r   r   r   detailed_helpstaticmethodr   rS   r   r   r   r   r   %   s/    J  
- / /5/r   r   )r   
__future__r   r   r   googlecloudsdk.api_lib.appr   r   r   googlecloudsdk.callioper	   googlecloudsdk.corer
   r   googlecloudsdk.core.consoler   r:   	six.movesErrorr   ReleaseTracksr-   BETAGACommandr   r   r   r   <module>ra      s      &  ' ; 6 3 ( * # 2 
 (:++ ( D%%**D,=,=,@,@AM/dll M/ BM/r   