
    H                        d Z ddlmZ ddlmZ ddlmZ ddl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ddgZg dZdgZddgZdZdZ eddg      Z G d de
j>                        Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)	 	 	 	 	 d3dZ*d4d Z+d! Z,d" Z-d# Z.d$ Z/d% Z0d& Z1d' Z2d( Z3d) Z4d* Z5d+ Z6d5d,Z7d- Z8d. Z9	 	 	 	 d6d/Z:d0 Z;d4d1Z<d2 Z=y)7z7Flags and helpers for the compute commitments commands.    )absolute_import)division)unicode_literalsN)arg_parsers)
completers)flags)scope)resource_args)	arg_utils12-month36-month)r   r   60-monthr   z%Y-%m-%dT%H:%M:%SZz%Y-%m-%dvcpumemoryc                        e Zd Z fdZ xZS )RegionCommitmentsCompleterc                 2    t        t        | 
  dddd| y )Ncompute.regionCommitmentsz$alpha compute commitments list --uri)
collectionlist_command )superr   __init__)selfkwargs	__class__s     ;lib/googlecloudsdk/command_lib/compute/commitments/flags.pyr   z#RegionCommitmentsCompleter.__init__.   s(    	
$d4 .;     )__name__
__module____qualname__r   __classcell__)r   s   @r   r   r   ,   s     r   r   c                    | j                   j                  j                  | j                   j                  j                  d}t	        | j                   j                  d      r#| j                   j                  j
                  |d<   |S )N)r   r   SIXTY_MONTHr   )
CommitmentPlanValueValuesEnumTWELVE_MONTHTHIRTY_SIX_MONTHhasattrr$   )messagesresults     r   _GetFlagToPlanMapr,   6   sl    %%99FF%%99JJ& X  44mD!,,@@LLF:	-r   c                     t        |       |   S N)r,   )r*   plan_args     r   TranslatePlanArgr0   @   s    	8	$X	..r   c                 >    | j                  d      r| j                  S y)N
auto_renewFIsSpecifiedr2   argss    r   TranslateAutoRenewArgForCreater7   D   s    	l#??	r   c                 >    | j                  d      r| j                  S y )Nr2   r3   r5   s    r   TranslateAutoRenewArgForUpdater9   J   s    	l#??	r   c                    | j                  d      rd}d}	 t        j                  j                  | j                  t               | j                  }|r	 t        j                  d      }|j                  t        j                  j                  | j                  t                    }|j                  t        j                        j                  t              }|st        d      |S y# t
        $ r d}Y w xY w# t
        $ r Y ,w xY w)z7Translates the custom end time arg to a RFC3339 format.custom_end_timeFNTzAmerica/Los_AngeleszLInvalid custom end time. Expected format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SSZ)r4   datetimestrptimer;   RFC3339_FORMAT
ValueErrorpytztimezonelocalizeDATE_ONLY_FORMAT
astimezoneutcstrftime)r6   try_date_only_parsefinal_date_timetzmidnight_date_time_mtvs        r   TranslateCustomEndTimeArgrK   P   s    	'(O!  !5!5~F,,o ]]01!#&&t';';=MN"
 1;;DHHENN
 " 
 	7  ! !!  s$   :C( BC9 (C65C69	DDc                     | j                  |d   | j                   j                  j                        | j                  |d   dz  | j                   j                  j                        gS )Nr   amounttyper   i   )ResourceCommitmentTypeValueValuesEnumVCPUMEMORY)r*   resources_args     r   TranslateResourcesArgrU   w   ss    !!v&**>>CC " 
 !!x([9**>>EE " 

 
r   c                    |j                   }t        | |      }d|j                         v rC|j                  | j	                  |d   | j                  j
                  j                               |j                  d      rS|j                  }|j                  | j	                  |d   |d   | j                  j
                  j                               |S )z,Util functions to parse ResourceCommitments.	local-ssdrM   resources_acceleratorcountrO   )rN   acceleratorTyperO   )
	resourcesrU   keysappendrP   rQ   	LOCAL_SSDr4   rX   ACCELERATOR)r*   r6   rT   r[   accelerator_args        r   TranslateResourcesArgGroupra      s    ..-#Hm<)M&&((## -,,@@JJ 	$ 	
 
-.00O##"7++F3,,@@LL 	$ 	
 
r   c                 ,    | r| j                  d      S g S )z)List arguments are delineated by a comma.,)split)args    r   TranslateMergeArgrf      s    3&B&r   c                 \    t        j                  dt        | ddt         j                        S )N
commitmentr   )resource_name	completerpluralnameregional_collectionregion_explanation)compute_flagsResourceArgumentr   REGION_PROPERTY_EXPLANATION)rk   s    r   MakeCommitmentArgrr      s-    		'	' *5&BB
 r   c                     t        | |       t        | ||||       t        |        t        |        t	        |        t        |        y)z4Add general arguments for `commitments create` flag.N)AddPlanForCreateAddReservationArgGroupAddResourcesArgGroupAddSplitSourceCommitmentAddMergeSourceCommitmentsAddCustomEndTime)parsersupport_share_settingsupport_stable_fleetsupport_existing_reservation"support_reservation_sharing_policysupport_60_month_plans         r   AddCreateFlagsr      sH     601"( v6"F#6r   c                 2    t        |        t        | |       y)z4Add general arguments for `commitments update` flag.N)AddAutoRenewAddPlanForUpdaterz   r   s     r   AddUpdateFlagsr      s    v601r   c                 P    | j                  dd|rt        d      S t        d      S )N--planTDuration of the commitment.requiredchoiceshelp)add_argumentEXTENDED_VALID_PLANSVALID_PLANSr   s     r   rt   rt      sB    			&;"(	 
 
  BM(	 
 
 r   c                 P    | j                  dd|rt        d      S t        d      S )Nr   Fr   r   )r   EXTENDED_VALID_UPDATE_PLANSVALID_UPDATE_PLANSr   s     r   r   r      sD    				 * ) 
 
 
 ( 
 
 r   c                 ,    | j                  dddd      S )Nz--auto-renew
store_trueFz'Enable auto renewal for the commitment.)actiondefaultr   r   rz   s    r   r   r      s&    			4	 
 
 r   c                     | j                  ddd       | j                  ddt        d       | j                  d	dt        d
       t        | d       y)z6Add license based flags for `commitments create` flag.z	--licenseTzApplicable license URI. For example: `https://www.googleapis.com/compute/v1/projects/suse-sap-cloud/global/licenses/sles-sap-12`r   r   z--cores-per-licenseFzZCore range of the instance. Must be one of: `1-2`, `3-4`, `5+`. Required for SAP licenses.r   rO   r   z--amountzNumber of licenses purchased.)r   N)r   strintrt   r   s    r   AddLicenceBasedFlagsr      so    h	   	5   	4c0O   67r   c                 *    | j                  ddd      S )Nz--split-source-commitmentFzsCreates the new commitment by splitting the specified source commitment and redistributing the specified resources.r   r   r   s    r   rw   rw     s&    			!J	 
 
 r   c                 *    | j                  ddd      S )Nz--merge-source-commitmentsFzeCreates the new commitment by merging the specified source commitments and combining their resources.r   r   r   s    r   rx   rx     s%    			">	 
 
 r   c                 4    | j                  ddt        d      S )Nz--custom-end-timeFzMSpecifies a custom future end date and extends the commitment's ongoing term.r   )r   r   r   s    r   ry   ry     s(    			 
 
 r   c           
      $   | j                  dd      }d}|j                  d|t        j                  t        t        t        j
                         d             d	}|j                  d
|t        j                  t        t        d             y)zDAdd the argument group for ResourceCommitment support in commitment.z/Manage the commitment for particular resources.Tr   a  Resources to be included in the commitment. For details and examples of valid
specifications, refer to the
[custom machine type guide](https://cloud.google.com/compute/docs/instances/creating-instance-with-custom-machine-type#specifications).
*memory*::: The size of the memory, should include units (e.g. 3072MB or 9GB). If no units are specified, GB is assumed.
*vcpu*::: The number of the vCPU cores.
*local-ssd*::: The size of local SSD.
z--resources)r   rW   r   spec)r   rO   aN  Manage the configuration of the type and number of accelerator cards to include in the commitment.
*count*::: The number of accelerators to include.
*type*::: The specific type (e.g. nvidia-tesla-k80 for NVIDIA Tesla K80) of the accelerator. Use `gcloud compute accelerator-types list` to learn about all available accelerator types.
z--resources-accelerator)rY   rO   N)	add_groupr   r   ArgDictr   
BinarySizer   )rz   resources_groupresources_helpaccelerator_helps       r   rv   rv   $  s    $$7$ % /. #..0  

 c3$?@  r   c                 `    t        j                  d| j                  j                  ddd       S )z:Helper to get a choice flag from the commitment type enum.z--typezaType of commitment. `memory-optimized` indicates that the commitment is for memory-optimized VMs.zgeneral-purposec                     | dk7  S )NTYPE_UNSPECIFIEDr   )xs    r   <lambda>z#GetTypeMapperFlag.<locals>.<lambda>T  s
    q$66r   )help_strr   include_filter)r   ChoiceEnumMapperr%   rQ   )r*   s    r   GetTypeMapperFlagr   J  s4    		#	#--4  6	
 	r   c                    | j                  dd      }t        |d       |j                  d      }t        |       t        j                  d      j                  |       t        j                  d      j                  |       t        j                  d	      j                  |       t        j                  d
      j                  |       t        j                  d      j                  |       t        j                  d      j                  |       t        j                  d      j                  |       t        j                  d      j                  |       | S )z=Add reservation arguments to the update-reservations command.z8Manage reservations that are attached to the commitment.Tmutexz7Path to a YAML file of two reservations' configuration.)custom_textz:Specify source and destination reservations configuration.z--source-acceleratorz--dest-acceleratorz--source-local-ssdz--dest-local-ssdz--source-share-settingz--source-share-withz--dest-share-settingz--dest-share-with)	r   AddReservationsFromFileFlagAddReservationArgumentsreservation_flagsGetAcceleratorFlagAddToParserGetLocalSsdFlagGetSharedSettingFlagGetShareWithFlag)rz   parent_reservations_groupreservations_groups      r   AddUpdateReservationGroupr   X  sK   $..@ /  K 1::B ,-&&'=>JJ &&';<HH ##$89EE ##$67CC
 (()ABNN $$%:;GG (()?@LL $$%89EE 
-r   c                 ,   d}t         t         t        t         t         t        d}| j                  dt	        j
                  |      |j                  d      d       | j                  dt	        j
                  |      |j                  d	      d       | S )
zDAdd --source-reservation and --dest-reservation arguments to parser.a~  
{0} reservation configuration.
*reservation*::: Name of the {0} reservation to operate on.
*reservation-zone*:::  Zone of the {0} reservation to operate on.
*vm-count*::: The number of VM instances that are allocated to this reservation.
The value of this field must be an int in the range [1, 1000].
*machine-type*:::  The type of machine (name only) which has a fixed number of
vCPUs and a fixed amount of memory. This also includes specifying custom machine
type following `custom-number_of_CPUs-amount_of_memory` pattern, e.g. `custom-32-29440`.
*min-cpu-platform*::: Optional minimum CPU platform of the reservation to create.
*require-specific-reservation*::: Indicates whether the reservation can be consumed by VMs with "any reservation"
defined. If enabled, then only VMs that target this reservation by name using
`--reservation-affinity=specific` can consume from this reservation.
)reservationzreservation-zonezvm-countzmachine-typezmin-cpu-platformzrequire-specific-reservationz--source-reservationr   sourceT)rO   r   r   z--dest-reservationdestination)r   r   boolr   r   r   format)rz   	help_textreservation_specs      r   r   r     s    ) &* 	$45H%	   	$45M*	   
-r   c                 Z    |r|nd}| j                  dt        j                         |      S )Nz<Path to a YAML file of multiple reservations' configuration.z--reservations-from-file)rO   r   )r   r   FileContents)rz   r   r   s      r   r   r     s@     
 I 
 
		 ##% 
 
 r   c                 t    d}| j                  dt        j                  t        t        dddg      d|      S )	z.Add --existing-reservation argument to parser.a:  
  Details of the existing on-demand reservation or auto-created future
  reservation that you want to attach to your commitment. Specify a new instance
  of this flag for every existing reservation that you want to attach. The
  reservations must be in the same region as the commitment.
  *name*::: The name of the reservation.
  *zone*::: The zone of the reservation.
  For example, to attach an existing reservation named reservation-name in the
  zone reservation-zone, use the following text:
  --existing-reservation=name=reservation-name,zone=reservation-zone
  z--existing-reservation)rl   zonerl   r   )r   required_keysr]   )rO   r   r   )r   r   r   r   )rz   r   s     r   AddExistingReservationFlagr     sK    
) 
		S)&&9I  
 
 r   c                 X   t         j                  j                  dt        j                  j
                  di      }t        | dd      }|g S g }|D ]W  }|j                  |d   gt        j                  j
                  |d   |      d   }|j                  |j                                Y |S )z9Method to translate existing-reservations args into URLs.r   zcompute.reservationsexisting_reservationNrl   r   r   )
ro   ResourceResolverFromMapcompute_scope	ScopeEnumZONEgetattrResolveResourcesr]   SelfLink)r6   r[   resolverexisting_reservationsreservation_urlsr   reservation_refs          r   ResolveExistingReservationArgsr     s    ++33m--224JK( "$(>E"I*k//	V	$$F	
 	O O4467 + 
r   c                    | j                  dd      }t        |       |rt        |       |j                  d      }t	        j
                  d      j                  |       |j                  ddd	gd	d
       |j                  d      }t        ||       |r|j                  dd      }t        |       |r|j                  dd      }	t        |	       yy)z0Adds all flags needed for reservations creation.z:Manage the reservations to be created with the commitment.Tr   z9Manage the reservation to be created with the commitment.)r   F)
positionalz--reservation-typespecificz*The type of the reservation to be created.)hiddenr   r   r   z9Manage the specific SKU reservation properties to create.z7Manage the properties of a shared reservation to create)r   r   z?Manage the properties of a reservation sharing policy to createN)r   r   r   add_argument_groupr
   GetReservationResourceArgAddArgumentr   AddFlagsToSpecificSkuGroupAddFlagsToShareSettingGroup'AddFlagsToReservationSharingPolicyGroup)
rz   r{   r|   support_existing_reservationsr~   reservations_manage_groupsingle_reservation_groupspecific_sku_reservation_groupshare_setting_reservation_group reservation_sharing_policy_groups
             r   ru   ru     s    %..B$ /  78"896IIF J  ))U;GG ''l7 (  $<#N#NF $O $  $&:  33J 	4 	
 $   ?@''?'R'RN (S ($ ,,LM (r   c                 ^    t        j                         g}|D ]  }|j                  |         y)z3Adds flags needed for a reservation sharing policy.N)r   GetReservationSharingPolicyFlagr   groupr6   re   s      r   r   r     s/     779
$ cOOE r   c                    t        j                         t        j                  d      t        j                         t        j                  d      t        j
                         t        j                         t        j                         g}|r#|j                  t        j                                |D ]  }|j                  |         y)z6Adds flags needed for a specific sku zonal allocation.Fr   N)r   GetRequireSpecificAllocationGetVmCountFlagGetMinCpuPlatformGetMachineTyper   r   GetResourcePolicyFlagr]   instance_flagsAddMaintenanceIntervalr   )r   r|   r6   re   s       r   r   r   !  s     446&&6))+&&6'')**,--/
$ KK5578cOOE r   c                     t        j                         t        j                         g}|D ]  }|j                  |         y)z7Adds flags needed for an allocation with share setting.N)r   r   r   r   r   s      r   r   r   4  s;     ,,.((*
$
 cOOE r   )FFFFF)Fr.   )FFFF)>__doc__
__future__r   r   r   r<   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer   compute_completersr   ro   r	   r   ,googlecloudsdk.command_lib.compute.instancesr   /googlecloudsdk.command_lib.compute.reservationsr   r
   $googlecloudsdk.command_lib.util.apisr   r@   r   r   r   r   r>   rC   sorted_REQUIRED_RESOURCESListCommandCompleterr   r,   r0   r7   r9   rK   rU   ra   rf   rr   r   r   rt   r   r   r   rw   rx   ry   rv   r   r   r   r   r   r   ru   r   r   r   r   r   r   <module>r	     s:    > &  '  / O E E P V I :  :&;  \ ):6 % fh/0 !3!H!H /$N4'
  !&',.282	#L'T%P
.,  "'',3Nl&r   