
    l                        S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr   SSKJ!r!  SSK"J#r#  SSK"J$r$  SSK%J&r&  S r'S r(S r)S r*S r+S r,S r-S r.S r/S r0S r1S  r2S! r3S" r4S# r5S$ r6S% r7S& r8S' r9S( r:S) r;S* r<S+ r=S, r>S- r?S. r@S/ rAS0 rB\R                  \R                  " \R                  R                  \R                  R                  5       " S1 S2\R                  5      5       5       rIS3S4S5S6.\IlJ        g)7zLCommand for migrate from legacy firewall rules to network firewall policies.    )absolute_import)division)unicode_literalsN)
list_pager)base_classes)poller)tags)waiter)base)
exceptions)flags)convert_terraform)secure_tags_utils)endpoint_utils)
operations)log)
properties)filesc                     / nU HP  nSnUR                    H!  nUR                  U R                  :X  d  M  SnM#     U(       d  M?  UR                  U5        MR     U$ NFT)associationsattachmentTargetselfLinkappend)networkfirewall_policiesfiltered_policiesfirewall_policy
associatedassociations         -lib/surface/compute/firewall_rules/migrate.py)_GetFirewallPoliciesAssociatedWithNetworkr"   +   s]    *oJ&33		%	%)9)9	9
 4 z/ + 
    c                 v    / nU H0  nUR                   U R                  :X  d  M  UR                  U5        M2     U$ N)r   r   r   )r   	firewallsfiltered_firewallsfirewalls       r!   "_GetFirewallsAssociatedWithNetworkr)   7   s<    h7+++)  
r#   c                     [        5       nU  H9  nUR                  UR                  5        UR                  UR                  5        M;     U$ r%   )setupdate
sourceTags
targetTags)selected_firewallsr	   r(   s      r!   _GetLegacyTagsr0   ?   s>    	$$hKK##$KK##$ % 
+r#   c                     [        5       nU  HB  nUR                  UR                  5        U(       a  M'  UR                  UR                  5        MD     U$ r%   )r+   r,   sourceServiceAccountstargetServiceAccounts)r/   keep_target_service_accountsservice_accountsr(   s       r!   _GetServiceAccountsr6   G   sJ    U$hH::;''h<<= % 
r#   c                 :    SU R                   s=::  a  S::  a   g  gg)NiiTF)priority)rules    r!   _IsDefaultFirewallPolicyRuler:   P   s     4==.J. / 
r#   c                 (    SSR                  X5      4$ )NFz"Mapping for {} '{}' was not found.)format)fieldtags     r!   _UnsupportedTagResultr?   X   s    
5<<UH	IIr#   c                 V   U R                    H  nSU-   nXA;  d  M  [        SU5      s  $    U(       d+  U R                   H  nSU-   nXA;  d  M  [        SU5      s  $    U R                   H  nXQ;  d  M
  [        SU5      s  $    U R                   H  nXQ;  d  M
  [        SU5      s  $    g)zHChecks if the given VPC Firewall can be converted by the Migration Tool.sa:source_service_accounttarget_service_account
source_tag
target_tag)T )r2   r?   r3   r-   r.   )r(   tag_mappingr4   service_accountprefixed_service_accountr>   s         r!   _IsFirewallSupportedrJ   \   s     "77o$62"
"$<  8 
&#99!&!8	!	4$$&>
 	
 :   c
"<55 !   c
"<55 ! 
r#   c                 d    U H*  n[         R                  " X R                  5      (       d  M*    g   g)NTF)rematchname)r(   patternspatterns      r!   _IsExcludedFirewallrQ   x   s'    g	xx''  
r#   c                     XR                   R                  R                  :X  a   U R                  R                  R                  $ U R                  R                  R                  $ r%   )FirewallDirectionValueValuesEnumINGRESSFirewallPolicyRuleEGRESS)messages	directions     r!   _ConvertRuleDirectionrZ      sJ    ##<<DDD&&??GGG		$	$	=	=	D	DDr#   c                     / nU H6  nUR                  U R                  UR                  UR                  S95        M8     U$ )N)
ipProtocolports)r   %FirewallPolicyRuleMatcherLayer4Config
IPProtocolr]   )rX   
l4_configslayer4_configsconfigs       r!   _ConvertLayer4Configsrc      sI    .f66(( 	7 	
  
r#   c                 P    U Vs/ s H  nU R                  X   S9PM     sn$ s  snf )NrN   FirewallPolicyRuleSecureTag)rX   rG   r	   r>   s       r!   _ConvertTagsrh      s:     
# **0@*A
  
s   #c                 X    U Vs/ s H  nU R                  USU-      S9PM     sn$ s  snf )NrA   re   rf   )rX   rG   r5   rH   s       r!   _ConvertServiceAccountsrj      sJ    
 .	
 ./ **5?23 +  .	
  
s   'c                    UR                   n[        XUR                  5      nU(       d  / nU[        XUR                   5      -   nU R	                  UR
                  UR                  UR                  [        XR                  5      UR                  UUR                  R                  U R                  UR                  UR                  [        XUR                   5      [        XUR"                  5      -   [%        X5      S9UUS9
$ )z-Converts VPC Firewall to FirewallPolicy.Rule.)destIpRangessrcIpRangessrcSecureTagslayer4Configs)
disabledruleNamedescriptionrY   r8   actionenableLoggingrM   r3   targetSecureTags)r3   rh   r.   rj   rV   rp   rN   rr   rZ   rY   r8   	logConfigenableFirewallPolicyRuleMatcherdestinationRangessourceRangesr-   r2   rc   )rX   r(   rs   r`   rG   r4   target_service_accountstarget_secure_tagss           r!   _ConvertRuleInternalr}      s    %::#H8;N;NO	% +.Ex==/  
	$	$  }}&&%h0B0BC  &&--..11++8(2E2EF')G)G
 .hC / 
 4)) 
% 
 r#   c                     UR                   (       a  [        U USUR                   UU5      $ [        U USUR                  UU5      $ )Ndenyallow)deniedr}   allowed)rX   r(   rG   r4   s       r!   _ConvertRuler      sS    __$  
"
 r#   c                 Z    SnU  H#  nU(       a	  U(       a    gU=(       d    U(       + nM%     gr    )statusesfalse_detectedstatuss      r!   _IsPrefixTruer      s*    .f.#16zN  
r#   c                 <    U nUR                  5         [        U5      $ r%   )reverser   )r   statuses_copys     r!   _IsSuffixTruer      s    -	}	%%r#   c                     [         R                  " U 5       n[        R                  " U5      nSSS5        WR                  5        VVs0 s H  u  pEU[        R                  " U5      _M     nnnU$ ! , (       d  f       NI= f! [         a/    [
        R                  R                  SR                  U S95         g[         a/    [
        R                  R                  SR                  U S95         g[         a^  n[
        R                  R                  SR                  U S95        [
        R                  R                  [        U5      5         SnAgSnAff = fs  snnf )z6Imports legacy to secure tag mapping from a JSON file.Nz:File '{file}' was not found. Tag mapping was not imported.filezOOS error occurred when opening the file '{file}'. Tag mapping was not imported.z\Unexpected error occurred when reading the JSON file '{file}'. Tag mapping was not imported.)r   
FileReaderjsonloadFileNotFoundErrorr   r   Printr<   OSError	Exceptionrepritemsr   TranslateSecureTag)	file_namefdataekvrG   s          r!   _ReadTagMappingr      s)   			)	$YYq\d 
%0 >BZZ\=ITQa		-	-a	00\   
7 
%	$	 JJDKK 	L 	

 	 JJ	VV+ 	 JJ	%%+VV%; JJT!WsF   B A2B 
#E2
B <B  B 6E;5E2	E;AEEc                     U R                   R                  S5      nSU R                   US  -   nUR                  SU R                  -  SU R                  -  5      $ )Nz
/projects/z//compute.googleapis.comzinstances/%s)r   findreplacerN   id)instancestart_indexresource_names      r!   _GetFullCanonicalResourceNamer     sZ    !!&&|4+,x/@/@/NN-			x}}$x{{"
 r#   c                 j    U R                   U R                   R                  S5      [        S5      -   S  $ )Nz/zones/)zoner   len)r   s    r!   _GetInstanceLocationr     s,    	x}})))4s9~EG	HHr#   c                 Z  ^ U4S jnUR                   nUR                  R                  UR                  U SSS95      nUR                  R
                   Vs/ s H  nUR                  R                  PM     nn[        [        R                  " U6 5      n[        [        X85      5      $ s  snf )zGets instances in the network.c           	         > [        U R                   Vs/ s H4  nUR                  R                  ST-  5      (       d  M(  UR                  PM6     sn5      $ s  snf )Nz/%s)r   networkInterfacesr   endswith)r   network_interfacenetwork_names     r!   _HasInterfaceMatchingNetwork<_GetInstancesInNetwork.<locals>._HasInterfaceMatchingNetwork  sY    !)!;!;!;$$--el.BC 	"!!!;   s   'A AT  )projectincludeAllScopes
maxResults)MESSAGES_MODULE	instancesAggregatedList%ComputeInstancesAggregatedListRequestr   additionalPropertiesvaluelist	itertoolschainfilter)	r   r   compute_clientr   rX   instance_aggregationsiteminstances_listr   s	    `       r!   _GetInstancesInNetworkr     s     ++((22AA44 5  (--BBB$ jjB   9??N34)	f1=	>>s    B(c           	      H   [         R                  " 5       n[        U5      nUR                  X0S9nUR	                  US9n[        U5      n[        R                  " U5          [         R                  " 5       R                  U5      nUR                  (       d1  [        R                  " USR                  X0UR                  5      5        SSS5        g! [         a5  n[         R"                  R%                  S['        U5      -   5         SnANCSnAff = f! , (       d  f       g= f)zBinds tag to the instance.)parenttagValue)
tagBindingzQWaiting for TagBinding for parent [{}] and tag value [{}] to be created with [{}]z"Tag binding could not be created: N)rm_tagsTagMessagesr   
TagBinding,CloudresourcemanagerTagBindingsCreateRequestr   	endpointsCrmEndpointOverridesTagBindingsServiceCreatedoner   WaitForReturnOperationr<   rN   r   r   r   r   r   )		tag_valuer   rX   r   tag_bindingbinding_reqlocationopr   s	            r!   _BindTagToInstancer   6  s      "(/9-##=#M+EE F + "(+(%%h/	G%%'..{;bWW))  &} I	
	 0/  G	jj;d1gEFFG 0/s1    D"A&C
D+DDDD
D!c                     [        U5      nU(       d  g[        XU5      nU H  n[        XF5        [        XF5        M     g)z:Binds secure tags to instances with matching network tags.N)r   r   _BindTagsToInstance_BindServiceTagsToInstance)r   r   tag_mapping_file_namer   rG   vm_instancesvms          r!   _BindSecureTagsToInstancesr   O  s;       56+	
'~N,b({/ r#   c                 h    UR                   R                   H  nX ;   d  M
  [        X   U5        M     g r%   )r	   r   r   )rG   r   r>   s      r!   r   r   ^  s'    WW]]c
)2. r#   c                     UR                    Vs/ s H  o"R                  PM     nnU H  nSU-   nX@;   d  M  [        X   U5        M     g s  snf NrA   )serviceAccountsemailr   )rG   r   sar5   prefixed_tags        r!   r   r   d  sL    )+););<);2hh);<b2:L"2B7  =s   A
c                 H   [        [        S U5      5      n[        R                  UR	                  U5      5      n [
        R                  " U SS9 n[        R                  " XE5        SSS5        g! , (       d  f       g= f! [         a/    [        R                  R                  SR                  U S95         g[         a^  n[        R                  R                  SR                  U S95        [        R                  R                  [        U5      5         SnAgSnAff = f)z4Exports legacy to secure tag mapping to a JSON file.c                     SU -   $ r   r   )xs    r!   <lambda>"_WriteTagMapping.<locals>.<lambda>p  s    r#   Tpathcreate_pathNzOOS error occurred when opening the file '{file}'. Tag mapping was not exported.r   z\Unexpected error occurred when writing the JSON file '{file}'. Tag mapping was not exported.)r+   mapdictfromkeysunionr   
FileWriterr   dumpr   r   r   r   r<   r   r   )r   r	   r5   prefixed_service_accountsmappingr   r   s          r!   _WriteTagMappingr   m  s     "#&;=M"NOMM$**%>?@'			yd	;q
ii 
<	;	;	 JJ	VV+ 	 JJ	%%+VV%; JJT!Ws<   B A1(B 1
A?;B ?B 6D!:	D!ADD!c                     [         R                  " U SS9 nUR                  U5        SSS5        g! , (       d  f       g= f! [         a/    [        R
                  R                  SR                  U S95         g[         a^  n[        R
                  R                  SR                  U S95        [        R
                  R                  [        U5      5         SnAgSnAff = f)zExports Terraform script.Tr   NzTOS error occurred when opening the file '{file}'. Terraform script was not exported.r   z_Unexpected error occurred when writing to the file '{file}'. Terraform script was not exported.
r   r   writer   r   r   r   r<   r   r   )r   	tf_scriptr   r   s       r!   _WriteTerraformScriptr     s    			yd	;qggi 
<	;	;	 JJ	Y/ 	 JJ	$$*F	F$: JJT!Ws7   A 2A 
A A  A 6C";	C"ACC"c                     [         R                  " U SS9 nU H  nUR                  US-   5        M     SSS5        g! , (       d  f       g= f! [         a/    [        R
                  R                  SR                  U S95         g[         a^  n[        R
                  R                  SR                  U S95        [        R
                  R                  [        U5      5         SnAgSnAff = f)z-Exports regexes used for excluding firewalls.Tr   
NzWOS error occurred when opening the file '{file}'. Exclusion patterns were not exported.r   zbUnexpected error occurred when writing to the file '{file}'. Exclusion patterns were not exported.r   )r   rO   r   rP   r   s        r!   _WriteExclusionPatternsr    s    			yd	;q'	$  
<	;	; 
 JJ	$f)f4 	 JJ	''-v9v'= JJT!Ws8   A >A 
AA A 6C.	C.AC))C.c                     [         R                  " U 5       nUR                  5       nU Vs/ s H  o3R                  S5      PM     nnSSS5        SnW H  n [        R                  " U5        M     U(       d  / S4$ US4$ s  snf ! , (       d  f       ND= f! [         a3    [
        R                  R                  SR                  U S95        / S4s $ [         a3    [
        R                  R                  SR                  U S95        / S4s $ [         ab  n[
        R                  R                  SR                  U S95        [
        R                  R                  [        U5      5        / S4s SnA$ SnAff = f! [         ad  nSn[
        R                  R                  S	R                  U5      5        [
        R                  R                  [        U5      5         SnAGM  SnAff = f)
z'Imports exclusion patterns from a file.r  NzBFile '{file}' was not found. Exclusion patterns were not imported.r   TzWOS error occurred when opening the file '{file}'. Exclusion patterns were not imported.z_Unexpected error occurred when reading the file '{file}'. Exclusion patterns were not imported.Fz&Cannot compile regular expression '{}')r   r   	readlinesrstripr   r   r   r   r<   r   r   r   rL   compile)r   r   lineslinerO   r   successrP   s           r!   _ReadExclusionPatternsr    s   			)	$kkme056++d#h6 
%. 'g jj  
t8O	5C 7 
%	$ 
 JJL	Y	 t8O	 JJ	$f)f4 t8O	 JJ	''-v9v'= JJT!Wt8O   g	jj?FFwOP	jjtAw sq   B BBB	B E=B
BB B :E:9E:	E:AE5/E:5E:=
G+AG&&G+c                   :    \ rS rSrSrSr/ SQr\S 5       rS r	Sr
g)Migratei  z@Migrate from legacy firewall rules to network firewall policies.N)zgke-(.+)-ipv6-allzCgke-(.+)-(.+)-((master)|(vms)|(all)|(inkubelet)|(exkubelet)|(mcsd))zk8s-fw-(l7-)?(.+)z'k8s-(.+)-((node)|(http)|(node-http))-hcz(.+)-hczk8s2-(.+)-(.+)-(.+)-(.+)(-fw)?zk8s2-(.+)-l4-shared-hc-fwzgke((gw)|(mcg))1-l7-(.+)-(.+)c                    UR                  SSS9nUR                  SSS9  UR                  SSSS	9  UR                  S
SSS	9  UR                  SSSS	9  [        R                  " SS[        R
                  SSSSS S9U l        U R                  R                  U5        UR                  SSSS9  UR                  SSSSS9  UR                  SSSS9  UR                  SSSS9  UR                  SSSSS9  UR                  S SSS!S9  g )"NT)mutexrequiredz--target-firewall-policyz^      Name of the new Network Firewall Policy used to store the migration
      result.
      )helpz--export-tag-mapping
store_truez      If set, migration tool will inspect all VPC Firewalls attached to
      SOURCE_NETWORK, collect all source and target tags, and store them in
      TAG_MAPPING_FILE.
      )rs   r  z--export-exclusion-patternszk      If set, migration tool will dump list of regexes used to filter VPC Firewall out of migration.
      z--bind-tags-to-instancesz      If set, migration tool will bind secure tags to the instances with the
      network tags which match secure tags from the tag mapping file.
      z--source-networkr   Fzcompute.networksz<The VPC Network for which the migration should be performed.)rN   r   	completerpluralr  global_collection
short_helpdetailed_helpz--tag-mapping-filezQPath to a JSON file with legacy tags and service accounts to secure tags mapping.)r  r  z--export-terraform-scriptzfIf set, migration tool will output a terraform script to create a Firewall Policy with migrated rules.)rs   r  r  z--terraform-script-output-filez9Path to a file where to store generated Terraform script.z--exclusion-patterns-filezPath to a file with exclusion patterns used for VPC Firewall filtering. Each regular expression describing a single firewall naming pattern must be placed in a single line. No leading or tailing whitespaces.z--forcezpIf set, migration will succeed even if the tool detects that original rule evaluation order cannot be preserved.z.--skip-migrate-target-service-accounts-to-tagszrIf set, migration will keep target service accounts as they are and will not try to replace them with secure tags.)	add_groupadd_argumentcompute_flagsResourceArgumentnetwork_flagsNetworksCompleterNETWORK_ARGAddArgument)clsparsergroups      r!   ArgsMigrate.Args  s    4$7E	"
   

   
%
   
"
   $4411,JCO OO'
$	   #4   (H   #$	  	 C   8B  r#   c                 j   [         R                  " U R                  5       5      nUR                  R                  nUR
                  nUR                  (       a  UR                  nO2[        R                  R                  R                  R                  5       n[        US5      n[        USS5      n[        USS5      n[        USS5      n	[        USS5      n
[        USS5      n[        US	S5      n[        US
S5      n[        USS5      n[        USS5      n[        USS5      nU(       a  U	(       d  [        R                  " S5      eU
(       a  U(       d  [        R                  " S5      eU(       a  U	(       d  [        R                  " S5      eU(       a  [        XeX5        gU
(       aD  [        XR                   5        ["        R$                  R'                  SR)                  U5      5        gU(       a<  [+        U5      u  nnUU l        U(       a   ["        R$                  R'                  S5        gUR,                  R/                  UR1                  XVS95      n["        R$                  R'                  SR)                  U5      5        UR2                  R5                  UR7                  US95      nUR8                   H:  nUR:                  U:X  d  M  ["        R$                  R'                  SU-   S-   5          g   [=        UUR8                  5      n["        R$                  R'                  SR)                  [?        U5      U5      5        [?        U5      S:  a   ["        R$                  R'                  S5        g[@        RB                  " URD                  SURG                  US9SSS9n[I        UU5      n["        R$                  R'                  SR)                  [?        U5      U5      5        ["        R$                  R'                  S R)                  [?        U R                   5      5      5        U R                    H"  n["        R$                  R'                  U5        M$     ["        R$                  R'                  S!5        S"n/ nU H7  n[K        UU R                   5      (       + nURM                  UUU45        US-   nM9     U(       a|  / nU H!  u  nnnU(       d  M  URM                  U5        M#     [O        U5      n [Q        UU5      n![S        U	U U!5        ["        R$                  R'                  S#R)                  U	5      5        g[U        5       n"U	(       a.  [W        U	5      n"U"c   ["        R$                  R'                  S$5        gU V#VV$s/ s H+  u  n#nn$U#RX                  U#RZ                  (       a  S"OSU$U#U4PM-     n%nn#n$[]        U%5      n%/ n&S"n'S"n(U% H^  u  n)  nnnS%u  n*n+Sn,U(       a0  U(S-   n([_        UU"U5      u  n*n+U*(       a  [a        UUU"U5      n,OU'S-   n'U&RM                  U)UUU,U*U+45        M`     U((       a  ["        R$                  R'                  S&R)                  U(5      5        ["        R$                  R'                  S'5        U& Ha  u  nnn    nU(       d  M  ["        R$                  R'                  S(R)                  URX                  UR:                  URb                  5      5        Mc     ["        R$                  R'                  S)5        [?        U&5      U(-
  n-U-(       a  ["        R$                  R'                  S*R)                  U-5      5        ["        R$                  R'                  S'5        U& Ha  u  nnn    nU(       a  M  ["        R$                  R'                  S(R)                  URX                  UR:                  URb                  5      5        Mc     ["        R$                  R'                  S)5        U'(       a  ["        R$                  R'                  S+R)                  U'5      5        U& HW  u  nn  nn*n+U*(       a  M  ["        R$                  R'                  S,R)                  URX                  UR:                  U+5      5        MY     ["        R$                  R'                  S)5        / n.U H9  nURd                   H&  n/[g        U/5      (       a  M  U.RM                  U/5        M(     M;     U. V/s/ s H  n/U/RX                  U/4PM     n.n/[]        U.5      n.U. V)V/s/ s H  u  n)n/U)SS-U/S-S)4PM     n.n)n// n0URh                  URj                  Rl                  Rn                  :X  a#  U0Rq                  U&5        U0Rq                  U.5        O"U0Rq                  U.5        U0Rq                  U&5        U0 VV*s/ s H  u        nn*nU*PM     n1nn*Sn2S.n3URh                  URj                  Rl                  Rn                  :X  a2  [s        U15      (       d!  ["        R$                  R'                  U35        S-n2O1[u        U15      (       d!  ["        R$                  R'                  U35        S-n2U2(       a(  U(       a   ["        R$                  R'                  S/5        OgU0 V4V#VV5V6s/ s H  u  n4n#nn5n6nU6(       d  M  U4U5U#4PM     n7n5nn#n4n6[?        [w        U7 V4V5V#s/ s H	  u  n4n5n#U4PM     sn#n5n45      5      [?        U75      :g  n8U8(       a>  ["        R$                  R'                  S05        ["        R$                  R'                  S15        S2n9/ n:U7 H|  u  n)n/nU8(       a[  U9U/l,        U9S-   n9["        R$                  R'                  S3R)                  U/RX                  U)U/Rx                  U/Rb                  5      5        U:RM                  U/U45        M~     U8(       a  ["        R$                  R'                  S)5        U R                  5       [z        R                  R|                  :X  a0  UR                  S4UUR~                  R                  R                  S59nOUR                  S4US69nU(       a  [        R                  " UU5      S!-   n;U: H"  u  n/nU;[        R                  " U/5      -   S!-   n;M$     U(       a;  [        UU;5        ["        R$                  R'                  S7R)                  U5      5        g["        R$                  R'                  S85        ["        R$                  R'                  U;5        gUR2                  R                  UR                  UUS995      n<[        R                  " UR2                  5      n=UR                  R                  U<R                  S:S;9n>[        R                  " U=U>S<R)                  U5      5        ["        R$                  R'                  S=5        ["        R$                  R'                  S>5        / n?U: H  u  n/nU?RM                  UR2                  R                  UR                  UU/US?95      5        U(       d  MH  ["        R$                  R'                  S(R)                  URX                  UR:                  URb                  5      5        M     [        R                  " UR                  UR2                  5      n=U? V<s/ s H'  n<UR                  R                  U<R                  S:S;9PM)     n@n<[        R                  " U=[        R                  " U@5      S@5        gs  sn$nn#f s  sn/f s  sn/n)f s  sn*nf s  sn6n5nn#n4f s  sn#n5n4f s  sn<f )AzRun the migration logic.source_networktarget_firewall_policyNexport_tag_mappingFtag_mapping_fileexport_exclusion_patternsexclusion_patterns_filebind_tags_to_instancesexport_terraform_scriptterraform_script_output_fileforce,skip_migrate_target_service_accounts_to_tagszD--tag-mapping-file must be specified if --export-tag-mapping is set.zR--exclusion-patterns-file must be specified if --export-exclusion-patterns is set.zH--tag-mapping-file must be specified if --bind-tags-to-instances is set.z(Exclusion patterns were exported to '{}'zCCould not import exclusion patterns. Migration cannot be completed.)r   r   zYLooking for VPC Firewalls and Network Firewall Policies associated with VPC Network '{}'.)r   zFirewall Policy "z" already exists.zHFound {} Network Firewall Policies associated with the VPC Network '{}'.   zhMigration tool does not support multiple Network Firewall Policies associated with a single VPC Network.r   Listr   )service
batch_sizerequestmethodr=   z=Found {} VPC Firewalls associated with the VPC Network '{}'.
z/{} pattern(s) used to filter VPC Firewalls out:r  r   z!Legacy tags were exported to '{}'z,Stop processing, missing tag mapping file...)Fz$Not a customer defined VPC Firewall.z Found {} selected VPC Firewalls.zpriority: name 'description'z{}: {} '{}'rF   z#{} VPC Firewalls were not selected.z,Could not convert {} selected VPC Firewalls:z{}: {} - {}TzPSafe migration is impossible, because rule evaluation order cannot be preserved.z5WARNING: Forcing migration of chosen firewall rules.
z.Updating rules priorities to avoid collisions.z7new-priority: old-priority rule-name 'rule-description'i  z{}: {} {} '{}'zyNetwork Firewall Policy containing all VPC Firewalls and FirewallPolicy.Rules migrated using GCP Firewall Migration Tool.)rr   rN   vpcNetworkScope)rr   rN   z*Terraform script exported to the file '{}'z6Terraform script for migrated Network Firewall Policy:)r   firewallPolicyzcompute.globalOperations)
collectionz)Creating new Network Firewall Policy '{}'z&Migrating the following VPC Firewalls:z*old-priority: rule-name 'rule-description')r8  firewallPolicyRuler   	Migrating)Sr   ComputeApiHolderReleaseTrackclientapitools_clientr   r   r   VALUEScore	GetOrFailgetattrr   ToolExceptionr   r  exclusion_patternsr   r   r   r<   r  networksGetComputeNetworksGetRequestnetworkFirewallPoliciesr2  )ComputeNetworkFirewallPoliciesListRequestr   rN   r"   r   r   YieldFromListr&   ComputeFirewallsListRequestr)   rQ   r   r0   r6   r   r   r   r8   r   sortedrJ   r   rr   rulesr:   %networkFirewallPolicyEnforcementOrderNetwork4NetworkFirewallPolicyEnforcementOrderValueValuesEnumAFTER_CLASSIC_FIREWALLextendr   r   r+   rq   r   ALPHAFirewallPolicyVpcNetworkScopeValueValuesEnumGLOBAL_VPC_NETWORKr   ConvertFirewallPolicyConvertFirewallPolicyRuler   Insert+ComputeNetworkFirewallPoliciesInsertRequestr   Poller	resourcesParser   r
   WaitForAddRule,ComputeNetworkFirewallPoliciesAddRuleRequestBatchPollerOperationBatch)Aselfargsholderr>  rX   r   r   policy_namer(  r   r*  exclusion_patterns_file_namer,  r-  !terraform_script_output_file_namer/  r4   rO   errr   fp_list_responser   r   fetched_firewallsassociated_firewallsrP   firewall_idmarked_firewallsr(   selectedr/   _legacy_tagsr5   rG   r   r   sorted_firewallsconverted_firewallsconversion_failures_countselected_firewalls_countr8   r   errorconverted_firewallnon_selected_firewall_countfirewall_policy_rulesr9   joined_rulesr   safe_migration_impossiblesafe_migration_error_messageprsrules_to_migratepriorities_remap_neededcurrent_prioritymigrated_rulesr   responseoperation_polleroperation_ref	responsesoperation_refssA                                                                    r!   RunMigrate.Rune  s!   **4+<+<+>?F]]**F%%H ||g!!&&..88:g 4!12L$ 8$?K ';UC#D*<dC ')5! $+'$  %T+CUK%d,EuM(/,d)% D'5)E#*<e$ 
 "7$$
P 
 !)E$$1 
 &;$$   
!6  !
&(?(? 
jj
4
;
;*
  $,-IJmh (d	

	
 	 oo!!** 	+ 	
G JJ	""(&"6 55::::7:K
 ,11				,

+-0CC	
 	 2 B!'' JJ	-.= !	jj2  #00  44W4E >" JJHOO$%|	
 JJ9@@''(	

 **	jjw +JJTK((43J3JKKhx;?@!Ok ) #3
(Ha8

#
#H
- $4 ##56k,
: ,k;KL	jj
-
4
45J
K 
 &K#$9:k		

GH "2!1Q" 
!((Q2q(;!1   ./  ! .>*!Q(Govu	#;a#? .k#?
 +*	 
 '@!&C
#  Xx);VU
K% /?.  	jj
,
3
34L
M 
jj56,?
(!XxAq8
**

""##X]]H4H4H -@ 
jjr 	 #;;   #	jj
/
6
6)
 
jj56,?
(!XxAqx
**

""##X]]H4H4H -@ 
jjr !	jj
8
?
?'
 /B
*!Xq!VUv
**

""8#4#4hmmUK /B
 
jjr ,!''$+D11

&
&t
, ( - +@*?$*?   ##89 !6 !6Xt 
4tT2. 5   L55PPgg	h -./0/0-. 7CCl2Aq!QlHC %	 !
 	55PPgg	h 8$$

56$(!8$$

56$(! !	

D	
 	 =INL&8q!Q1aA	Aq	LN " 01 09Aq!Q 012		
 	jjGH	jj
C N$4 $	 (+a/

##x8H8H	

 T8,- %5 	jjr d//555 // "11PPcc 0 o !//  0 o 

1
1/7
K  $'$)CCDIIDP 	 $ 
+?K

8??1	
 	 	

D	
 	

#--44<<O 	= 	
H }}V%C%CD$$**&@ + M NN3::;G JJ=>JJABI(h

(
(
0
0CC!,%)! D  


  !!8==(2F2F	
 )" ))v55 "	 "H 	*D 	 	
 "	   NN&//?Ur0 D8 O 2Zs0   2|)||%|| 	| <|)+.|0)rE  )__name__
__module____qualname____firstlineno____doc__r  rE  classmethodr#  r  __static_attributes__r   r#   r!   r  r    s2     I+> g gRLr#   r  zXCreate a new Network Firewall Policy and move all customer defined firewall rules there.z
*{command}* is used to create a new Network Firewall Policy that contain
all rules defined in already existing Network Firewall Policy associated with
the given VPC Network and all customer defined VPC Firewall Rules attached to
that VPC Network.
z
To execute the migration for VPC Network 'my-network' which stores the result
in 'my-policy' Network Firewall Policy, run:

  $ {command} --source-network=my-network --target-firewall-policy=my-policy
  )briefDESCRIPTIONEXAMPLES)Kr  
__future__r   r   r   r   r   rL   apitools.base.pyr   googlecloudsdk.api_lib.computer   )googlecloudsdk.api_lib.compute.operationsr   'googlecloudsdk.api_lib.resource_managerr	   r   googlecloudsdk.api_lib.utilr
   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.computer   r  <googlecloudsdk.command_lib.compute.network_firewall_policiesr   r   +googlecloudsdk.command_lib.compute.networksr  +googlecloudsdk.command_lib.resource_managerr   r   r   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r"   r)   r0   r6   r:   r?   rJ   rQ   rZ   rc   rh   rj   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  UniverseCompatibleReleaseTracksr=  rT  BETACreateCommandr  r  r   r#   r!   <module>r     sR   S &  '   	 ' 7 < C . ( . E Z Z N S B # * *	J8E%P(&BI?:G20/8.(*&R D%%++T->->-C-CDY	d   Y	 E Y	|	  r#   