
    ,                        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  ej*                  ej,                  j.                         G d dej0                               Z ej*                  ej,                  j4                         G d de             Z ej*                  ej,                  j8                         G d de             Zdddde_        y)z$Command for updating firewall rules.    )absolute_import)division)unicode_literals)encoding)base_classes)firewalls_utils)utils)apis)base)
exceptions)flagsc                   P    e Zd ZdZdZdZdZed        Zd Z	d Z
d Zd Zd	 Zd
 Zy)UpdateFirewallUpdate a firewall rule.TNc                    t        j                  dt        j                        }t	        j
                         | _        | j                  j                  |d       t        j                  |d| j                  | j                         t        j                  |d       t	        j                  |       t	        j                  ||       y Ncomputeupdate)operation_typeT)
for_updatewith_egress_supportwith_service_account)r   )r
   GetMessagesModulecompute_apiCOMPUTE_GA_API_VERSIONr   FirewallRuleArgumentFIREWALL_RULE_ARGAddArgumentr   AddCommonArgswith_egress_firewallr   AddArgsForServiceAccountAddEnableLoggingAddLoggingMetadataclsparsermessagess      ,lib/surface/compute/firewall_rules/update.pyArgszUpdateFirewall.Args*   s    %%i&1&H&HJH!668C%%fX%F!!44 55	7
 ,,VE	6"	VX.    c                    t        j                  |j                  |t         j                  j                        | _        t        d |j                  |j                  |j                  |j                  |j                  fD              }| j                  r7|xr3 t        d |j                  |j                  |j                  fD              }| j                  r,|xr( t        d |j                   |j"                  fD              }|xr |j$                  d u }|xr |j&                  d u }|xr |j(                   }|rt+        j,                  d      |j                  r"|j                  rt        j.                  d      y y )Nc              3   $   K   | ]  }|d u  
 y wN .0xs     r(   	<genexpr>z2UpdateFirewall.ValidateArgument.<locals>.<genexpr>=   s       76A 	
T	6   c              3   $   K   | ]  }|d u  
 y wr-   r.   r/   s     r(   r2   z2UpdateFirewall.ValidateArgument.<locals>.<genexpr>B   s      &IGa t)Gr3   c              3   $   K   | ]  }|d u  
 y wr-   r.   r/   s     r(   r2   z2UpdateFirewall.ValidateArgument.<locals>.<genexpr>F   s      &QOa t)Or3   z'At least one property must be modified.z8Can NOT specify --rules and --allow in the same request.)r   
ParseRulesallow
ActionTypeALLOWnew_allowedalldescriptionsource_rangessource_tagstarget_tagsr    destination_rangespriorityrulesr   source_service_accountstarget_service_accountsdisabledenable_logginglogging_metadatar   UpdatePropertyErrorArgumentValidationError)selfr'   args
args_unsets       r(   ValidateArgumentzUpdateFirewall.ValidateArgument9   st   &11

Ho88>>@D  7**d..0B0B""D$4$467 7J    I# &I++T]]DJJG&I #Ij    Q# &Q00$2N2NO&Q #Qj 5 5J<!4!4!<J9D$9$9 9J**
35 5 zzdjj33
DF F !zr*   c                 (   t        j                  | j                               }|j                  }| j	                  |j
                  |       | j                  j                  ||j                        }| j                  ||      }g }|j                  |g      }| j                  |||d   |      }|r|d   |k(  r|d   S |j                  j                  |      5  |j                  | j                  |||      g      }	ddd       |	S # 1 sw Y   	S xY w)z7Issues requests necessary to update the Firewall rules.r   N)r   ComputeApiHolderReleaseTrackclientrM   r'   r   ResolveAsResource	resources_GetGetRequestMakeRequestsModifyapitools_clientIncludeFields_GetSetRequest)
rJ   rK   holderrQ   resource_referenceget_requestcleared_fieldsobjects
new_objectresource_lists
             r(   RunzUpdateFirewall.RunT   s   **4+<+<+>?F]]F&//40 //AAf %%f.@AKN!!;-0GVT71:~FJ
 z1QZ				-	-n	=))v'9:F
GIm 
> 	 
> s   $DDc                     |j                   j                  d|j                  j                  |j	                         |j
                        fS )z7Returns the request for the existing Firewall resource.Get)firewallproject)rW   	firewallsr'   ComputeFirewallsGetRequestNamere   )rJ   rQ   r[   s      r(   rT   zUpdateFirewall._GetGetRequestp   sI    "",,eOO66+002*22 7 45 5r*   c                     |j                   j                  |j                  ||j                        }|j                  j
                  d|fS )N)rd   firewallResourcere   Patch)r'   ComputeFirewallsPatchRequestnamere   rW   rf   )rJ   rQ   r[   replacementrequests        r(   rY   zUpdateFirewall._GetSetRequestw   sK    oo::!!$"** ; ,G "",,gw??r*   c                     |j                  d      rX||j                  st        j                  dd      t	        j
                  |      j                  |j                        |_        y y )NrG   z--logging-metadataz9cannot toggle logging metadata if logging is not enabled.)	IsSpecifiedenablecalliope_exceptionsInvalidArgumentExceptionr   GetLoggingMetadataArgGetEnumForChoicerG   metadata)rJ   r'   rK   
log_configs       r(   _AddLoggingMetadataz"UpdateFirewall._AddLoggingMetadata~   sk    *+		:#4#4!:: GI 	I "77
$$T%:%:;  ,r*   c                 l   |j                   r| j                  }n,|j                   |j                  }n|j                  d       g }|j                  r|j                  }n,|j                  |j                  }n|j                  d       d}|j
                  r|j
                  }n,|j
                  |j                  }n|j                  d       g }|j                  r|j                  }n,|j                  |j                  }n|j                  d       g }|j                  r|j                  }	n,|j                  |j                  }	n|j                  d       g }	g }
|j                  r|j                  rDt        j                  |j                  |j                  t        j                  j                         }nt        j                  |j                  |j                  t        j                  j"                        }
n?|j                  3|j                  r|j                  d       g }n|j                  d       g }
|j$                  }|j&                  |j&                  }n|j&                  }g }|j(                  r|j(                  }n*|j(                  |j*                  }n|j                  d       g }|j,                  r|j,                  }n*|j,                  |j.                  }n|j                  d	       g }|j0                  r|j0                  }n*|j0                  |j2                  }n|j                  d
       |j5                  d      rD|j                  j7                  |j8                        }| j;                  |j                  ||       nJ|j<                  t?        j@                  |j<                        nd}| j;                  |j                  ||       |j                  jC                  |jD                  ||||
||jF                  ||||	|||      }|jH                  |jH                  |_$        |S )z8Returns a modified Firewall message and included fields.Nallowedr<   sourceRanges
sourceTags
targetTagsdenieddestinationRangessourceServiceAccountstargetServiceAccountsrF   )rr   )rm   	directionrA   r{   r   r<   networkr|   r}   r   r~   r   r   	logConfig)%r7   r:   r{   appendr<   r=   r|   r>   r}   r?   r~   rB   r   r6   r'   r8   r9   DENYr   rA   r@   r   rC   r   rD   r   rq   FirewallLogConfigrF   ry   r   r   CopyProtoMessageFirewallrm   r   rE   )rJ   rQ   rK   existingr]   r{   r<   r=   r>   r?   r   r   rA   r@   rC   rD   rx   new_firewalls                     r(   rV   zUpdateFirewall.Modify   s   zz  g		  gI&g$$k				!((kM*k((m				#++mN+m$$k				!''kL)k$$k				!''kL)kFzz			!,,TZZ-<-G-G-M-MO !++DJJ,;,F,F,K,KM					i(h'""I}}""hh22		 	 	(#55/0 ## $ < <		%	%	- ( > >34 ## $ < <		%	%	- ( > >34()??44D<O<O4Pj
vjA + 
#
#H$6$6
715  vjA??++]]  ",55 , L  }} "mmlr*   )__name__
__module____qualname____doc__r    r   r   classmethodr)   rM   ra   rT   rY   ry   rV   r.   r*   r(   r   r   !   sJ    / /F685@<ur*   r   c                        e Zd ZdZed        Zy)BetaUpdateFirewallr   c                    t        j                  dt        j                        }t	        j
                         | _        | j                  j                  |d       t        j                  |d| j                  | j                         t        j                  |d       t	        j                  |       t	        j                  ||       y r   )r
   r   r   COMPUTE_BETA_API_VERSIONr   r   r   r   r   r   r    r   r!   r"   r#   r$   s      r(   r)   zBetaUpdateFirewall.Args  s    %%i&1&J&JLH!668C%%fX%F!!44 55	7
 ,,VE	6"	VX.r*   Nr   r   r   r   r   r)   r.   r*   r(   r   r          / /r*   r   c                        e Zd ZdZed        Zy)AlphaUpdateFirewallr   c                    t        j                  dt        j                        }t	        j
                         | _        | j                  j                  |d       t        j                  |d| j                  | j                         t        j                  |d       t	        j                  |       t	        j                  ||       y r   )r
   r   r   COMPUTE_ALPHA_API_VERSIONr   r   r   r   r   r   r    r   r!   r"   r#   r$   s      r(   r)   zAlphaUpdateFirewall.Args  s    %%i&1&K&KMH!668C%%fX%F!!44 55	7
 ,,VE	6"	VX.r*   Nr   r.   r*   r(   r   r     r   r*   r   r   a          *{command}* is used to update firewall rules that allow/deny
        incoming/outgoing traffic. The firewall rule will only be updated for
        arguments that are specifically passed. Other attributes will remain
        unaffected. The `action` flag (whether to allow or deny matching
        traffic) cannot be defined when updating a firewall rule; use
        `gcloud compute firewall-rules delete` to remove the rule instead.
        zz        To update the firewall rule ``RULE'' to enable logging, run:

          $ {command} RULE --enable-logging
        )briefDESCRIPTIONEXAMPLESN)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   r   r	   r   googlecloudsdk.api_lib.utilr
   googlecloudsdk.callioper   r   rs   "googlecloudsdk.command_lib.compute1googlecloudsdk.command_lib.compute.firewall_rulesr   ReleaseTracksrP   GAUpdateCommandr   BETAr   ALPHAr   detailed_helpr.   r*   r(   <module>r      s    + &  ' % 7 : ? , ( E 9 C D%%(()[T'' [ *[| D%%**+/ / ,/& D%%++,/, / -/* 	"	  r*   