
    7B                         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Zd	ed
efdZd	ed
efdZd ZddZ	 ddZddZd ZddZddZd dZddZd dZd Zd Zd ZddZ	 	 ddZddZ ddZ!y)!z"Helper functions for DNS commands.    )absolute_import)division)unicode_literals)util)apis)flagsNipreturnc                 N    	 t        j                  |        y# t        $ r Y yw xY w)zReturns True if ip is an IPv4.TF)ipaddrIPv4Address
ValueErrorr	   s    *lib/googlecloudsdk/command_lib/dns/util.pyIsIPv4r      *    
r	     	$$c                 N    	 t        j                  |        y# t        $ r Y yw xY w)zReturns True if ip is an IPv6.TF)r   IPv6Addressr   r   s    r   IsIPv6r   %   r   r   c                     d}| |Bi }||d<   | |j                   j                  |       |d<   |||d<   |r |j                   di |}|S )ao  Generate a keyspec from the given (unparsed) command line arguments.

  Args:
    algorithm: (str) String mnemonic for the DNSSEC algorithm to be specified in
        the keyspec; must be a value from AlgorithmValueValuesEnum.
    key_length: (int) The key length value to include in the keyspec.
    key_type: (KeyTypeValueValuesEnum) Enum value for whether to create a
        keyspec for a KSK or a ZSK.
    messages: (module) Module (generally auto-generated by the API build rules)
        containing the API client's message classes.

  Returns:
    A messages.DnsKeySpec instance created from the given arguments.
  NkeyType	algorithm	keyLength )
DnsKeySpecAlgorithmValueValuesEnum)r   
key_lengthkey_typemessageskey_spec	spec_argss         r   ParseKeyr#   .   st      (j4I#Ii'22KK
 i)i$$$1y1h	/    c                    d}g }d}| j                   r/t        j                  d|      j                  | j                         }d}| j                  r/t        j                  d|      j                  | j                        }|dk(  r!|j
                  j                  j                  }n |j
                  j                  j                  }t        || j                  ||      }||j                  |       |dk(  r!|j
                  j                  j                  }n |j
                  j                  j                  }t        || j                  ||      }	|	|j                  |	       i }
|r||
d<   t        | dd      1t        j                   |      j                  | j"                        |
d<   | j$                  2t        j&                  ||      j                  | j$                        |
d<   |
r |j(                  d	i |
}|S )
a  Parse all relevant command line arguments and generate a DNSSEC config.

  Args:
    args: (dict{str,(str|int)}) Dict of command line arguments; value type
      dependent on particular command line argument.
    messages: (module) Module (generally auto-generated by the API build rules)
      containing the API client's message classes.
    api_version: api_version that this function should use.

  Returns:
    A messages.ManagedZoneDnsSecConfig instance populated from the given
    command line arguments.
  Nkskzskv2defaultKeySpecsdenial_of_existencenonExistencestater   )ksk_algorithmr   GetKeyAlgorithmFlagMapperGetEnumForChoicezsk_algorithmr   KeyTypeValueValuesEnumKEY_SIGNING
keySigningr#   ksk_key_lengthappendZONE_SIGNINGzoneSigningzsk_key_lengthgetattrGetDoeFlagMapperr*   dnssec_stateGetDnsSecStateFlagMapperManagedZoneDnsSecConfig)argsr    api_versiondnssec_config	key_specsr-   r0   key_enumksk_keyzsk_keydnssec_config_argss              r   ParseDnssecConfigArgsrF   O   s     -)-	33 t))*  -	33 t))* 
 D""99EEH""99DDH]D$7$78L'WD""99FFH""99EEH]D$7$78L'W,5()T($/;x(99$$	& ~& 
""'"@"@+#t(() w 4H44J7IJM	r$   c           	         g }| j                   j                  d      }| j                   j                  d      }||D ]  }t        |      r$|j                  | j                  |d|             2t	        |      r$|j                  | j                  d||             a|j                  | j                  dd||              ||D ]  }t        |      r$|j                  | j                  |d|             2t	        |      r$|j                  | j                  d||             a|j                  | j                  dd||              | j                  |      S )a  Parses list of forwarding nameservers into ManagedZoneForwardingConfig.

  Args:
    messages: (module) Module (generally auto-generated by the API build rules)
      containing the API client's message classes.
    server_list: (list) List of IP addresses to use as forwarding targets for
      the DNS Managed Zone that uses default forwarding logic (based on RFC1918
      check).
    private_server_list: (list) List of IP addresses to use as forwarding
      targets for the DNS Managed Zone that always use the private VPC path.

  Returns:
    A messages.ManagedZoneForwardingConfig instance populated from the given
    command line arguments.
  r      Nipv4Addressipv6AddressforwardingPath)rJ   rK   
domainNamerL   targetNameServers)+ManagedZoneForwardingConfigNameServerTargetForwardingPathValueValuesEnumr   r5   r   ManagedZoneForwardingConfig)r    server_listprivate_server_listtarget_serversdefault_enumprivate_enumnames          r   2ParseManagedZoneForwardingConfigWithForwardingPathrY      s   " .EEcc	,EEcc	,	@@ d< A 	

 $<@@ d< A 	
 	@@  +	 A 	
 , $#	@@ d+ A -	. $<@@ d+ A -	.
 	@@  +	 A 	
 $* 
	-	-	-	OOr$   c                     t        |      }| sg S | D cg c]"  }|j                  |j                               $ c}S c c}w z.Build PolicyNetwork message from parsed_value.)
networkUrl)GetMessagesPolicyNetworkSelfLinkparsed_valueversionr    network_refs       r   PolicyNetworkProcessorrd      sT     !(	I &
%+ (<(<(>?%
  
   '>c                     t        | d      S )z3Build Beta PolicyNetwork message from parsed_value.v1beta2rb   )rd   )ra   s    r   BetaPolicyNetworkProcessorri      s     
 i	@@r$   c                     t        |      }| sg S | D cg c]"  }|j                  |j                               $ c}S c c}w r[   )r]   ResponsePolicyNetworkr_   r`   s       r   ResponsePolicyNetworkProcessorrl      sT     !(	I &
%+ $$0D0D0F$G%
  
re   c                     t        |      }t        |       r-|j                  | d|j                  j                  d            S |j                  d| |j                  j                  d            S )a  Build a single TargetNameServer based on 'value'.

  Args:
    value: (str) A string representation of an IPV4 ip address representing the
      PrivateTargetNameServer.
    version: (str) A string indicating the version of the API to be used, should
      be 'v1' only before removing BetaTargetNameServerType.

  Returns:
    A messages.PolicyAlternativeNameServerConfigTargetNameServer instance
    populated from the given ip address.
  Nr   rI   r]   r   1PolicyAlternativeNameServerConfigTargetNameServerrQ   valuerb   r    s      r   TargetNameServerTyperr           !(E]EEQQoo
 F   EEQQoo
 F  r$   c                     t        |      }t        |       r-|j                  | d|j                  j                  d            S |j                  d| |j                  j                  d            S )a  Build a single TargetNameServer based on 'value'.

  Args:
    value: (str) A string representation of an IPV4 ip address representing the
      PrivateTargetNameServer.
    version: (str) A string indicating the version of the API to be used, should
      be one of 'v1beta2' or 'v1alpha2'. This function will be removed after
      promoting v6 address to GA.

  Returns:
    A messages.PolicyAlternativeNameServerConfigTargetNameServer instance
    populated from the given ip address.
  Nr   rI   rn   rp   s      r   BetaTargetNameServerTyperu     }     !(E]EE	:	:	&	&q	) F + + EE	:	:	&	&q	) F + +r$   c                     t        |      }t        |       r-|j                  | d|j                  j                  d            S |j                  d| |j                  j                  d            S )a  Build a single PrivateTargetNameServer based on 'value'.

  Args:
    value: (str) A string representation of an IPV4 ip address representing the
      PrivateTargetNameServer.
    version: (str) A string indicating the version of the API to be used, should
      be 'v1' only before removing BetaPrivateTargetNameServerType.

  Returns:
    A messages.PolicyAlternativeNameServerConfigTargetNameServer instance
    populated from the given ip address.
  NrH   rI   rn   rp   s      r   PrivateTargetNameServerTyperx   6  rs   r$   c                     t        |      }t        |       r-|j                  | d|j                  j                  d            S |j                  d| |j                  j                  d            S )a  Build a single PrivateTargetNameServer based on 'value'.

  Args:
    value: (str) A string representation of an IPV4 ip address representing the
      PrivateTargetNameServer.
    version: (str) A string indicating the version of the API to be used, should
      be one of 'v1beta2' or 'v1alpha2'. This function will be removed after
      promoting v6 address to GA.

  Returns:
    A messages.PolicyAlternativeNameServerConfigTargetNameServer instance
    populated from the given ip address.
  NrH   rI   rn   rp   s      r   BetaPrivateTargetNameServerTyperz   V  rv   r$   c                 4    t        | ||      }t        ||      S )z6Build a list of PolicyNetworks from command line args.)ParseNetworksrd   rq   projectrb   networkss       r   ParsePolicyNetworksr   u  s    5'73(	'	22r$   c           	          | sg S t        j                  |      }| D cg c]  }|j                  |dd|i       }}|S c c}w )zPBuild a list of PolicyNetworks or ResponsePolicyNetworks from command line args.zcompute.networksr~   )
collectionparams)api_utilGetRegistryParse)rq   r~   rb   registrynetwork_namer   s         r   r|   r|   {  sk    	I!!'*(
 <A	 <A< nn
'W%  ' <A	   
/s   ?c                 4    t        | ||      }t        ||      S )z>Build a list of ResponsePolicyNetworks from command line args.)r|   rl   r}   s       r   ParseResponsePolicyNetworksr     s    5'73(	''	::r$   c                     |s|syt        |       }g }|r||D cg c]  }t        ||        c}z  }|r||D cg c]  }t        ||        c}z  }|j                  |      S c c}w c c}w )a  Parses list of alternative nameservers into AlternativeNameServerConfig.

  Args:
    version: (str) A string indicating the version of the API to be used, should
      be 'v1' only before removing BetaParseAltNameServers.
    server_list: (Sequence) List of IP addresses to use as forwarding targets
      for the DNS managed zone that uses default forwarding logic.
    private_server_list: (Sequence) List of IP addresses to use as forwarding
      targets for the DNS Managed Zone that always uses the private VPC path.

  Returns:
    A messages.PolicyAlternativeNameServerConfig instance populated from the
    given command line arguments.Only the not none server list will be parsed
    and
    an empty list will be returned if both are none.
  NrN   )r]   rr   rx   !PolicyAlternativeNameServerConfigrb   rS   rT   r    result_listr	   s         r   ParseAltNameServersr     s    " 
0!(+L"(W5LLK;N;NR#B0;N K 
	3	3# 
4 
% % M
   A#A(c                     |s|syt        |       }g }|r||D cg c]  }t        ||        c}z  }|r||D cg c]  }t        ||        c}z  }|j                  |      S c c}w c c}w )a#  Parses list of alternative nameservers into AlternativeNameServerConfig.

  Args:
    version: (str) A string indicating the version of the API to be used, should
      be one of 'v1beta2' or 'v1alpha2'. This function will be moved after
      promoting v6 address to GA.
    server_list: (Sequence) List of IP addresses to use as forwarding targets
      for the DNS Managed Zone that uses default forwarding logic.
    private_server_list: (Sequence) List of IP addresses to use as forwarding
      targets for the DNS Managed Zone that always uses the private VPC path.

  Returns:
    A messages.PolicyAlternativeNameServerConfig instance populated from the
    given command line arguments.Only the not none server list will be parsed
    and
    an empty list will be returned if both are none.
  NrN   )r]   ru   rz   r   r   s         r   BetaParseAltNameServersr     s    ( 
0!(+KPKb,R9KPPK%%B 	(G4% K 
	3	3# 
4 
% % Qr   c                     t        |      }| j                  dk(  rE|dk(  r |j                  j                  j                  S |j                  j                  j
                  S y)z%Parses response policy rule behavior.bypassResponsePolicyr(   N)r]   behaviorResponsePolicyRuleBehaviorValueValuesEnumBYPASS_RESPONSE_POLICYr   )r>   rb   ms      r    ParseResponsePolicyRulesBehaviorr     sj    '!	]],,RY]aRa177NN  ighg{g{  hT  hT  hi  hi  ir$   c                 .    t        j                  d|       S )Ndns)r   GetMessagesModulerh   s    r   r]   r]     s    			w	//r$   )v1)NN)rg   )"__doc__
__future__r   r   r   googlecloudsdk.api_lib.dnsr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.dnsr   r   strboolr   r   r#   rF   rY   rd   ri   rl   rr   ru   rx   rz   r   r|   r   r   r   r   r]   r   r$   r   <module>r      s    ) &  ' 7 , 0 s t s t B@H 59CPL
A
@+>@+>3;%@ )-04 %F0r$   