
    8                         S r SSKrSSKJrJrJr  SSKJr  SSKJ	r	  SSKJ
r  SSKJr  SSKJr  SSKJr  SS	KJr   " S
 S\5      r " S S\5      rg)z1Utilities for Backup and DR restore command apis.    N)AnyDictList)encoding)alias_ip_range_utils)	constants)
exceptions)yaml)filesc            	          \ rS rSrSr\S\\\4   S\	4S j5       r
\S\\\4   S\	4S j5       r\S\R                  4S j5       r\S\R                  S	\S
\\   4S j5       r\S\R                  S\\\\4      4S j5       r\S\R                  S\\\4   4S j5       r\S\R                  S\\\4   4S j5       r\S\R                  S\	S\S\4S j5       r\S\R                  S\\\4   4S j5       r " S S\R0                  5      r\S\R                  S\4S j5       rSr\S\R                  S\S\4S j5       rS rg!)"ComputeUtil   z1Util class for Restoring Compute Engine Instance.network_interfacereturnc                     SU ;   $ )Nexternal-ipv6-address r   s    3lib/googlecloudsdk/api_lib/backupdr/restore_util.py_HasIpV6AccessConfig ComputeUtil._HasIpV6AccessConfig!   s    "&777    c                     SU ;   $ )Naddressr   r   s    r   _HasIpV4AccessConfig ComputeUtil._HasIpV4AccessConfig%   s    )))r   client_messagesc                 P   Uc  g[        5       nU GH  nU R                  5       nU R                  5       nU R                  5       nSU;   a
  US   Ul        SU;   a
  US   Ul        SU;   a
  US   Ul        SU;   a2  US   Ul        SU;   a  US   Ul        O[        R                  " SS5      eSU;   a
  US   Ul
        S	U;   a2  US	   Ul        S
U;   a  US
   Ul        O[        R                  " S
S5      eSU;   aF  U R                  R                  US   5      Ul        U R                  R                  US   5      Ul        SU;   a   [        R                   " U SUS   5      Ul        SU;   a#  U R                  R%                  US   5      Ul        SU;   a
  US   Ul        SU;   a#  U R                  R+                  US   5      Ul        SU;   a
  US   Ul        [0        R3                  U5      (       aB  U R                  R4                  R6                  Ul        UR:                  R=                  U/5        O[[0        R?                  U5      (       aA  U R                  R4                  R@                  Ul        URB                  R=                  U/5        URE                  U5        GM     U$ )a  Parses the network interface data into client messages.

Args:
  client_messages:
  network_interfaces: A dictionary containing the network interface data

Returns:
  List of parsed client messages for Network Interface

Raises:
  InvalidArgumentException:
Nnetworksubnetzprivate-network-ipzinternal-ipv6-addresszinternal-ipv6-prefix-lengthzDPrefix length of the provided IPv6 address is expected but not foundr   r   zexternal-ipv6-prefix-lengthznetwork-tieraliasesTz
stack-typezqueue-countznic-typeznetwork-attachment)#listNetworkInterfaceAccessConfigr   
subnetwork	networkIPipv6AddressinternalIpv6PrefixLengthr	   InvalidArgumentExceptionnatIPexternalIpv6externalIpv6PrefixLengthNetworkTierValueValuesEnumnetworkTierr   $CreateAliasIpRangeMessagesFromStringaliasIpRangesStackTypeValueValuesEnum	stackType
queueCountNicTypeValueValuesEnumnicTypenetworkAttachmentr   r   TypeValueValuesEnumDIRECT_IPV6typeipv6AccessConfigsextendr   ONE_TO_ONE_NATaccessConfigsappend)r   network_interfacesmessagesr   messageaccess_configaccess_config_ipv6s          r   ParserNetworkInterface"ComputeUtil.ParserNetworkInterface)   s     !vH/002g%224m*779	'	'+I6	&	&.x8	!2	2-.BC	 $5	5/0GH(,==->+.'
* 33+ 
 
'	'/	:	 $5	5*;#+
' ),==8I+9

5 33+ 
 
,	,((CC!.1 	! ((CC!.1 	&
 
'	' EE!), 	 
*	*,,EE!,/ 	
 
+	+.}=	(	(,,CC!*- 	
 
!2	2$56J$K!		)	)*;	<	<((<<HH 	 	!!((*<)=>++,=>>((<<KK 	 	$$m_5oogi 0j Or   service_accountscopesc                 d    S nUc  SnUc  [         R                  nU R                  X" U5      S9/$ )zParses the service account data into client messages.

Args:
  client_messages:
  service_account: An email id of the service account
  scopes: A list containing the scopes

Returns:
  List of parsed client messages for Service Account
c                     [        5       nU  H4  n[        R                  R                  X"/5      nUR	                  U5        M6     U$ )N)r"   compute_constantsSCOPESgetr;   )rG   converted_scopesscopes      r   _ConvertAliasToScopes?ComputeUtil.ParserServiceAccount.<locals>._ConvertAliasToScopes   sB    %!((,,UG<&  r   default)emailrG   )rJ   DEFAULT_SCOPESServiceAccount)r   rF   rG   rO   s       r   ParserServiceAccount ComputeUtil.ParserServiceAccount   sL     !o~ //f&&!*?*G 	' 	
 r   disksc                 b   Uc  g[        5       nU H  nU R                  5       nU R                  5       Ul        SU;   a
  US   Ul        SU;   a  US   UR                  l        SU;   a  US   UR                  l        SU;   a  U R                  US   S9Ul        UR                  U5        M     U$ )zParses the disk data into client messages.

Args:
  client_messages:
  disks: A list of dictionaries containing the disk data

Returns:
  List of parsed client messages for Disk
Nzdevice-namenamezreplica-zoneszkms-key)
kmsKeyName)
r"   AttachedDiskInitializeParamsinitializeParams
deviceNamediskNamereplicaZonesCustomerEncryptionKeydiskEncryptionKeyr>   )r   rW   r@   diskrA   s        r   ParserDisksComputeUtil.ParserDisks   s     }vH,,.g!0!A!A!Cg	$	!-0	4,0L  )	D	 04_0E  -	d	11	? 2  	!
 oog   Or   metadatac                     U R                  UR                  5        VVs/ s H  u  p#U R                  X#S9PM     snnS9$ s  snnf )zParses the metadata data into client messages.

Args:
  client_messages:
  metadata: A dictionary containing the metadata

Returns:
  List of parsed client messages for Metadata
keyvalue)items)Metadatark   Entry)r   rf   ri   rj   s       r   ParseMetadataComputeUtil.ParseMetadata   sU     ## 'nn.
.
 !!c!7.
 $  
s   A 
labelsc                     U R                   R                  UR                  5        VVs/ s H(  u  p#U R                   R                  R                  X#S9PM*     snnS9$ s  snnf zParses the labels data into client messages.

Args:
  client_messages:
  labels: A dictionary containing the labels

Returns:
  List of parsed client messages for Labels
rh   )additionalProperties) ComputeInstanceRestorePropertiesLabelsValuerk   AdditionalPropertyr   rp   ri   rj   s       r   ParseLabelsComputeUtil.ParseLabels   sq     ;;GG
 %lln	
 -
 <<HH[[ \  -	
 H  
   /A
enable_uefi_networkingthreads_per_corevisible_core_countc                 p    Uc  Uc  Uc  gU R                  5       nUb  Xl        Ub  X$l        Ub  X4l        U$ )zParses the advanced machine features data into client messages.

Args:
  client_messages:
  enable_uefi_networking:
  threads_per_core:
  visible_core_count:

Returns:
  List of parsed client messages for AdvancedMachineFeatures
N)AdvancedMachineFeaturesenableUefiNetworkingthreadsPerCorevisibleCoreCount)r   r{   r|   r}   rA   s        r   ParseAdvancedMachineFeatures(ComputeUtil.ParseAdvancedMachineFeatures   sP    & 	&$&557G)%;"#/%!3Nr   acceleratorc                 ^    Ub  SU;  a  gU R                  US   UR                  SS5      S9/$ )zParses the accelerator data into client messages.

Args:
  client_messages:
  accelerator: A dictionaries containing the accelerator data

Returns:
  List of parsed client messages for Accelerator
Nr9   count   )acceleratorTypeacceleratorCount)AcceleratorConfigrL   )r   r   s     r   ParseAcceleratorComputeUtil.ParseAccelerator  sI     fK7 	))'/(__Wa8 	* 	
 r   c                       \ rS rSrSrSrg)&ComputeUtil.NodeAffinityFileParseErrori5  z6Error raised when node affinity file cannot be parsed.r   N)__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   NodeAffinityFileParseErrorr   5  s    @r   r   	file_pathc                    Uc  g[         R                  " U5      n[        R                  " U5      nU(       d  [        R                  S5      e/ nU H  nU(       d  [        R                  S5      e [        R                  " U R                  U5      nUR                  (       d  [        R                  S5      eUR                  5       (       a=  [        R                  SR                  UR                  UR                  5       5      5      eUR                  U5        M     U$ ! [         a*  n[        R                  SR                  U5      5      UeSnAff = f)zParses the node affinity data from file into client messages.

Args:
  client_messages:
  file_path: A path to the file containing the node affinity data.

Returns:
  List of parsed client messages for NodeAffinity

Raises:
  NodeAffinityFileParseError:
NziNo node affinity labels specified. You must specify at least one label to create a sole tenancy instance.z"Empty list item in JSON/YAML file.z6Failed to parse node affinity values from the file {}.z6A key must be specified for every node affinity label.z,Key [{0}] has invalid field formats for: {1})r   ReadFileContentsr
   loadr   r   r   PyValueToMessageNodeAffinity	Exceptionformatri   all_unrecognized_fieldsr>   )r   r   node_affinities_fileaffinities_yamlnode_affinitiesaffinitynode_affinityes           r   GetNodeAffinitiesFromFile%ComputeUtil.GetNodeAffinitiesFromFile8  sL   "  11)<ii 45O225 
 O#440
 	
	 11(((
 44D
 	
 
	.	.	0	044:AA!!=#H#H#J
 	

 ]+3 $4 #  44DKK
 		s   1!D!!
E+%EEz'compute.googleapis.com/reservation-namereservation_affinityreservationc                    Uc  gUS:X  a-  U R                  U R                   R                  R                  S9$ US:X  a-  U R                  U R                   R                  R                  S9$ US:X  aX  Uc  [        R
                  " SS5      eU R                  U R                   R                  R                  [        R                  U/S9$ g)	a"  Parses the reservation affinity data into client messages.

Args:
  client_messages:
  reservation_affinity: type of reservation affinity
  reservation: name of the specific reservation

Returns:
  List of parsed client messages for ReservationAffinity

Raises:
  InvalidArgumentException:
Nany)consumeReservationTypenonespecificr   z9Reservation is required for specific reservation affinity)r   ri   values)	AllocationAffinity%ConsumeReservationTypeValueValuesEnumANY_RESERVATIONNO_RESERVATIONr	   r)   SPECIFIC_RESERVATIONr   RESERVATION_AFFINITY_KEY)r   r   r   s      r   ParseReservationAffinity$ComputeUtil.ParseReservationAffinitys  s    & #u$//!0!C!C!i!i!y!y 0   v%//!0!C!C!i!i!x!x 0   z)		11G
 	
 //!0!C!C!i!i!~!~22 0  
 r   r   N)r   r   r   r   r   staticmethodr   strr   boolr   r   types
ModuleTyperD   r   rU   rd   rn   rx   intr   r   core_exceptionsErrorr   r   r   r   r   r   r   r   r   r      s8   98d38n 8 8 8 *d38n * * * g''g gR '':=GKCy > ''04T#s(^0D B ''37S> & 5#3#3 T#s(^  & ''"  	 @ ''6:38n ,A?#8#8 A 6''6476 6p G*''** * *r   r   c                   R    \ rS rSrSr\S\R                  S\\	\
4   4S j5       rSrg)DiskUtili  zUtil class for Restoring Disk.r   rp   c                     U R                   R                  UR                  5        VVs/ s H(  u  p#U R                   R                  R                  X#S9PM*     snnS9$ s  snnf rr   )DiskRestorePropertiesru   rk   rv   rw   s       r   rx   DiskUtil.ParseLabels  sp     00<<
 %lln	
 -
 11==PP Q  -	
 =  
rz   r   N)r   r   r   r   r   r   r   r   r   r   r   rx   r   r   r   r   r   r     s3    &5#3#3 T#s(^  r   r   )r   r   typingr   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   r   rJ   googlecloudsdk.callioper	   googlecloudsdk.corer   r
   googlecloudsdk.core.utilr   objectr   r   r   r   r   <module>r      sE    8  " " % ? I . = $ *@& @Fv r   