
    E$                     ,   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mZ ddlZ ej0                  ej2                  j4                        ej6                   G d dej8                                      Z ej0                  ej2                  j<                         G d de             Z ej0                  ej2                  j@                         G d de             Z!dddde_"        y)z$Command for creating firewall rules.    )absolute_import)division)unicode_literals)base_classes)firewalls_utils)utils)apis)base)
exceptions)resource_manager_tags_utils)flagsprogress_trackerNc                   :    e Zd ZdZdZdZed        Zd Zd Z	d Z
y)Create&Create a Compute Engine firewall rule.Nc                 J   t        j                  dt        j                        }|j                  j                  t        j                         t        j                         | _	        | j                  j                  |d       t        j                  dd      | _        t        j                  |ddd       t        j                   |d	       t        j"                  |       t        j$                  ||       |j                  j'                  t        j(                         y 
Ncomputecreate)operation_typez+The network to which this rule is attached.F)requiredT)
for_updatewith_egress_supportwith_service_account)r   )r	   GetMessagesModulecompute_apiCOMPUTE_GA_API_VERSIONdisplay_info	AddFormatr   DEFAULT_LIST_FORMATFirewallRuleArgumentFIREWALL_RULE_ARGAddArgumentnetwork_flagsNetworkArgumentForOtherResourceNETWORK_ARGr   AddCommonArgsAddArgsForServiceAccountAddEnableLoggingAddLoggingMetadataAddCacheUpdaterFirewallsCompleterclsparsermessagess      ,lib/surface/compute/firewall_rules/create.pyArgszCreate.Args*   s    %%i&1&H&HJH
!!%";";<!668C%%fX%F#CC5GCO!! !	#
 ,,VF	6"	VX.
''(@(@A    c           	         |j                   }|j                  r!|j                  rt        j                  d      t        |j                        t        |j                        z  rt        j                  d      t        j                  |j                  |j                  t        j                  j                        }| j                  j                  ||j                        }| j                  j                  ||j                        }|j                  j                  ||j!                         |j"                  |j%                         |j&                  |j(                  |j*                        }|j,                  |j,                  |_        d |_        |j.                  r>|j.                  dv r0|j                  j                  j0                  j2                  |_        n/|j                  j                  j0                  j4                  |_        |j6                  |_        |j8                  |_        g }g }|j                  sDt        j                  |j                  |j                  t        j                  j                        }n|j                  dk(  rDt        j                  |j                  |j                  t        j                  j                        }nR|j                  dk(  rCt        j                  |j                  |j                  t        j                  j<                        }||_        ||_         |jB                  |_"        |jF                  |_$        |jK                  d      r"|jL                  stO        jP                  dd	      |jK                  d
      r{|j                  jS                  |jL                        }	|jK                  d      r=tU        jV                  |j                        jY                  |jZ                        |	_.        |	|_/        |jK                  d      r+| ja                  |j                  |jb                        |_2        ||jf                  fS )Nz8Can NOT specify --rules and --allow in the same request.z#Must specify --rules with --action.)allowednamedescriptionnetworksourceRanges
sourceTags
targetTags)EGRESSOUTALLOWDENYlogging_metadataz--logging-metadataz9cannot toggle logging metadata if logging is not enabled.enable_logging)enableresource_manager_tags)4clientrulesallowr   ArgumentValidationErrorboolaction
ParseRulesr1   
ActionTyper?   r'   ResolveAsResource	resourcesr#   FirewallNamer8   SelfLinksource_rangessource_tagstarget_tagsdisabled	directionDirectionValueValuesEnumr=   INGRESSprioritydestination_rangesdestinationRangesr@   r6   deniedsource_service_accountssourceServiceAccountstarget_service_accountstargetServiceAccountsIsSpecifiedrB   r   InvalidArgumentExceptionFirewallLogConfigr   GetLoggingMetadataArgGetEnumForChoicerA   metadata	logConfig_CreateFirewallParamsrD   paramsproject)
selfholderargsrE   r6   network_reffirewall_reffirewallr\   
log_configs
             r2   _CreateFirewallzCreate._CreateFirewall=   s   ]]Fzzdjj33
DF F DKK4

++33
/1 1 ((V__)8)C)C)I)IKG ""44T6;K;KLK));;f L '' $$$$&''#### ( %H }} --hH~~$..,==
//
"
"
;
;
B
B  //
"
"
;
;
C
C  H!%!8!8HGF;;**
**foo'A'A'G'GIg		**
**foo'A'A'G'GIg		))
**foo'A'A'F'FHfHHO%)%A%AH"%)%A%AH"*+D4G4G//

E 
 ()??44D<O<O4Pj			,	-#99OO


400
1 	 &h 22
//455ho \))))r4   c                    t        j                  |      }|j                  }t        t	        j
                  |            D cg c]"  \  }}|j                  j                  ||      $ }}} ||j                  |            S c c}}w )N)keyvalue)additionalProperties)resourceManagerTags)r   GetResourceManagerTagsFirewallParamssortedsix	iteritemsResourceManagerTagsValueAdditionalProperty)rk   r1   rD   resource_manager_tags_mapri   rt   ru   additional_propertiess           r2   rh   zCreate._CreateFirewallParams   s    #::!	
 
 $$F !/H!IJJJC 	''::s%:PJ   ";;!6 < 
 	s   'Bc                 t   t        j                  | j                               }|j                  }| j	                  ||      \  }}|j
                  j                  ||      }t        j                  dd      5 }|j                  |j                  j                  d|fg|      cddd       S # 1 sw Y   yxY w)z4Issues requests necessary for adding firewall rules.)rp   rj   zCreating firewallF)autotickInsertr   N)r   ComputeApiHolderReleaseTrackrE   rr   r1   ComputeFirewallsInsertRequestr   ProgressTrackerMakeRequestsapitools_client	firewalls)rk   rm   rl   rE   rp   rj   requesttrackers           r2   Runz
Create.Run   s    **4+<+<+>?F]]F,,VT:Hgoo;;7 < ,G		)	)
 
  6#9#9#C#CX#*#, "-?F ! H	
 
 
s   9+B..B7)__name__
__module____qualname____doc__r#   r'   classmethodr3   rr   rh   r    r4   r2   r   r   "   s8     /+B B$O*b"Hr4   r   c                        e Zd ZdZed        Zy)
BetaCreater   c                    t        j                  dt        j                        }|j                  j                  t        j                         t        j                         | _	        | j                  j                  |d       t        j                  dd      | _        t        j                  |ddd       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(   r)   r*   r+   r.   s      r2   r3   zBetaCreate.Args   s    %%i&1&J&JLH
!!%";";<!668C%%fX%F#CC5GCO!! !	#
 ,,VF	6"	VX.r4   Nr   r   r   r   r   r3   r   r4   r2   r   r          ./ /r4   r   c                        e Zd ZdZed        Zy)AlphaCreater   c                    t        j                  dt        j                        }|j                  j                  t        j                         t        j                         | _	        | j                  j                  |d       t        j                  dd      | _        t        j                  |ddd       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(   r)   r*   r+   r.   s      r2   r3   zAlphaCreate.Args   s    %%i&1&K&KMH
!!%";";<!668C%%fX%F#CC5GCO!! !	#
 ,,VF	6"	VX.r4   Nr   r   r4   r2   r   r      r   r4   r   r   zW
*{command}* is used to create firewall rules to allow/deny
incoming/outgoing traffic.
a  
To create a firewall rule allowing incoming TCP traffic on port 8080, run:

  $ {command} example-service --allow=tcp:8080
      --description="Allow incoming traffic on TCP port 8080" --direction=INGRESS

To create a firewall rule that allows TCP traffic through port 80 and
determines a list of specific IP address blocks that are allowed to make
inbound connections, run:

  $ {command} tcp-rule --allow=tcp:80
      --source-ranges="10.0.0.0/22,10.0.0.0/14" --description="Narrowing TCP traffic"

To list existing firewall rules, run:

  $ gcloud compute firewall-rules list

For more detailed examples see
[](https://cloud.google.com/vpc/docs/using-firewalls)
  )briefDESCRIPTIONEXAMPLES)#r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   r   r   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   r   "googlecloudsdk.command_lib.computer   1googlecloudsdk.command_lib.compute.firewall_rulesr   +googlecloudsdk.command_lib.compute.networksr%   googlecloudsdk.core.consoler   r{   ReleaseTracksr   GAUniverseCompatibleCreateCommandr   BETAr   ALPHAr   detailed_helpr   r4   r2   <module>r      s   + &  ' 7 : ? , ( . J C N 8 
 D%%(()HHT HH  *HHV D%%**+/ / ,/, D%%++,/* / -/. 6 r4   