
    ^                     P   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr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  \R*                  rSrSr " S S5      rS\R2                  S\4S jrS\R2                  S\4S jr     S=S\R2                  S\\R:                  R<                     S\\   S\\   S\\   S\\   4S jjr    S>S\R2                  S\\R:                  RB                     S\\   S\\   S\\   S\\   4S jjr"   S>S\R2                  S\R:                  RF                  S \\   S\\   S\\   4
S! jjr$S\R2                  S\R:                  RB                  S"\\   S#\\   S$\\   4
S% jr%S\R2                  SS&S'\&4S( jr'S\R2                  S)\S*\4S+ jr(         S?S\R2                  S,\
" S-S.\\   4/5      S\R:                  RB                  S/\\   S0\\   S1\\   S2\\   S3\\	\\4      S4\\   S5\\   S6\\   S7\\   4S8 jjr)          S@S\R2                  S\S9\S/\\   S0\\   S1\\   S2\\   S3\\	\\4      S4\\   S6\\   S7\\   S5\\   S\\   4S: jjr* SAS\R2                  S\R:                  RB                  S;\&4S< jjr+g)Bz.The BigQuery CLI data transfer client library.    )absolute_import)division)print_functionN)AnyDict
NamedTupleOptional)	discovery)utils)bq_api_utils)bq_error)bq_id_utils)bq_processor_utilsauthorization_codeversion_infoc                       \ rS rSrSr     SS\\   S\\   S\\   S\\   S\\   4
S	 jjr SS
\\	\\
4      S\	\\
4   4S jjr SS
\\	\\4      S\	\\
4   4S jjrS\4S jrS rS\S\	\\4   4S jrSrg)TransferScheduleArgs   z.Arguments to customize data transfer schedule.Nschedule
start_timeend_timedisable_auto_schedulingevent_driven_schedulec                 @    Xl         X l        X0l        X@l        XPl        g N)r   r   r   r   r   )selfr   r   r   r   r   s         +platform/bq/clients/client_data_transfer.py__init__TransferScheduleArgs.__init__   s     M OM#: !6    options_to_copyreturnc                    U R                  5         0 nU R                  (       a   U R                  U R                  5      US'   U$ U R                  (       a  0 US'   U$ 0 US'   U(       a  SU;   a  [	        US   5      US'   U R
                  (       a  U R
                  US   S'   U R                  (       a!  U R                  U R                  5      US   S'   U R                  (       a!  U R                  U R                  5      US   S'   U$ )a-  Returns a dictionary of schedule options v2.

Args:
  options_to_copy: Existing options to copy from.

Returns:
  A dictionary of schedule options v2 expected by the
  bigquery.transfers.create and bigquery.transfers.update API methods.

Raises:
  bq_error.BigqueryError: If shedule options conflict.
eventDrivenSchedulemanualScheduletimeBasedScheduler   	startTimeendTime)	_validate_schedule_optionsr   _process_event_driven_scheduler   dictr   r   _time_or_infitityr   r   r!   optionss      r   to_schedule_options_v2_payload3TransferScheduleArgs.to_schedule_options_v2_payload,   s
    	##%G!!'+'J'J

$
$(g#$* N% 
	%	%"$g" N &(g!"	0OC'+/0(
#$ 
37==#$Z0	484J4JOO5
#$[1 
262H2HMM3
#$Y/ Nr    c                     [        U=(       d    0 5      nU R                  b  U R                  U R                  5      US'   U R                  b  U R                  U R                  5      US'   U R                  US'   U$ )zReturns a dictionary of schedule options.

Args:
  options_to_copy: Existing options to be copied.

Returns:
  A dictionary of schedule options expected by the
  bigquery.transfers.create and bigquery.transfers.update API methods.
r'   r(   disableAutoScheduling)r+   r   r,   r   r   r-   s      r   to_schedule_options_payload0TransferScheduleArgs.to_schedule_options_payloadV   so     ?(b)G"!33DOODgk}} 11$--@gi'+'C'CG#$Nr    time_strc                     U=(       d    S$ )zCReturns None to indicate Inifinity, if time_str is an empty string.N )r   r5   s     r   r,   &TransferScheduleArgs._time_or_infitityo   s    tr    c                     [        U R                  U R                  U R                  /5      nU R                  SLn[        U R                  UU/5      S:  a  [        R                  " S5      eg)zgValidates schedule options.

Raises:
  bq_error.BigqueryError: If the given schedule options conflict.
N   zThe provided scheduling options conflict. Please specify one of no_auto_scheduling, time-based schedule or event-driven schedule.)	anyr   r   r   r   sumr   r   BigqueryError)r   is_time_based_scheduleis_event_driven_schedules      r   r)   /TransferScheduleArgs._validate_schedule_optionss   s~     !	7  $99E(("$
 	
 	

 ""O 
r    c                      [         R                  " U5      nSU;  a  [        R                  " S5      eU$ ! [         a  n[        R                  " S5      UeSnAff = f)a  Processes the event_driven_schedule given in JSON format.

Args:
  event_driven_schedule: The user specified event driven schedule. This
    should be in JSON format given as a string. Ex:
    --event_driven_schedule='{"pubsub_subscription":"subscription"}'.

Returns:
  parsed_event_driven_schedule: The parsed event driven schedule.

Raises:
  bq_error.BigqueryError: If there is an error with the given params.
z9Event driven schedule should be specified in JSON format.Npubsub_subscriptionz<Must specify pubsub_subscription in --event_driven_schedule.)jsonloads	Exceptionr   r=   )r   r   parsed_event_driven_schedulees       r   r*   3TransferScheduleArgs._process_event_driven_schedule   sk    "%)ZZ0E%F"
 $@@""
H  ('  ""
Es   6 
A AA)r   r   r   r   r   )NNNFNr   )__name__
__module____qualname____firstlineno____doc__r	   strboolr   r   r   r/   r3   r,   r)   r*   __static_attributes__r7   r    r   r   r      s    6 !%"& $05-177 3-7 	7
  (~7 &c]7 9=(%d38n5(CH~(V 9=%d38n5CH~2 ,( ( CH~(r    r   transfer_clienttransfer_idc                     U R                  5       R                  5       R                  5       R                  US9R	                  5       $ Nname)projects	locationstransferConfigsgetexecute)rQ   rR   s     r   get_transfer_configr\      s5     y{
sswyr    
identifierc                     U R                  5       R                  5       R                  5       R                  5       R	                  US9R                  5       $ rT   )rW   rX   rY   runsrZ   r[   )rQ   r]   s     r   get_transfer_runr`      s<     y{tv
s
swyr    	referencelocation	page_size
page_tokendata_source_idsc                 8   Sn[         R                  " U[         R                  R                  SS9  Ub$  U[        R
                  :  a  [        R
                  n[        R                  " XX4U5      nU(       Ga  [        R                  " U5        U R                  5       R                  5       R                  5       R                  " S0 UD6R                  5       nUR                  S/ 5      nUb  SU;   a  [        U5      U:  a  [        R                  " UUU[        U5      -
  US   U5      nU(       a  [        R                  " U5        U R                  5       R                  5       R                  5       R                  " S0 UD6R                  5       nUR                  UR                  S/ 5      5        OgSU;   a  [        U5      U:  a  M  [        U5      S:  a  [         R"                  " S5        UR                  S5      (       a  XhR                  S5      4$ U4$ )	a  Return a list of transfer configurations.

Args:
  transfer_client: the transfer client to use.
  reference: The ProjectReference to list transfer configurations for.
  location: The location id, e.g. 'us' or 'eu'.
  page_size: The maximum number of transfer configurations to return.
  page_token: Current page token (optional).
  data_source_ids: The dataSourceIds to display transfer configurations for.

Returns:
  A list of transfer configurations.
Nlist_transfer_configsmethodrY   nextPageTokenr:   z1There are no transfer configurations to be shown.r7   )r   	typecheckApiClientHelperProjectReferencer   MAX_RESULTSPrepareTransferListRequestApplyParametersrW   rX   rY   listr[   rZ   lenextendlogginginfo)	rQ   ra   rb   rc   rd   re   resultsrequestresults	            r   rg   rg      s   * '!!22$
 %111$00i999/' &&w/  "					 	 	 
  jj*B/Gv%#g,*B$??G$?#
 

,
,W
5&&(y{ t   wy  ..OR8
9
' v%#g,*B( 7|allFGzz/""zz/233
r    run_attemptmax_resultsstatesc                    [         R                  " U[         R                  R                  SS9  [	        U5      n[
        R                  " XX4U5      nU R                  5       R                  5       R                  5       R                  5       R                  " S0 UD6R                  5       nUR                  S/ 5      nUGb  SU;   a  [        U5      U:  a  UR                  S5      nU[        U5      -  n[
        R                  " XX4U5      nU R                  5       R                  5       R                  5       R                  5       R                  " S0 UD6R                  5       nUR                  UR                  S/ 5      5        SU;   a  [        U5      U:  a  M  UR                  S5      (       a  XR                  S5      4$ U4$ )a  Return a list of transfer runs.

Args:
  transfer_client: the transfer client to use.
  reference: The ProjectReference to list transfer runs for.
  run_attempt: Which runs should be pulled. The default value is 'LATEST',
    which only returns the latest run per day. To return all runs, please
    specify 'RUN_ATTEMPT_UNSPECIFIED'.
  max_results: The maximum number of transfer runs to return (optional).
  page_token: Current page token (optional).
  states: States to filter transfer runs (optional).

Returns:
  A list of transfer runs.
list_transfer_runsrh   transferRunsrj   r7   )r   rk   rl   TransferConfigReferencerN   r   PrepareTransferRunListRequestrW   rX   rY   r_   rq   r[   rZ   rr   rs   )	rQ   ra   ry   rz   rd   r{   rw   responsetransfer_runss	            r   r}   r}     s   . !!99!
 )n)<<kv'  y{tvt	  	
 wy  ,,~r2-
X
%#m*<{*J<<0jS''k"@@
+6g 
"
"
$9;?464	  	
 79  8<<;< X
%#m*<{*J ||O$$\\/:;;
	r    message_typec                 L   [        U5      n[        R                  " UUUUS9nU R                  5       R	                  5       R                  5       R                  5       R                  5       R                  " S0 UD6R                  5       nUR                  S/ 5      nUb  SU;   a  [        U5      U:  a  US   nU[        U5      -  n[        R                  " UUUUS9nU R                  5       R	                  5       R                  5       R                  5       R                  5       R                  " S0 UD6R                  5       nUR                  UR                  S/ 5      5        SU;   a  [        U5      U:  a  M  UR                  S5      (       a  XvR                  S5      4$ U4$ )a\  Return a list of transfer run logs.

Args:
  transfer_client: the transfer client to use.
  reference: The ProjectReference to list transfer run logs for.
  message_type: Message types to return.
  max_results: The maximum number of transfer run logs to return.
  page_token: Current page token (optional).

Returns:
  A list of transfer run logs.
)rz   rd   r   transferMessagesrj   r7   )rN   r   PrepareListTransferLogRequestrW   rX   rY   r_   transferLogsrq   r[   rZ   rr   rs   )rQ   ra   r   rz   rd   rw   r   transfer_logss           r   list_transfer_logsr   A  s   & )n)<<	'  y{tv|~t 
  wy  ,,126-
X
%#m*<{*JO,jS''k"@@
!#	g 
"
"
$9;?46<>4 
  79  8<<(:B?@% X
%#m*<{*J& \\/""<<899
	r    r   r   run_timec                 L   [         R                  " U[         R                  R                  SS9  [	        U5      nU(       a  SU0nOSX#S.0nU R                  5       R                  5       R                  5       nUR                  XVS9R                  5       nUR                  S5      $ )aR  Starts manual transfer runs.

Args:
  transfer_client: the transfer client to use.
  reference: Transfer configuration name for the run.
  start_time: Start time of the range of transfer runs.
  end_time: End time of the range of transfer runs.
  run_time: Specific time for a transfer run.

Returns:
  The list of started transfer runs.
start_manual_transfer_runsrh   requestedRunTimerequestedTimeRange)r'   r(   )parentbodyr_   )r   rk   rl   r   rN   rW   rX   rY   startManualRunsr[   rZ   )	rQ   ra   r   r   r   r   r   configs_requestr   s	            r   r   r   ~  s    & !!99)
 y>&)D 	JLD $,,.88:JJL/,,F,FNNP(	f	r    z3bq_id_utils.ApiClientHelper.TransferConfigReferencer"   c                 8   [         R                  " U[         R                  R                  SS9   U R	                  5       R                  5       R                  5       R                  UR                  S9R                  5         g! [        R                   a     gf = f)z$Returns true if the transfer exists.transfer_existsrh   rU   TF)r   rk   rl   r   rW   rX   rY   rZ   transferConfigNamer[   r   BigqueryNotFoundError)rQ   ra   s     r   r   r     s     !!99
((*::<@@)) A gi		'	' s   AB BBproject_referencedata_source_idc                     US-   U-   nU R                  5       R                  5       R                  5       R                  US9R	                  5       $ )Nz/locations/-/dataSources/rU   )rW   rX   dataSourcesrZ   r[   )rQ   r   r   data_source_retrievals       r   _fetch_data_sourcer     sL     55F 
  y{{}
s%s&wyr    id_fallbacksIDS
project_idtarget_datasetdisplay_namerefresh_window_daysparams	auth_infoservice_account_namedestination_kms_keynotification_pubsub_topicschedule_argsc           
         [         R                  " U[         R                  R                  SS9  S[        R
                  " US9R                  -   n[        XR                  5      n/ n0 nUS   US'   U(       a  X?S'   UR                  S5        U(       a  UR                  S5        XOS	'   U(       a'  [        R                  " Xo5      nUR                  S
5        U(       a<  [        XUS   5      n[        R                  " UUUUS   5      nUR                  S5        U(       a4  UR                  UR                  S5      5      US'   UR                  S5        U
(       a  XS'   UR                  S5        Ub  [         U;   a  UR                  [         5        Ub  ["        U;   a  UR                  ["        5        U(       a  UR                  S5        U	(       a  SSU	00US'   UR                  S5         U R%                  5       R'                  5       R)                  5       R+                  UUR                  SR-                  U5      Uc  SOUR                  [         5      Uc  SOUR                  ["        5      USS9R/                  5         g! [0        R2                   aX  nU(       aG  S[5        U5      ;   a3  [        R6                  " XS9n[0        R8                  " SU< 3SS0/ 5      Ue SnAg SnAgSnAff = f)a  Updates a transfer config.

Args:
  transfer_client: the transfer client to use.
  id_fallbacks: IDs to use when they have not been explicitly specified.
  reference: the TransferConfigReference to update.
  target_dataset: Optional updated target dataset.
  display_name: Optional change to the display name.
  refresh_window_days: Optional update to the refresh window days. Some data
    sources do not support this.
  params: Optional parameters to update.
  auth_info: A dict contains authorization info which can be either an
    authorization_code or a version_info that the user input if they want to
    update credentials.
  service_account_name: The service account that the user could act as and
    used as the credential to create transfer runs from the transfer config.
  destination_kms_key: Optional KMS key for encryption.
  notification_pubsub_topic: The Pub/Sub topic where notifications will be
    sent after transfer runs associated with this transfer config finish.
  schedule_args: Optional parameters to customize data transfer schedule.

Raises:
  BigqueryTypeError: if reference is not a TransferConfigReference.
  bq_error.BigqueryError: required field not given.
update_transfer_configrh   z	projects/)r   dataSourceIddestinationDatasetIdz&transfer_config.destination_dataset_idztransfer_config.display_namedisplayNameztransfer_config.paramsz(transfer_config.data_refresh_window_daysscheduleOptionsV2z!transfer_config.scheduleOptionsV2r   z)transfer_config.notification_pubsub_topicNr   kms_key_namevalueencryption_configurationz%encryption_configuration.kms_key_name,2)r   rV   
updateMaskauthorizationCodeversionInfoserviceAccountNamex__xgafvzNot found: Dataset)r   r]   Not found: reasonnotFound)r   rk   rl   r   bq_client_utilsGetProjectReference	projectIdr\   r   appendr   ProcessParamsFlagr   ProcessRefreshWindowDaysFlagr/   rZ   AUTHORIZATION_CODEVERSION_INFOrW   rX   rY   patchjoinr[   r   BigqueryInterfaceErrorrN   GetDatasetReferencer   )rQ   r   ra   r   r   r   r   r   r   r   r   r   r   current_configupdate_maskupdate_itemsdata_source_inforG   dataset_references                      r   r   r     s   Z !!99%
 "))|DNN '33. +,!/!?,~+9'(?@56".%77ML/0 )N>,J &BB~&	L AB4423	
 $%
 :;0I,-BC1Y>)*|y8|$-."560L+, >?((*::<BB))88K(%D9==9K+L%-D9==3N/ C 
 gi		(	( .#a&8)==# **.
08Z2H"	 9~s   /BJ K2A	K--K2data_sourcec                    0 nU(       a  X=S'   U(       a  XMS'   O[         R                  " S5      eX-S'   U(       a#  [        XU5      n[        R                  " X^X5      nU(       a  [        R
                  " Xm5      nO[         R                  " S5      eU(       a	  US-   U-   nOUS-   nU
(       a  U
R                  5       US'   U	(       a  XS	'   U(       a	  S
SU00US'   U R                  5       R                  5       R                  5       R                  UUUc  SOUR                  [        5      Uc  SOUR                  [        5      US9R                  5       nUS   $ )a  Create a transfer config corresponding to TransferConfigReference.

Args:
  transfer_client: the transfer client to use.
  reference: the TransferConfigReference to create.
  data_source: The data source for the transfer config.
  target_dataset: The dataset where the new transfer config will exist.
  display_name: A display name for the transfer config.
  refresh_window_days: Refresh window days for the transfer config.
  params: Parameters for the created transfer config. The parameters should be
    in JSON format given as a string. Ex: --params="{'param':'value'}". The
    params should be the required values needed for each data source and will
    vary.
  auth_info: A dict contains authorization info which can be either an
    authorization_code or a version_info that the user input if they need
    credentials.
  service_account_name: The service account that the user could act as and
    used as the credential to create transfer runs from the transfer config.
  notification_pubsub_topic: The Pub/Sub topic where notifications will be
    sent after transfer runs associated with this transfer config finish.
  schedule_args: Optional parameters to customize data transfer schedule.
  destination_kms_key: Optional KMS key for encryption.
  location: The location where the new transfer config will run.

Raises:
  BigqueryNotFoundError: if a requested item is not found.
  bq_error.BigqueryError: if a required field isn't provided.

Returns:
  The generated transfer configuration name.
r   r   z A display name must be provided.r   zParameters must be provided.z/locations/z/locations/-r   r   r   r   r   N)r   r   r   r   r   rV   )r   r=   r   r   r   r   r/   rW   rX   rY   createrZ   r   r   r[   )rQ   ra   r   r   r   r   r   r   r   r   r   r   rb   create_itemsr   r   new_transfer_configs                    r   create_transfer_configr   P  sn   \ , +9'(".

 
 !C
DD!,~ )K &BB|L %77ML

 
 !?
@@&1F 'F446 $% 0I,-"560L+,
  y{v'dY]];M-N  }}\*1  
 wy $ 
V	$$r    ignore_not_foundc                    [         R                  " U[         R                  R                  SS9   U R	                  5       R                  5       R                  5       R                  UR                  S9R                  5         g! [        R                   a0  nU(       d  [        R                  " SU< 3SS0/ 5      Ue SnAgSnAff = f)a}  Deletes TransferConfigReference reference.

Args:
  transfer_client: the transfer client to use.
  reference: the TransferConfigReference to delete.
  ignore_not_found: Whether to ignore "not found" errors.

Raises:
  BigqueryTypeError: if reference is not a TransferConfigReference.
  bq_error.BigqueryNotFoundError: if reference does not exist and
    ignore_not_found is False.
delete_transfer_configrh   rU   r   r   r   N)r   rk   rl   r   rW   rX   rY   deleter   r[   r   r   )rQ   ra   r   rG   s       r   r   r     s    $ !!99%
((*::<CC)) D gi		'	' **&
(8Z*@" s   AB C&CC)NNNNN)NNN)	NNNNNNNNN)
NNNNNNNNNN)F),rM   
__future__r   r   r   rC   rt   typingr   r   r   r	   googleapiclientr
   clientsr   r   r   r   r   r   Servicer   r   r   ResourcerN   r\   r`   rl   rm   intrg   r   r}   TransferRunReferencer   r   rO   r   r   r   r   r   r7   r    r   <module>r      s   4 &  %   2 2 % ,    $


 * I( I(X);); # i&8&8 c  IM"# $%)D''D33DDED smD }	D
 D c]DV "& $ ;'';33KKL; #; #	;
 ; SM;B #'!% $:'':**??: 3-: #	:
 :z$''$**BB$ $ sm	$
 sm$N''D 
('' 6 %)"&)- *.*.)-/348#B''B8C=)	
B **BBB SMB 3-B "#B SMB S#X'B #3-B "#B   (}!B" 01#BR %)"&)- *.*./348)-"q%''q%q% q% SM	q%
 3-q% "#q% SMq% S#X'q% #3-q%  (}q% 01q% "#q% smq%n #''**BB r    