
    6                         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
d Zd	 Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Zd Zd Zy)z8Code that's shared between multiple subnets subcommands.    )absolute_import)division)unicode_literals)
exceptions)flagsNc                     |r |r| j                   j                  |||      S |r| j                   j                  ||      S | j                   j                  ||      S )z%Creates a subnetwork secondary range.)	rangeNamereservedInternalRangeipCidrRange)r	   r
   )r	   r   )messagesSubnetworkSecondaryRange)clientnameip_cidr_rangereserved_internal_ranges       3lib/googlecloudsdk/api_lib/compute/subnets_utils.py_CreateSecondaryRanger      sx    ??335! 4 # # ??33.E 4 G G ??33M 4 3 3    c                    g }i }i }t               }|r:|D ]5  }t        j                  |      D ]  \  }}|||<   |j                  |        7 |r:|D ]5  }t        j                  |      D ]  \  }}	|	||<   |j                  |        7 t	        |      D ]=  }|j                  t        | ||j                  |      |j                  |                   ? |S )z)Creates all secondary ranges of a subnet.)setsix	iteritemsaddsortedappendr   get)
r   secondary_range,secondary_range_with_reserved_internal_rangesecondary_rangesrange_name_to_cidr%range_name_to_reserved_internal_rangerange_names
range_namer   internal_ranges
             r   CreateSecondaryRangesr%   )   s     *,'+*'*}}_'E
#*m)6:&
# (F + 2G(+o(F
$*n<J-j9
# )G H ;'jJ 2 6 6z B155jA	CD (
 
r   c                    d }|| j                   j                         }||_        | j                   j                  |j                  |j
                  |j                         |      }| j                  | j                  j                  d|fg      S ||| j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        |j                  |_        |j                  j                  t!        | ||             | j                  t#        | ||      g      S || j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        |j                  |_        |d   D ]p  }||j                  D cg c]  }|j$                   c}vs*t'        j(                  ddj+                  ||j                  D cg c]  }|j$                   c}             |j                  D cg c]  }|j$                  |d   vr| c}|_        g }|j                  s|j-                  d	       | j                  j/                  |      5  | j                  t#        | ||      g      cddd       S ||	|	|
||D| j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        | j                   j1                  |
      }|3t3        j4                  | j                         j7                  |      |_        |	|	|_        |
3t3        j<                  | j                         j7                  |
      |_        |||_         |||_!        ||_"        | j                  t#        | ||      g      S || j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        | j                   j                  jG                  tI         ||                  |_%        | j                  t#        | ||      g      S || j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        ||_&        | j                  t#        | ||      g      S || j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        | j                   j                  jO                  |      |_(        | j                  t#        | ||      g      S || j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        | j                   j                  jR                  jT                  |_+        | j                   jY                  |j                  |jZ                  |j
                  ||      }| j                  | j                  j                  d|fg      S |	||||| j                   j                         }| j                  | j                  j                  d | j                   j                  di |j                         fg      d   }|j                  |_        |*| j                   j                  j]                  |      |_/        |*| j                   j                  ja                  |      |_1        |||_2        |||_3        |||_4        | j                  t#        | ||      g      S | j                  g       S c c}w c c}w c c}w # 1 sw Y   )xY w)a  Make the appropriate update request for the args.

  Args:
    client: GCE API client
    subnet_ref: Reference to a subnetwork
    enable_private_ip_google_access: Enable/disable access to Google Cloud APIs
      from this subnet for instances without a public ip address.
    allow_cidr_routes_overlap: Allow/Disallow this subnetwork's ranges to
      conflict with existing static routes.
    add_secondary_ranges: List of secondary IP ranges to add to the subnetwork
      for use in IP aliasing.
    add_secondary_ranges_with_reserved_internal_range: List of secondary IP
      ranges that are associated with InternalRange resources.
    remove_secondary_ranges: List of secondary ranges to remove from the
      subnetwork.
    enable_flow_logs: Enable/disable flow logging for this subnet.
    aggregation_interval: The internal at which to aggregate flow logs.
    flow_sampling: The sampling rate for flow logging in this subnet.
    metadata: Whether metadata fields should be added reported flow logs.
    filter_expr: custom CEL expression for filtering flow logs
    metadata_fields: custom metadata fields to be added to flow logs
    set_new_purpose: Update the purpose of the subnet.
    set_role_active: Updates the role of a BACKUP subnet to ACTIVE.
    drain_timeout_seconds: The maximum amount of time to drain connections from
      the active subnet to the backup subnet with set_role_active=True.
    private_ipv6_google_access_type: The private IPv6 google access type for the
      VMs in this subnet.
    stack_type: The stack type for this subnet.
    ipv6_access_type: The IPv6 access type for this subnet.
    external_ipv6_prefix: The IPv6 external prefix to be assigned to this
      subnet.
    internal_ipv6_prefix: The IPv6 internal prefix to be assigned to this
      subnet. When ULA is enabled, the prefix will be ignored.
    ip_collection: The IP collection that provisions BYOIP v6 addresses for this
      subnet.

  Returns:
    response, result of sending the update request for the subnetwork
  c                 B    | j                  dd      j                         S )N-_)replaceupper)xs    r   <lambda>z-MakeSubnetworkUpdateRequest.<locals>.<lambda>   s    aiiS1779r   N)projectregion
subnetwork*subnetworksSetPrivateIpGoogleAccessRequestSetPrivateIpGoogleAccessGetr   zremove-secondary-rangesz;Subnetwork does not have a range {}, present ranges are {}.secondaryIpRanges)enable)r.   r0   r/   subnetworkResourcedrainTimeoutSecondsPatch )5r   *SubnetworksSetPrivateIpGoogleAccessRequestprivateIpGoogleAccess1ComputeSubnetworksSetPrivateIpGoogleAccessRequestr.   r/   NameMakeRequestsapitools_clientsubnetworks
SubnetworkComputeSubnetworksGetRequestAsDictr4   fingerprintextendr%   CreateSubnetworkPatchRequestr	   calliope_exceptionsUnknownArgumentExceptionformatr   IncludeFieldsSubnetworkLogConfigr    GetLoggingAggregationIntervalArgGetEnumForChoiceaggregationIntervalflowSamplingGetLoggingMetadataArgmetadata
filterExprmetadataFields	logConfig&PrivateIpv6GoogleAccessValueValuesEnumConvertPrivateIpv6GoogleAccessprivateIpv6GoogleAccessallowSubnetCidrRoutesOverlapPurposeValueValuesEnumpurposeRoleValueValuesEnumACTIVEroleComputeSubnetworksPatchRequestr0   StackTypeValueValuesEnum	stackTypeIpv6AccessTypeValueValuesEnumipv6AccessTypeexternalIpv6PrefixinternalIpv6PrefixipCollection) r   
subnet_refenable_private_ip_google_accessallow_cidr_routes_overlapadd_secondary_ranges1add_secondary_ranges_with_reserved_internal_rangeremove_secondary_rangesenable_flow_logsaggregation_intervalflow_samplingrQ   filter_exprmetadata_fieldsset_new_purposeset_role_activedrain_timeout_secondsprivate_ipv6_google_access_type
stack_typeipv6_access_typeexternal_ipv6_prefixinternal_ipv6_prefixip_collectionconvert_to_enumgoogle_accessgoogle_access_requestr0   original_subnetworkr   rcleared_fields
log_configpatch_requests                                    r   MakeSubnetworkUpdateRequestr   B   s6	   ~ :/$0BBD *IM' 	II&&$$!(7D	 	J 	F  				+	+-G		     	(9E++-J --				+	+U	5	5	5	L
8I8I8K	L	N/  	
 $7#H#HJ 0<<J  ''(=	?@
 	%fj*	EFH H*++-J --				+	+U	5	5	5	L
8I8I8K	L	N/  	
 $7#H#HJ 0<<J'*	:+G+GH+Gaakk+GH	H!::% (%%+VJ,H,HI,Hqq{{,HI&KL 	L + '88$8a;;5a88 	
8$J 
 N''/0				-	-n	=  '
JG
HJ 
>	= 	$(<(H!X%9?#>++-J --				+	+U	5	5	5	L
8I8I8K	L	N/  	
 1<<J44<L4MJ'','M'M
//(++,@A $  -j!77
//++H5 )j""1j%J	%fj*	EFH H&2++-J --				+	+U	5	5	5	L
8I8I8K	L	N/  	
 1<<J 	""II* ?@B	C & 	%fj*	EFH H ,++-J --**444Kz7H7H7JK0 /  	
	
 1<<J.GJ+	%fj*	EF  "++-J --				+	+U	5	5	5	L
8I8I8K	L	N/  	
 1<<J33JJJ	%fj*	EFH H"++-J --				+	+U	5	5	5	L
8I8I8K	L	N/  	
 1<<Joo00DDKKJOOOBB""((  %1 C M 

 
 
,
,g}	EF  		"		%		)		)++-J --**444Kz7H7H7JK0 /  	
	
 1<<J
//
$
$
=
=j
I  #
//
$
$
B
B 
 '&:j#'&:j#  -j	%fj*	EF  
		R	  O I J$ 
>	=s   cc!c&c++c4c                     | j                   j                  |j                  |j                  |j                  |      }| j
                  j                  d|fS )N)r.   r0   r/   r6   r8   )r   r^   r.   r0   r/   r?   r@   )r   rf   subnetwork_resourcer   s       r   rF   rF   6  sU    //@@  &&,	 A .-
 
 
 
,
,g}	EEr   c                 0    dddd}|j                  |       S )zReturn PrivateIpv6GoogleAccess enum defined in mixer.

  Args:
    choice: Enum value of PrivateIpv6GoogleAccess defined in gcloud.
  DISABLE_GOOGLE_ACCESS%ENABLE_BIDIRECTIONAL_ACCESS_TO_GOOGLE#ENABLE_OUTBOUND_VM_ACCESS_TO_GOOGLE)DISABLEENABLE_BIDIRECTIONAL_ACCESSENABLE_OUTBOUND_VM_ACCESS)r   )choicechoices_to_enums     r   rV   rV   ?  s'     )%L#H/
 
		V	$$r   )NNNNNNNNNNNNNNNNNNNN)__doc__
__future__r   r   r   googlecloudsdk.callioper   rG   3googlecloudsdk.command_lib.compute.networks.subnetsr   r   r   r%   r   rF   rV   r9   r   r   <module>r      sv    ? &  ' E E 
38 %)"6: $(-q!hF%r   