
     g                     v   d Z ddlmZ ddlmZ ddlmZ ddlmZm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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m$Z$  G d dejJ                        Z&y	)zThe BigQuery list CLI command.    )absolute_import)division)print_function)DictOptional)app)flags)overrideN)client_connection)client_data_transfer)client_dataset)
client_job)client_model)client_project)client_reservation)client_routine)client_row_access_policy)client_table)utils)bigquery_command)bq_cached_client)utils_flags)utils_id)bq_error)bq_id_utils)bq_processor_utilsc                   x     e Zd ZdZdedej                  f fdZd	dedee	   fdZ
edeeef   fd       Z xZS )
ListCmdz1ls [(-j|-p|-d)] [-a] [-n <number>] [<identifier>]namefvc                    t         t        |   ||       t        j                  dd dd|       t        j                  dd d|       t        j                  dd	d
d|       t        j
                  dd dd|       t        j
                  dd d|       t        j
                  dd d|       t        j                  dd	dd|       t        j                  dd	dd|       t        j                  dd	dd|       t        j                  dd	d|       t        j                  dd	d|       t        j                  d d d!|       t        j                  d"d	d#|       t        j                  d$d	d%|       t        j                  d&d'd(|       t        j                  d)d	d*|       t        j                  d+d d,|       t        j                  d-d d.d/|       t        j                  d0d	d1|       t        j                  d2d	d3|       t        j                  d4d d5|       t        j                  d6d d7|       t        j                  d8d d9|       t        j                  d:d d;|       t        j                  d<d d=|       t        j                  d>d d?|       t        j                  d@d dA|       t        j                  dBd dC|       t        j                  dDd	dE|       | j                  |       y )FNallzShow all results. For jobs, will show jobs from all users. For datasets, will list hidden datasets.For transfer configs and runs, this flag is redundant and not necessary.a)
short_nameflag_valuesall_jobszDEPRECATED. Use --all instead)r%   jobsFz'Show jobs described by this identifier.jmax_resultszMaximum number to list.nmin_creation_timezDTimestamp in milliseconds. Return jobs created after this timestamp.max_creation_timezETimestamp in milliseconds. Return jobs created before this timestamp.projectszShow all projects.pdatasetsz+Show datasets described by this identifier.dmodelszShow all models.mroutineszShow all routines.row_access_policieszShow all row access policies.transfer_locationz7Location for list transfer config (e.g., "eu" or "us").transfer_configzeShow transfer configurations described by this identifier. This requires setting --transfer_location.transfer_runzList the transfer runs.run_attemptLATESTzFor transfer run, respresents which runs should be pulled. See https://cloud.google.com/bigquery/docs/reference/datatransfer/rest/v1/projects.transferConfigs.runs/list#RunAttempt for detailstransfer_logz%List messages under the run specifiedmessage_typezusage:- messageTypes:INFO For transferlog, represents which messages should be listed. See https://cloud.google.com/bigquery/docs/reference/datatransfer/rest/v1/projects.transferConfigs.runs.transferLogs#MessageSeverity for details.
page_tokenz#Start listing from this page token.kprint_last_tokenz:If true, also print the next page token for the jobs list.print_unreachablezQIf true, also print unreachable locations for the dataset list and the jobs list.filterar  Filters resources based on the filter expression.
For datasets, use a space-separated list of label keys and values in the form "labels.key:value". Datasets must match all provided filter expressions. See https://cloud.google.com/bigquery/docs/filtering-labels#filtering_datasets_using_labels for details
For transfer configurations, the filter expression, in the form "dataSourceIds:value(s)", will show transfer configurations with  the specified dataSourceId. 
For transfer runs, the filter expression, in the form "states:VALUE(s)", will show transfer runs with the specified states. See https://cloud.google.com/bigquery/docs/reference/datatransfer/rest/v1/TransferState for details.
For jobs, the filter expression, in the form "states:VALUE(s)", will show jobs with the specified states. See https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobStatus for details.reservationz9List all reservations for the given project and location.capacity_commitmentzOLists all capacity commitments (e.g. slots) for the given project and location.reservation_assignmentz;List all reservation assignments for given project/locationreservation_groupz?List all reservation groups for the given project and location.reservation_group_nameziReservation group name used as a filter when listing reservation. Used in conjunction with --reservation.parent_job_idzfOnly show jobs which are children of this parent job; if omitted, shows all jobs which have no parent.
connectionz/List all connections for given project/locationmigration_workflowz@List all migration workflows for the given project and location.)	superr   __init__r	   DEFINE_booleanDEFINE_integerDEFINE_stringDEFINE_bool_ProcessCommandRc)selfr   r    	__class__s      $platform/bq/frontend/command_list.pyrJ   zListCmd.__init__,   sq   	'4!$+		
 
 
D9r 
1 
! 
N	 
O	 
E/CR 
5 
%+ 
E/R 
'	 
A	 
	5 
8b 
	  
/	 
	  
- 
D	 
	 
	%* 14 
C	 
	 
 E	 
I	 
 	. 
	/ 
9	 
J	 	2    
identifierreturnc                      t        j                  | j                  | j                  | j                        rt        j                  d      | j                  r|rt        j                  d      | j                  | j                  | _        t        j                  j                         }|rt        j                  ||      }n	 t        j                  ||      }| j                   rKt#        j$                  |t"        j&                  j(                  d|dt+        |      j,                  d       nt/        |t"        j&                  j(                        r	 t        j0                  ||      }t#        j$                  |t+        d      t"        j&                  j2                  t"        j&                  j4                  fd|d	t+        |      j,                  d       | j                  r4t/        |t"        j&                  j4                        r|j7                         }| j8                  }d}d}| j                  rt"        j&                  j:                  }t        j6                  ||      }t#        j$                  |t"        j&                  j2                  d
|dd       t=        j>                  | j@                        }tC        jD                  ||| jF                  | j                  || jH                  | jJ                  || jL                  	      }|jO                  d      }
n| jP                  rt"        j&                  jR                  }t        j0                  ||      }tU        jV                  |jY                         || jF                  |      }	d|	v r|	d   }d|	v 
rt        jZ                  |	       
nj| j\                  rt"        j&                  j^                  }t        j0                  ||      }ta        jb                  |je                         || jF                  || j@                        }	d|	v r|	d   }d|	v 	rt        jZ                  |	       	n| jf                  r	 t"        j&                  jh                  }t        jj                  ||r|ndtl        jn                  jp                  d      }ts        jt                  |jw                         || jF                  | jx                        }	d|	v r|	d   }nt{        d       d|	v rt        jZ                  |	       	n| j~                  r	 t"        j&                  j                  }t        j                  ||tl        jn                  jp                  d      }ts        j                  |jw                         || jF                  | jx                        }	d|	v r|	d   }nt{        d       d|	v rt        jZ                  |	       nB| j                  rWd}	t"        j&                  j                  }t        jj                  ||tl        jn                  jp                  d      }	 	 ts        j                  |jw                         |      }	|	g}|	r%d |	v r!t        |	d          }|d!z  }t{        d"|z         	 	 | j                  -t        j                  tl        j                  j                         ts        j                  |jw                         || jF                  | jx                  | j                  $      }	d%|	v r|	d%   ng }	 |st{        d'       |	rd|	v rt        jZ                  |	       n| j                  rt"        j&                  j                  }t        j                  ||tl        jn                  jp                  d(      }	 t        j                  tl        j                  j                         ts        j                  |jw                         || jF                  | jx                  )      }	d*|	v r|	d*   }nt{        d+       d|	v rt        jZ                  |	       n| j                  r't"        j&                  j                  }t        j6                  |t        j                  ||            }t#        j$                  |t"        j&                  j2                  d-|dd       | j                  t        j                  d.      t        j                  |j                         || j                  | jF                  || j@                  /      }| jF                  r2t        |      d0k(  r$t        |d1   2      }t        jZ                  |       |d3   }n| j                  rt"        j&                  j                  }| j                  }t        j                  ||      }t"        j&                  j                  |4      }t        j                  |j                         ||| jF                  | jx                  | j@                  5      }| jF                  r2t        |      d0k(  r$t        |d1   2      }t        jZ                  |       |d3   }n| j                  rt"        j&                  j                  }t        j                  ||      }t"        j&                  j                  |6      }t        j                  |j                         || j                  | jF                  | jx                  7      }| jF                  r2t        |      d0k(  r$t        |d1   2      }t        jZ                  |       |d3   }n| j                  rt"        j&                  j                  }t        j                  |j                         tl        j                  jp                  tl        jn                  jp                  | jF                  | jx                  8      }d9|v r|d9   }nt{        d:       d|v r3t        jZ                  |       n| j                   rxt"        j&                  j                  }t        j                  ||| jF                  | jx                  ;      }	d<|	v r|	d<   }nt{        d=       d|	v rt        jZ                  |	       n| j                  ratl        jn                  jp                  st        j                  d>      d}| j                  d?d@dAtl        jn                  jp                  iB       n+| j                  r2t        j6                  ||      }t"        j&                  j4                  }n| j                  s|Yt"        j&                  j2                  }| j                  dCd@       t        j                  |j                  | jF                  |D      }nt/        |t"        j&                  j2                        rt"        j&                  j4                  }nGt"        j&                  j(                  }t        j                  |j                  || jF                  |E      }|t"        j&                  j4                  u rg| j                  dFd@       t        j                  |j                  ||| jF                  | j                  || j@                  G      }|jO                  dF      }|s| j                  s| j                  r|J t        j                  ||| |H       yy# t        j                  $ r d}Y w xY w# t        j                  $ r Y w xY w# t|        $ r }
t        j                  d|d|
      d}
~
ww xY w# t|        $ r }
t        j                  d|d|
      d}
~
ww xY w# t        j                  $ r Y t|        $ r }
t        j                  d#|d|
      d}
~
ww xY w# t|        $ r }
t        j                  d&|d|
      d}
~
ww xY w# t|        $ r }
t        j                  d,|d|
      d}
~
ww xY w)Ia  List the objects contained in the named collection.

    List the objects in the named project or dataset. A trailing : or
    . can be used to signify a project or dataset.
     * With -j, show the jobs in the named project.
     * With -p, show all projects.

    Examples:
      bq ls
      bq ls -j proj
      bq ls -j --filter:'states:RUNNING,PENDING' proj
      bq ls -p -n 1000
      bq ls mydataset
      bq ls -a
      bq ls -m mydataset
      bq ls --routines mydataset
      bq ls --row_access_policies mytable (requires whitelisting)
      bq ls --filter labels.color:red
      bq ls --filter 'labels.color:red labels.size:*'
      bq ls --transfer_config --transfer_location='us'
          --filter='dataSourceIds:play,adwords'
      bq ls --transfer_run --filter='states:SUCCEEDED,PENDING'
          --run_attempt='LATEST' projects/p/locations/l/transferConfigs/c
      bq ls --transfer_log --message_type='messageTypes:INFO,ERROR'
          projects/p/locations/l/transferConfigs/c/runs/r
      bq ls --capacity_commitment --project_id=proj --location='us'
      bq ls --reservation --project_id=proj --location='us'
      bq ls --reservation_assignment --project_id=proj --location='us'
      bq ls --reservation_assignment --project_id=proj --location='us'
          <reservation_id>
      bq ls --reservation_group --project_id=proj --location='us'
      bq ls --connection --project_id=proj --location=us
      bq ls --migration_workflow --project_id=proj --location=us
    z.Cannot specify more than one of -j, -p, or -d.z$Cannot specify an identifier with -pN)id_fallbacksrT   zInvalid identifier "z<" for ls, cannot list row access policies on object of type T)is_usage_errorz-" for ls, cannot call list on object of type z)Cannot determine job(s) associated with "")	bqclient	referencer)   	all_usersstate_filterr+   r,   r<   rF   results)model_clientr[   r)   r<   r1   nextPageToken)routines_api_clientr[   r)   r<   filter_expressionr3   - )rW   rT   default_locationdefault_reservation_id)clientr[   	page_sizer<   assignmentsz!No reservation assignments found.z(Failed to list reservation assignments 'z': )rW   rT   re   default_capacity_commitment_idcapacityCommitmentszNo capacity commitments found.z%Failed to list capacity commitments ')rg   r[   sizei   @zBI Engine reservation: %sGBz Failed to list BI reservations ')rg   r[   rh   r<   rD   reservationszFailed to list reservations 'zNo reservations found.)rW   rT   re   default_reservation_group_id)reservation_group_clientr[   rh   r<   reservationGroupszNo reservation groups found.z#Failed to list reservation groups 'z<Cannot determine transfer configuration(s) associated with "z<Need to specify transfer_location for list transfer configs.)transfer_clientr[   locationrh   r<   data_source_ids      )r`   r   )transferConfigName)r)   r<   states)transferRunName)r;   r)   r<   )rg   
project_idrr   r)   r<   connectionszNo connections found.)rZ   table_referencerh   r<   rowAccessPolicieszNo row access policies found.z9Need to specify location for listing migration workflows.migration_workflowslsrr   )command_flags_for_this_resourcer-   )	apiclientr)   r<   )r   r[   r)   r<   r/   )r   rW   r[   r)   list_allr<   rb   )objects_listobjects_typepassed_flagsobjects_metadata){frontend_utilsValidateAtMostOneSelectedr(   r.   r0   r   
UsageErrorr&   r#   r   ClientGetbq_client_utilsGetReferencer   BigqueryErrorr4   r   	typecheckApiClientHelperTableReferencetype__name__
isinstanceGetDatasetReferenceProjectReferenceDatasetReferenceGetProjectReferencer=   JobReferencer   ParseStateFilterExpressionr@   r   ListJobsWithTokenAndUnreachabler)   r+   r,   rF   popr2   ModelReferencer   list_modelsGetModelsApiClientPrintPageTokenr3   RoutineReferencer   ListRoutinesGetRoutinesApiClientrC   ReservationAssignmentReferenceGetReservationReferencebq_flagsLOCATIONvaluer   ListReservationAssignmentsGetReservationApiClientr<   printBaseExceptionrB   CapacityCommitmentReferenceGetCapacityCommitmentReferenceListCapacityCommitmentsrA   ReservationReferenceListBiReservationsintBigqueryNotFoundErrorrE   r   fail_if_not_using_alpha_featureAlphaFeaturesRESERVATION_GROUPSListReservationsrD   ReservationGroupReferenceGetReservationGroupReferenceListReservationGroupsr6   TransferConfigReferencefrontend_id_utilsFormatProjectIdentifierr5   r   list_transfer_configsGetTransferV1ApiClientlendictr7   TransferRunReferencer8   FormatDataTransferIdentifierslist_transfer_runsr:   TransferLogReferencelist_transfer_logsr;   rG   ConnectionReferencer   ListConnectionsGetConnectionV1ApiClient
PROJECT_IDRowAccessPolicyReferencer   &list_row_access_policies_with_granteesrH   DelegateToGcloudAndExitPossiblyDelegateToGcloudAndExitr   list_projectsr   r   list_tablesr   #ListDatasetsWithTokenAndUnreachabler>   r?   PrintObjectsArrayWithMetadata)rP   rT   rg   r[   r<   r^   r   object_typer]   responseesize_in_bytessize_in_gbytestransfer_configsr8   formatted_identifierlist_transfer_runs_resultlist_transfer_log_resultlist_connections_resultss                      rR   RunWithArgszListCmd.RunWithArgs   s   J //GNNKLLvv*NNABB }} }}df$$((*F!..*i#00J
	 


%
%
4
4
 i11	3
 	 
I{::II	J	%99!j)
 
4j))::)):: i11	3
  vv*;..?? //1iJGvv//<<k!55*i 


%
%
6
6<F
H	 (BB4;;Ol#CC&&FF# 22 22**
 !$$Y/g	//>>k!55*i ))002&&	h 
X	8$	H	$%%h/	//@@k!55*i  ,,$99;&& KKh 
x	:&	H	$%%h/		$	$
!11PP#;;%/zS%..44#&	
	 &@@113&&	
 H$]+'
3
4h&

'
'
1
 
	!	!
!11MM#BB!%..44+.	
	 &==113&&	
 !H,23'
0
1h&

'
'
1
 
		h//DDk!99#,,22!$	i
'::335( J'(*hv./-(,>?.
->
?
((477&&99 (88335!(( $ ; ;( +9H*Dh~&"  &'	o1%%h/			//IIk!>>#,,22'*	i
33""55	
 &;;%+%C%C%E&&	
 (*01'
.
/h&

'
'
1
 
		//GGk!55&>>ji 


%
%
6
6 
			'nnJ
 	
 .CC 779))$$++ 
		c"23q8(8(;<
%%j1 #g			//DDk$$k.LL
* --EE1 F i
 #7"I"I

'
'
)

&&__# 
		c";<A(A!(DE
%%j1)!,g			//DDk.LL
* --BB. C i
 "6!H!H

'
'
)
((&&__" 
		c":;q@(@(CD
%%j1(+g	//CCk!2!B!B002((..$$**&&__" 
2	2*=9%&	4	4%%&>?		!	!//HHk)PP#$$__	h 
	(./-.	H	$%%h/		 	 $$nnG
 	
 k
""

+5x7H7H7N7N*O # 
 
!55*i  //@@k	9$//@@k
**:t<,,$$&&g
 
I{::KK	L//@@k//>>k(($$&&	g k11BBB
**:t<'KK$$&&66 KK !$$Z0g$''4+A+A$$$22"+	 ,BO ##  	2 %% 	
	J  
$$AKQO
 	

2  
$$>H!L
 	

. ++  
$$9CQG
 	

*  
$$6@!D
 	

@  
$$<FJ
 	

s   { {5 B8| %B4|; 6A}' B~( B {21{25||	|8|33|8;	}$}}$'~%=~%~  ~%(	1	=88=c                 8    dt        | j                  xs d      iS )z+Parses command flags from the command line.r)   2   )strr)   )rP   s    rR   'ParseCommandFlagsSharedWithAllResourcesz/ListCmd.ParseCommandFlagsSharedWithAllResources  s#     	s4++1r2 rS   ) )r   
__module____qualname__usager   r	   
FlagValuesrJ   r   r   r   r
   r   r   __classcell__)rQ   s   @rR   r   r   )   sc    
A%L3 LE$4$4 L\FC F# FP tCH~  rS   r   )'__doc__
__future__r   r   r   typingr   r   abslr   r	   typing_extensionsr
   r   clientsr   r   r   r   r   r   r   r   r   r   r   r   frontendr   r   r   r   r   r   r   r   r   BigqueryCmdr    rS   rR   <module>r      sn    $ &  % !   &  % ( "    " & " ,   , % % ,   2   $b
** b
rS   