
    KC                         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Z G d d      Zd Zd Zd Zd Zd Zd Zy)z*Utils for compute instances bulk commands.    )absolute_import)division)unicode_literals)encoding)instance_utils)utils)resource_manager_tags_utils)secure_tags_utils)util)	arg_utilsNc                       e Zd ZdZd Zy)SupportedFeaturesz>Simple dataclass to hold status of supported features in Bulk.c                     || _         || _        || _        || _        || _        || _        || _        || _        d| _        |	| _	        |
| _
        || _        || _        y )NT)support_secure_tagssupport_display_devicesupport_numa_node_countsupport_snp_svsmsupport_max_count_per_zonesupport_custom_hostnames support_specific_then_x_affinitysupport_watchdog_timersupport_replica_zonessupport_graceful_shutdownsupport_source_snapshot_regionsupport_skip_guest_os_shutdown"support_preemption_notice_duration)selfr   r   r   r   r   r   r   r   r   r   r   r   s                =lib/googlecloudsdk/command_lib/compute/instances/bulk/util.py__init__zSupportedFeatures.__init__"   sl      3D"8D#:D ,D&@D#$<D!,LD)"8D!%D%>D"*HD'*HD'.PD+    N)__name__
__module____qualname____doc__r    r    r   r   r      s    FQr    r   c                 j    | j                  d      sy|j                  | |      }|j                         S )z4Get sourceInstanceTemplate value as required by API.source_instance_templateN)IsSpecifiedResolveAsResourceSelfLink)args	resourcesinstance_template_resourcerefs       r   _GetSourceInstanceTemplater/   @   s2    			4	5"44T9E#	r    c                    | j                  d      s#| j                  d      s| j                  d      sy|j                         }| j                  d      s| j                  d      r%|rt        | |      |_        nt	        | |      |_        | j                  d      r9t        j                  | j                  |j                  j                        |_	        |S )z,Get locationPolicy value as required by API.location_policymax_count_per_zonetarget_distribution_shapeN)
IsKnownAndSpecifiedLocationPolicy8_GetLocationPolicyLocationsMaxCountPerZoneFeatureEnabled	locations9_GetLocationPolicyLocationsMaxCountPerZoneFeatureDisabledr   ChoiceToEnumr3   TargetShapeValueValuesEnumtargetShape)r+   messagesmax_count_per_zone_enabledr1   s       r   _GetLocationPolicyr>   H   s     01		!	!"6	7&&'BC++-/	/0D4L4L5 "
BH  DH  
9:"+"8"8&&::#O 
r    c           	         g }| j                   j                         D ]  \  }}t        j                  ||j                  j
                        }|j                  |j                  j                  j                  dj                  |      |j	                  |                    |j                  j                  |      S )9Helper function for getting location for location policy.zones/{}
preferencekeyvalueadditionalProperties)r1   itemsr   r9   LocationPolicyLocationPreferenceValueValuesEnumappendr5   LocationsValueAdditionalPropertyformat)r+   r<   r7   zonepolicyzone_policys         r   r8   r8   j   s    )**002ldF((//IIK ..AA!!$'11[1I 	B 	
	 3 
	 	 	/	/Y	/	OOr    c                    g }| j                   r:| j                   j                         D ]  \  }}t        j                  ||j                  j
                        }| j                  r|| j                  v r}|j                  |j                  j                  j                  dj                  |      |j	                  ||j                  t        | j                  |                                  |j                  |j                  j                  j                  dj                  |      |j	                  |                    t        j                  d|j                  j
                        }| j                  r| j                  j                         D ]  \  }}| j                   r|| j                   vs| j                   r-|j                  |j                  j                  j                  dj                  |      |j	                  ||j                  t        |                                |j                  j                  |      S )r@   rA   )maxCount)rC   constraintsrD   rB   allowrG   )r1   rI   r   r9   rJ   rK   r2   rL   r5   rM   rN   rO   !LocationPolicyLocationConstraintsint)r+   r<   r7   rP   rQ   rR   zone_policy_allowed_preferencecounts           r   r6   r6   {   s"   )	,,224f**
(11KKk 
	 	 TT-D-D%D##22EE%%d+55* ( J J!$T%<%<T%B!C !K ! 6  F 
	
 	##22EE%%d+555M F 	
# 50 $-#9#9x..HH$  
..446e


4t/C/C#C""##22EE%%d+55= ( J J!$U !K ! 6  F 
	
	 7  
	 	 	/	/Y	/	OOr    c                 
   i }|r| j                  d      r| j                  }i }|D ]2  }||v r|j                  ||         ||<    |j                         ||<   4 t        j                  ||j
                  j                        S )z4Helper function for getting per_instance_properties.per_instance_hostnames)hostname)r(   r\   /BulkInsertInstanceResourcePerInstancePropertiesr   DictToAdditionalPropertyMessageBulkInsertInstanceResourcePerInstancePropertiesValue)r+   r<   instance_namesr   r\   per_instance_propertiesnames          r   _GetPerInstancePropertiesre      s     $"2"23K"L!88d%%

B
B-d3 C  d# 
B
B
D d#  
	1	1))DD
 r    c	                    | j                   }	| j                  xs g }
| j                  xs t        |
      }t	        | |j
                  |
|j                        }t        | |j
                  |j                        }|}| j                  d      r| j                  }t        | ||      }|du}t        j                  | ||dddd|j                  |j                  |j                   
      }t        j"                  | |      }t        j$                  | |d      }t        j&                  | ||      }t)        j*                  | |||||||      }t        j,                  | j.                  xs g | j0                  xs g z          }t)        j2                  | ||||      }t)        j4                  |j
                  |       }t)        j6                  |j
                  | dd|j8                  	      }t        j:                  | d      }t)        j<                  | |||      }t        j>                  |       }g }t)        j@                  | d|
      rw| j.                  xs g D ]  }d|d<   	 t)        jB                  d+i d| d|d|d|d|d|d|d|d|d|ddd|jD                  ddddddddddd|jF                  }d}t        jH                  | |      rt        jJ                  | |      }t        jL                  | |      } t)        jN                  | |      }!d}"| jP                  l| jR                  `|jT                  r| jV                  H| jX                  <| jZ                  0| j\                  s$|j^                  r| j`                  | jb                  | jX                  }#t        jd                  |j
                  | jP                  | jR                  |jT                  r| jV                  nd|#| jZ                  | j\                  |j^                  r| j`                  nd| jb                  	      }"g }$tg        | d d      }%|%r@|%D ];  }&ti        jj                  ||&|||!      }'|$jm                  |'jo                                = d}(|jp                  r7| j                  d"      r&|j
                  js                  | jt                  #      }(t        jv                  | ||jx                        })|j
                  j{                  | | j|                  ||!|||| j~                  |||||$||)|"$      }*|j                  r0| j                  r$t        j                  | j                        |*_D        | j                  rt        j                  | j                        }+|+w|j
                  jz                  },|,j                  t        t        j                  |+            D -.cg c]"  \  }-}.|,j                  j                  |-|.%      $ c}.}-&      |*_M        |jp                  r	|(r|(|*_N        |r||*_O        | j                  d'      r| j                  |*_Q        | j                  d(      rCt        j                  | j                  |j
                  j                  j                        |*_W        | j                  d)      rt        j                  | |      |*_Y        |j
                  j                  ||*||||	|*      S c c}.}-w ),z"Create bulkInsertInstanceResource.	min_countNFT)support_node_affinitysupport_min_node_cpusupport_max_run_duration"support_local_ssd_recovery_timeoutr   r   r   )instance_properties)r+   resource_parsercompute_clientholderprojectlocationscopeskip_defaults)r<   r+   )r<   r+   !support_confidential_compute_type%support_confidential_compute_type_tdxr   )r+   support_disksrs   romoder+   rp   rq   rr   rn   rm   	image_uriro   create_boot_diskboot_disk_size_gbsupport_kmsr   support_nvdimmsupport_boot_snapshot_urisupport_image_cseksupport_create_disk_snapshotsuse_disk_type_urir   )r+   rn   resource_policies)rp   rq   rr   enable_display_device)enableDisplay)canIpForwarddescriptiondisksguestAcceleratorslabelsmachineTypemetadataminCpuPlatformnetworkInterfacesserviceAccounts
schedulingtagsresourcePoliciesshieldedInstanceConfigreservationAffinityadvancedMachineFeaturesrD   rG   erase_windows_vss_signaturepost_key_revocation_action_typenetwork_performance_configs)rZ   instancePropertiesminCountperInstancePropertiessourceInstanceTemplatenamePatternlocationPolicyr%   )[name_patternpredefined_namesrZ   lenre   r<   r   r>   r   r(   rg   r/   r   GetSchedulingr   r   r   GetTags	GetLabelsGetMetadatacreate_utilsGetBulkNetworkInterfacesUseExistingBootDiskdiskcreate_diskGetImageUri"BuildShieldedInstanceConfigMessage&BuildConfidentialInstanceConfigMessager   GetConfidentialVmTypeGetProjectServiceAccountGetBootDiskSizeGbCheckSpecifiedDiskArgsCreateDiskMessagesr   r   CheckSpecifiedMachineTypeArgsCreateMachineTypeNameGetCanIpForward$GetAcceleratorsForInstancePropertiesenable_nested_virtualizationthreads_per_corer   numa_node_countvisible_core_countenable_uefi_networkingperformance_monitoring_unitr   enable_watchdog_timer
turbo_mode$CreateAdvancedMachineFeaturesMessagegetattrmaintenance_utilParseResourcePolicyWithScoperL   Namer   DisplayDevicer   GetReservationAffinityr   InstancePropertiesr   min_cpu_platformr   secure_tagsr
   GetSecureTags
secureTagsresource_manager_tagsr	   GetResourceManagerTagsResourceManagerTagsValuesortedsix	iteritemsrN   resourceManagerTagsdisplayDeviceconfidentialInstanceConfigr   eraseWindowsVssSignaturer   r9   r   Instance*PostKeyRevocationActionTypeValueValuesEnumpostKeyRevocationActionTypeGetNetworkPerformanceConfignetworkPerformanceConfigr`   )/r+   ro   rn   rm   rp   rq   rr   r-   supported_featuresr   rb   instance_countper_instance_propsr1   instance_min_countr'   rs   r   r   r   r   network_interfacesrz   ry   shielded_instance_configconfidential_instance_configconfidential_vm_typeservice_accountsr{   r   r   machine_type_namecan_ip_forwardguest_acceleratorsadvanced_machine_featuresr   parsed_resource_policiesr   rQ   resource_policy_refdisplay_devicereservation_affinityrl   ret_resource_manager_tagsproperties_messagerE   rF   s/                                                  r    CreateBulkInsertInstanceResourcer      s   $ "",((.B.::4^!4.0
11	 '
33/ &	k"7
O7 +$6-++
!#)- 2 L L

;
; 
?
?*  
		n	5$##
N& ''nmL(#<<%#!	 ((99?t//52
6
 &&
N,g) *LL&&T
 99!**,004->>  (==dDI!::
G^] %66t<
%((uM
 		Rd6l  ++   	
 & (   * ,  1FF  #'    '+!"  #$ (:'X'X%E* 11$F&<<" "11$F.#HH #
''3				*

4
4"".

!
!
-		$	$	0

*
*

3
3((4
//
%00;;##--!!!99   '',,!88 &&OO	
 $  d$7>#,II

 %%&9&>&>&@A $ ...43C3C4 $,,::00 ; N (>>
N.OO '//BB!""*#***&/5.7! C & ++0@0@%6%D%D&" 
#::&&	
 
 !,)22EE0B0[0[
 !'s}}5N'O P	  !Q*#u !99LL M  !Q	  1\ 1- ..>(6%!$ 2 
34(( 0 
786?6L6L,,((SS73
 
34224H 0 
	 	 	;	;,!.5$ 
< 
 A s   '[
)r$   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   /googlecloudsdk.api_lib.compute.instances.creater   r   "googlecloudsdk.command_lib.computer	   r
   4googlecloudsdk.command_lib.compute.resource_policiesr   r   $googlecloudsdk.command_lib.util.apisr   r   r   r/   r>   r8   r6   re   r   r%   r    r   <module>r      s[    1 &  ' % 9 Q J @ Y : 
Q QBDP"2Pj6Zr    