
    q                        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
Z
 G d d	e	j                        Z G d
 de	j                        Z G d de	j                        Z G d de	j                        Z G d de	j                        Z G d de	j                        Zd Zd Z G d de	j                        Zd Zd Zy)z Util functions for NAT commands.    )absolute_import)division)unicode_literals)flags)
exceptionsNc                   "     e Zd ZdZ fdZ xZS )ActiveIpsRequiredErrorz;Raised when active ranges are not specified for Public NAT.c                 0    d}t         t        |   |       y )Nz3--source-nat-active-ips is required for Public NAT.)superr	   __init__selfmsg	__class__s     Hlib/googlecloudsdk/command_lib/compute/routers/nats/rules/rules_utils.pyr   zActiveIpsRequiredError.__init__   s    
?C	
 $05    __name__
__module____qualname____doc__r   __classcell__r   s   @r   r	   r	      s    C6 6r   r	   c                   "     e Zd ZdZ fdZ xZS )ActiveIpsNotSupportedErrorz5Raised when active IPs are specified for Private NAT.c                 0    d}t         t        |   |       y )Nz9--source-nat-active-ips is not supported for Private NAT.)r   r   r   r   s     r   r   z#ActiveIpsNotSupportedError.__init__%   s    
EC	
$d4S9r   r   r   s   @r   r   r   "   s    =: :r   r   c                   "     e Zd ZdZ fdZ xZS )ActiveRangesRequiredErrorz<Raised when active ranges are not specified for Private NAT.c                 0    d}t         t        |   |       y )Nz7--source-nat-active-ranges is required for Private NAT.)r   r   r   r   s     r   r   z"ActiveRangesRequiredError.__init__-   s    
CC	
#T3C8r   r   r   s   @r   r   r   *   s    D9 9r   r   c                   "     e Zd ZdZ fdZ xZS )ActiveRangesNotSupportedErrorz7Raised when active ranges are specified for Public NAT.c                 0    d}t         t        |   |       y )Nz;--source-nat-active-ranges is not supported for Public NAT.)r   r!   r   r   s     r   r   z&ActiveRangesNotSupportedError.__init__5   s    
GC	
'7<r   r   r   s   @r   r!   r!   2   s    ?= =r   r!   c                   "     e Zd ZdZ fdZ xZS )DrainIpsNotSupportedErrorz4Raised when drain IPs are specified for Private NAT.c                 0    d}t         t        |   |       y )Nz8--source-nat-drain-ips is not supported for Private NAT.)r   r$   r   r   s     r   r   z"DrainIpsNotSupportedError.__init__=   s    
DC	
#T3C8r   r   r   s   @r   r$   r$   :   s    <9 9r   r$   c                   "     e Zd ZdZ fdZ xZS )DrainRangesNotSupportedErrorz6Raised when drain ranges are specified for Public NAT.c                 0    d}t         t        |   |       y )Nz:--source-nat-drain-ranges is not supported for Public NAT.)r   r'   r   r   s     r   r   z%DrainRangesNotSupportedError.__init__E   s    
FC	
&6s;r   r   r   s   @r   r'   r'   B   s    >< <r   r'   c                     | j                   |j                  j                  j                  j                  j
                  k(  S )N)typeclientmessages	RouterNatTypeValueValuesEnumPRIVATE)natcompute_holders     r   _IsPrivateNatr2   J   s6    	$$..BBJJ
L Lr   c                    |j                   j                  j                  | j                  | j                  |j                   j                  j                               }t        ||      }| j                  rg|r
t               t        j                  j                  | |j                        D cg c]  }t        j                  |       c}|j                  _        n|s
t#               | j$                  rh|s
t'               t        j(                  j                  | |j                        D cg c]  }t        j                  |       c}|j                  _        |S |r
t-               |S c c}w c c}w )z4Creates a Rule message from the specified arguments.)
ruleNumbermatchaction)r+   r,   RouterNatRulerule_numberr5   RouterNatRuleActionr2   source_nat_active_ipsr   r   ACTIVE_IPS_ARG_REQUIREDResolveAsResource	resourcessix	text_typer6   sourceNatActiveIpsr	   source_nat_active_rangesr!   ACTIVE_RANGES_ARGsourceNatActiveRangesr   )argsr1   r0   ruleis_private_natipsubnets          r   CreateRuleMessagerI   O   sS   				'	'	5	5!!JJ""++??A 
6 
$
 !n5.	&(( //AA.**
&
B 	b
&DKK" 
 
""	"")++ --??.**
)
F 	f
)DKK% 
+ 
#
%%	++&)s   /E,.E1c                   "     e Zd ZdZ fdZ xZS )RuleNotFoundErrorz Raised when a Rule is not found.c                 N    dj                  |      }t        t        |   |       y )NzRule `{0}` not found)formatr   rK   r   )r   r8   r   r   s      r   r   zRuleNotFoundError.__init__u   s#    
 
'
'
4C	
T+C0r   r   r   s   @r   rK   rK   r   s    (1 1r   rK   c                 `    | j                   D ]  }|j                  |k(  s|c S  t        |      )z=Returns the Rule with the given rule_number in the given NAT.)rulesr4   rK   )r0   r8   rE   s      r   FindRuleOrRaiserP   z   s.    iid+%k  	+&&r   c                    |j                   r|j                   | _         t        ||      }|j                  rf|r
t               t        j
                  j                  ||j                        D cg c]  }t        j                  |       c}| j                  _        |j                  rg|r
t               t        j                  j                  ||j                        D cg c]  }t        j                  |       c}| j                  _        n|j                   rg | j                  _        |j"                  rf|s
t%               t        j&                  j                  ||j                        D cg c]  }t        j                  |       c}| j                  _        |j*                  rg|s
t-               t        j.                  j                  ||j                        D cg c]  }t        j                  |       c}| j                  _        y|j2                  rg | j                  _        yyc c}w c c}w c c}w c c}w )z4Updates a Rule message from the specified arguments.N)r5   r2   r:   r   r   ACTIVE_IPS_ARG_OPTIONALr<   r=   r>   r?   r6   r@   source_nat_drain_ipsr$   DRAIN_IPS_ARGsourceNatDrainIpsclear_source_nat_drain_ipsrA   r!   rB   rC   source_nat_drain_rangesr'   DRAIN_RANGES_ARGsourceNatDrainRangesclear_source_nat_drain_ranges)rE   rD   r1   r0   rF   rG   rH   s          r   UpdateRuleMessager[      s   	ZZDJ n5.	&(( //AA.**,&,B 	b,&DKK"
 
%''$)$7$7$I$I.**%,%%,bb %,%DKK! &&$&DKK!	"")++ --??.**,),F 	f,)DKK%
 
!!(** ,,>>.**,(,F 	f,(DKK$
 ))')DKK$ *?&%)(s   .H0 H50H:"H?)r   
__future__r   r   r   5googlecloudsdk.command_lib.compute.routers.nats.rulesr   googlecloudsdk.corer   core_exceptionsr>   Errorr	   r   r   r!   r$   r'   r2   rI   rK   rP   r[    r   r   <module>rb      s    ' &  ' G = 
6_22 6:!6!6 :9 5 5 9=O$9$9 =9 5 5 9<?#8#8 <L
 F1-- 1'(*r   