
    5R                     f   S r SSKJr  SSKJr  SSKJr  SSKJr  SSK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rSSKJr  SrSrSrSrSrSrSr " S S\R6                  5      r " S S\R6                  5      r " S S\R6                  5      r " S S\R6                  5      r " S S\R6                  5      r S r!S  r"S! r#S" r$S# r%S$ r&S% r'S& r(S' r)S( r*S) r+S* r,S+ r-S, r.S- r/S. r0S/ r1S0 r2S1 r3S2 r4S3 r5S4 r6S5 r7S6 r8S7 r9S8 r:S9 r;S: r<S; r=S< r>S= r?S> r@S? rAS@ rBSA rCSCSB jrDg)Dz>A library that is used to support Cloud Pub/Sub Lite commands.    )absolute_import)division)print_function)unicode_literalsN)apis)util)
exceptions)log)
properties)
console_io)urlparsez	projects/z
locations/zreservations/ztopics/zsubscriptions/
pubsublitev1c                       \ rS rSrSrSrg)UnexpectedResourceField+   z,Error for having and unknown resource field. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       2lib/googlecloudsdk/command_lib/pubsub/lite_util.pyr   r   +       4r   r   c                       \ rS rSrSrSrg)InvalidPythonVersion/   z$Error for an invalid python version.r   Nr   r   r   r   r   r   /   s    ,r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )NoGrpcInstalled3   z8Error that occurs when the grpc module is not installed.c                 ,   > [         [        U ]  S5        g )NzPlease ensure that the gRPC module is installed and the environment is correctly configured. Run `sudo pip3 install grpcio` and set the environment variable CLOUDSDK_PYTHON_SITEPACKAGES=1.)superr"   __init__)self	__class__s    r   r&   NoGrpcInstalled.__init__6   s    	/4)	?@r   r   )r   r   r   r   r   r&   r   __classcell__)r(   s   @r   r"   r"   3   s    @@ @r   r"   c                       \ rS rSrSrSrg)InvalidSeekTarget=   z,Error for specifying an invalid seek target.r   Nr   r   r   r   r,   r,   =   r   r   r,   c                       \ rS rSrSrSrg)InvalidResourcePathA   z>Error for specifying an invalid fully qualified resource path.r   Nr   r   r   r   r/   r/   A   s    Fr   r/   c                  @    [         R                  " [        [        5      $ )z*Returns the Pub/Sub Lite v1 client module.)r   GetClientInstancePUBSUBLITE_API_NAMEPUBSUBLITE_API_VERSIONr   r   r   PubsubLiteClientr5   E       			 35K	LLr   c                  @    [         R                  " [        [        5      $ )z,Returns the Pub/Sub Lite v1 messages module.)r   GetMessagesModuler3   r4   r   r   r   PubsubLiteMessagesr9   J   r6   r   c                 Z    [         R                  " [        U R                  5      5      S-   $ )zCConvert Duration object to total seconds for backend compatibility.s)six	text_typeinttotal_seconds)durations    r   DurationToSecondsrA   O   s!    	s8112	3c	99r   c                 L    U R                  S5      nSR                  USS 5      $ )a  Returns the region from a location string.

Args:
  location: A str of the form `<region>-<zone>` or `<region>`, such as
    `us-central1-a` or `us-central1`. Any other form will cause undefined
    behavior.

Returns:
  The str region. Example: `us-central1`.
-N   )splitjoin)locationsplitss     r   DeriveRegionFromLocationrI   T   s'     >>#&	&!*	r   c                     [        U 5      nUR                  R                  S5      n[        U5      S:  a  US   S-   US   -   $ g)a6  Returns the region from a endpoint string.

Args:
  endpoint: A str of the form `https://<region-><environment->base.url.com/`.
    Example `https://us-central-base.url.com/`,
    `https://us-central-autopush-base.url.com/`, or `https://base.url.com/`.

Returns:
  The str region if it exists, otherwise None.
rC   rD   r      N)r   netlocrE   len)endpointparseddash_splitss      r   DeriveRegionFromEndpointrQ   c   sJ     H&##C(+q>C+a.00r   c                 P    UR                  S5      nUS   nUS-   U -   S-   US   -   $ )zTReturns a new endpoint string with the defined `region` prefixed to the netlocation.z://r   rC   rK   )rE   )regionurl	url_parts
url_schemes       r   CreateRegionalEndpointrW   v   s7    ii)|*	e	f	$s	*Yq\	99r   c                 T    [        U 5      nU(       a  U R                  US-   S5      n U $ )zCReturns a new endpoint string stripped of the region if one exists.rC    )rQ   replace)rN   rS   s     r   RemoveRegionFromEndpointr[   }   s*    #H-&b1H	/r   c                     SnSn[        U S5      (       a  U R                  nSnX4$ [        U S5      (       a  U R                  nSnX4$ [        U S5      (       a  U R                  nSnX4$ [	        S5      e)a  Returns a tuple of the resource and resource name from the `request`.

Args:
  request: A Request object instance.

Returns:
  A tuple of the resource string path and the resource name.

Raises:
  UnexpectedResourceField: The `request` had a unsupported resource.
rY   parentnamesubscriptionz3The resource specified for this command is unknown!)hasattrr]   r^   r_   r   )requestresourceresource_names      r   GetResourceInford      s     (-Wh~~HM 
	   w||HM 
	   w''##H"M
 
	   "=? ?r   c                     SSK Jn  [        U R                  S5      5      S:X  a  UR                  R                  U 5      $ UR                  R                  U 5      $ )Nr   )typesrC      )google.cloud.pubsubliterf   rM   rE   	CloudZoneparseCloudRegion)location_idrf   s     r   LocationToZoneOrRegionrm      sI    +		3	 A%??  --"";//r   c                 |    X R                  [        5      [        [        5      -   S nUR                  S5      S   nU$ )z,Returns the location from a resource string.N/r   )indexLOCATIONS_RESOURCE_PATHrM   rE   )rb   rG   s     r   DeriveLocationFromResourcerr      sA    nn%<=123 4 5(^^C #(	/r   c                 |    X R                  [        5      [        [        5      -   S nUR                  S5      S   nU$ )z+Returns the project from a resource string.Nro   r   )rp   PROJECTS_RESOURCE_PATHrM   rE   )rb   projects     r   DeriveProjectFromResourcerv      sA    ^^$:;/01 2 3'MM#q!'	.r   c                 f    [        U 5      u  pXR                  [        5      S n[        XU5        U $ )z;Returns an updated `request` with the resource path parsed.N)rd   rindexrt   setattr)ra   rb   rc   new_resources       r   ParseResourcer{      s3    +G4(//*@ABC,	',/	.r   c                    [        U 5      u  p[        [        U5      5      n[        R                  " [
        [        5      n[        U5      n[        X45      n[        R                  R                  R                  R                  U5        g)z<Sets the pubsublite endpoint override to include the region.N)rd   rI   rr   r   GetEffectiveApiEndpointr3   r4   r[   rW   r   VALUESapi_endpoint_overridesr   Set)ra   rb   _rS   rN   regional_endpoints         r   OverrideEndpointWithRegionr      sm    (+(#$>x$HI&))*=*@B( &h/(,V>**5599:KLr   c                     [         R                  " SS5      n[         R                  " SS5      nUR                  U S9nUR                  R                  U5      nUR                  $ )zBReturns the Cloud project number associated with the `project_id`.cloudresourcemanagerr   )	projectId)r   r8   r2   &CloudresourcemanagerProjectsGetRequestprojectsGetprojectNumber)
project_idcrm_message_moduleresource_managerreqru   s        r   ProjectIdToProjectNumberr      se    --.DdK++,BDIAA 	B 	#%%))#.'			r   c           
          [        U 5      u  p[        U5      n[        U5      n[        XUR	                  U[
        R                  " U5      5      5        U $ )z;Returns an updated `request` with the Cloud project number.)rd   rv   r   ry   rZ   r<   r=   )ra   rb   rc   r   project_numbers        r    OverrideProjectIdToProjectNumberr      sN    +G4((2*+J7.	'


:s}}^'D
EG 
.r   c                 L    A A[        U5      n[        U5      n[        U5        U$ )zCReturns an updated `request` with values for a valid Admin request.)r{   r   r   resource_refargsra   s      r   UpdateAdminRequestr      s*     D'"',W5'W%	.r   c                     A A[        U5      nUR                  R                  =R                  S-  sl        [	        U5        U$ )zAUpdates a CommitCursorRequest by adding 1 to the provided offset.rK   )r{   commitCursorRequestcursoroffsetr   r   s      r   UpdateCommitCursorRequestr      s?     D'"' 
$$++q0+W%	.r   c                 \    U R                   c  g[        U R                   R                  5      $ )z0Returns whether the topic has a reservation set.F)reservationConfigboolthroughputReservationtopics    r   _HasReservationr   	  s(    
$	e%%;;	<<r   c                 6   A AUR                   n[        U5      (       dz  UR                  c  0 Ul        UR                  R                  c  0 UR                  l        UR                  R                  nUR                  c  SUl        UR
                  c  SUl        U$ )zHAdds the default values for topic throughput fields with no reservation.      )r   r   partitionConfigcapacitypublishMibPerSecsubscribeMibPerSec)r   r   ra   r   r   s        r   "AddTopicDefaultsWithoutReservationr     s     D
--%			$ e%%-')e$$$--H  ("#h""*$%h!	.r   c                 @   A AUR                   n[        U5      (       d  U$ [        U5      u  pE[        U5      n[	        [        U5      5      nUR                  R                  nSR                  [        U[        U[        U5      UR                   R                  l        U$ )zEReturns an updated `request` with a resource path on the reservation.{}{}/{}{}/{}{})r   r   rd   rv   rI   rr   r   r   formatrt   rq   RESERVATIONS_RESOURCE_PATH)	r   r   ra   r   rb   r   ru   rS   reservations	            r   AddTopicReservationResourcer   $  s     D
--%			N(+(%h/'#$>x$HI&''==+
 '+BF
$k3 
--!!7
 
.r   c                     A A[        U5      u  p4SR                  U[        UR                  R                  5      UR                  l        U$ )z?Returns an updated `request` with a resource path on the topic.z{}/{}{})rd   r   TOPICS_RESOURCE_PATHr_   r   )r   r   ra   rb   r   s        r   AddSubscriptionTopicResourcer   9  sJ     D(+((//:N070D0D0J0J L' 
.r   c                 f    A SU;  d  UR                   (       d  U$ [        R                  " SSSS9  U$ )z:Prompts to confirm an update to a topic's partition count.
partitionszWarning: The number of partitions in a topic can be increased but not decreased. Additionally message order is not guaranteed across a topic resize. See https://cloud.google.com/pubsub/lite/docs/topics#scaling_capacity for more detailsT)messagedefaultcancel_on_no)r   r   PromptContinuer   s      r   ConfirmPartitionsUpdater   E  s:    T__N
  
.r   c                 R    A [        US5      (       a  UR                  S:H  Ul        U$ )Nstarting_offsetend)r`   r   skipBacklogr   s      r   UpdateSkipBacklogFieldr   V  s+    T$%%//58G	.r   c                 @    U R                   =(       d    U R                  $ )z"Returns the raw location argument.)rG   zone)r   s    r   GetLocationValuer   `  s    		#$))#r   c                     [        U 5      n[        U;  a  U$ [        U5      nU(       d  [        SR	                  U5      5      eU$ )zReturns the resource location (zone or region) extracted from arguments.

Args:
  args: argparse.Namespace, the parsed commandline arguments.

Raises:
  InvalidResourcePath: if the location component in a fully qualified path is
  invalid.
zGThe location component in the specified location path is invalid: `{}`.)r   rq   rr   r/   r   )r   rG   parsed_locations      r   GetLocationr   e  sJ     d#(H,O.x8/	
Q		  
r   c                 2   [        U 5      nUR                  [        5      (       dE  U R                  =(       d2    [        R
                  R                  R                  R                  5       $ [        U5      nU(       d  [        SR                  U5      5      eU$ )zReturns the project from either arguments or attributes.

Args:
  args: argparse.Namespace, the parsed commandline arguments.

Raises:
  InvalidResourcePath: if the project component in a fully qualified path is
  invalid.
zFThe project component in the specified location path is invalid: `{}`.)r   
startswithrt   ru   r   r~   core	GetOrFailrv   r/   r   )r   rG   parsed_projects      r   
GetProjectr   {  sy     d#(			3	4	4<<E:,,1199CCEE,X6.	
P		  
r   c                     U R                   S:X  a   UR                  R                  R                  $ UR                  R                  R                  $ )z4Returns the DeliveryRequirement enum from arguments.zdeliver-after-stored)delivery_requirementDeliveryConfig"DeliveryRequirementValueValuesEnumDELIVER_AFTER_STOREDDELIVER_IMMEDIATELYr   psls     r   GetDeliveryRequirementr     sA    	"88@@UUU				>	>	R	RRr   c                     U R                   S:X  a   UR                  R                  R                  $ UR                  R                  R                  $ )z4Returns the export DesiredState enum from arguments.paused)export_desired_stateExportConfigDesiredStateValueValuesEnumPAUSEDACTIVEr   s     r   GetDesiredExportStater     s@    	(*77>>>				5	5	<	<<r   c                 p   U R                   (       a:  UR                  UR                  [        R                  " U R                   5      S9S9$ U R
                  (       a:  UR                  UR                  [        R                  " U R
                  5      S9S9$ U R                  (       a  U R                  S:X  a-  UR                  UR                  R                  R                  S9$ U R                  S:X  a-  UR                  UR                  R                  R                  S9$ [        S5      e[        S5      e)	z1Returns a SeekSubscriptionRequest from arguments.)publishTime)
timeTarget)	eventTime	beginning)namedTargetr   z@Invalid starting offset value! Must be one of: [beginning, end].zSeek target must be specified!)publish_timeSeekSubscriptionRequest
TimeTargetr   FormatSeekTime
event_timer   NamedTargetValueValuesEnumTAILHEADr,   r   s     r   GetSeekRequestr     s0   	&&>>++D,=,=> " @ ' A A &&>>))$//: " < ' = = {*((S5P5P*D*DTT ) K K				&((S5P5P*D*DTT ) K K 
LN N <
==r   c                    U R                   (       aV  U R                   nUR                  [        5      (       d  SR                  [        U[        U5      nUR                  US9Ul        U R                  (       aO  U R                  nUR                  [        5      (       d"  SR                  [        U[        U[        U5      nXTl	        gg)z8Sets fully qualified resource paths for an ExportConfig.	{}{}/{}{}r   r   N)
export_pubsub_topicr   rt   r   r   PubSubConfigpubsubConfigexport_dead_letter_topicrq   deadLetterTopic)r   r   ru   rG   export_configr   s         r   SetExportConfigResourcesr     s    	$$E233!!"8'"6?e!$!1!1!1!>M	""))E233&&'=w'>';UDe %*! #r   c                     U R                   c  g[        X5      nU(       a   UR                  R                  R                  nUR                  US9n[        XX#U5        U$ )z'Returns an ExportConfig from arguments.NdesiredState)r   r   r   r   r   r   )r   r   ru   rG   requires_seekdesired_stater   s          r   GetExportConfigr     sZ    	%'2-$$@@GGM""">-4gG	r   c                    [        5       n[        U5      n[        U5      n[        R                  " [        U5      5      nUR                  =(       d    UR                  nUR                  SR                  [        U[        U5      UR                  UR                  UR                  [        X5      S9[!        XXSU5      S9UR"                  S9n[%        U5        ['        XU5        U(       d  [)        XU5        [+        5       nUR,                  R/                  U5      n	U(       a>  UR1                  U	R2                  [5        X5      S9n
UR,                  R7                  U
5        U(       a  UR"                  R8                  (       a~  UR:                  S:X  an  UR=                  U	R2                  SUR                  UR?                  UR>                  R@                  RB                  S9S	9S
9nUR,                  RE                  U5      n	U	$ )a  Issues a CreateSubscriptionRequest and potentially other requests.

Args:
  resource_ref: resources.Resource, the resource reference for the resource
    being operated on.
  args: argparse.Namespace, the parsed commandline arguments.

Returns:
  The created Pub/Sub Lite Subscription.
r   )deliveryRequirement)r   deliveryConfigexportConfig)r]   r_   subscriptionId)r^   seekSubscriptionRequestactivezexport_config.desired_stater   )r   )r^   
updateMaskr_   )#r9   r   r   r<   r=   r   r   r   :PubsubliteAdminProjectsLocationsSubscriptionsCreateRequestr   rt   rq   Subscriptionr   r   r   r   r_   r   r   r   r5   &admin_projects_locations_subscriptionsCreate8PubsubliteAdminProjectsLocationsSubscriptionsSeekRequestr^   r   Seekr   r   9PubsubliteAdminProjectsLocationsSubscriptionsPatchRequestr   r   r   Patch)r   r   r   rG   r   r   r   create_requestclientresponseseek_requestupdate_requests               r    ExecuteCreateSubscriptionRequestr    s    	#($*==!9*!EF.##6t- QQ  !7!8(D##

++"8"C , E&t.'46	 $ 7 && R 	(. ^,|>B	<~>&::AA( OO]]N44M P OL
1166|D ~22??DD]D]aiDiRR]]0%%))s7G7G+F+Fvv * O & P S QN <<BBH 
/r   c                     A UR                   R                  c  U$ [        U5      u  p4[        U5      n[	        U5      n[        5       n[        XXVUR                   R                  5        U$ )a9  Sets export resource paths for an UpdateSubscriptionRequest.

Args:
  resource_ref: resources.Resource, the resource reference for the resource
    being operated on.
  args: argparse.Namespace, the parsed commandline arguments.
  request: An UpdateSubscriptionRequest.

Returns:
  The UpdateSubscriptionRequest.
)r_   r   rd   rv   rr   r9   r   )r   r   ra   rb   r   ru   rG   r   s           r   AddExportResourcesr    sf     &&.N(+(%h/''1(#4g"//<<>	.r   c                 h    A [        5       n[        X5      Ul        [        R                  " S5        U$ )z.Sets the target for a SeekSubscriptionRequest.zThe seek operation will complete once subscribers react to the seek. If subscribers are offline, `pubsub lite-operations describe` can be used to check the operation status later.)r9   r   r  r
   warning)r   r   ra   r   s       r   SetSeekTargetr  .  s4     #$24$='!++23 
.r   c                    A UR                   (       a  U$ UR                  (       a0  SR                  UR                  [        UR                  5      Ul         UR
                  (       a\  UR                   (       a  U=R                   S-  sl         OSUl         U=R                   SR                  UR
                  5      -  sl         U$ )z/Updates the filter for a ListOperationsRequest.ztarget={}/{}{}z AND rY   zdone={})filterr_   r   r^   SUBSCRIPTIONS_RESOURCE_PATHdoner   s      r   UpdateListOperationsFilterr  <  s      
[[N 
 &,,W\\-H-1->->@GN 
YY~~nnngnNNi&&tyy11N	.r   c                     [         R                  R                  S:  d<  [         R                  R                  S:X  a9  [         R                  R                  S:  a  [	        SR                  U 5      5      egg)zVerifies that the python version is 3.6+.

Args:
  cmd: The string command that requires python 3.6+.

Raises:
  InvalidPythonVersion: if the python version is not 3.6+.
rg      znThe `{}` command requires python 3.6 or greater. Please update the
        python version to use this command.N)sysversion_infomajorminorr   r   )cmds    r   RequirePython36r#  V  se     	aC$4$4$:$:a$?$'$4$4$:$:Q$>
	//5vc{< < %? %@r   )gcloud)Er   
__future__r   r   r   r   r  googlecloudsdk.api_lib.utilr   !googlecloudsdk.command_lib.pubsubr   googlecloudsdk.corer	   r
   r   googlecloudsdk.core.consoler   r<   six.moves.urllib.parser   rt   rq   r   r   r  r3   r4   Errorr   r   r"   r,   r/   r5   r9   rA   rI   rQ   rW   r[   rd   rm   rr   rv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r#  r   r   r   <module>r,     su   E &  % ' 
 , 2 * # * 2 
 + % & ,   . "  5j.. 5-:++ -@j&& @5
(( 5G*** GM
M
:
&:!>0M"		=(*	"$
,,S=>4*$6r64<r   