
    k5                        S 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JrJ	r	J
r
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  \R(                  rSrSrSrSS/rSrSrS\R6                  S\R8                  R:                  4S jr      S2S\R6                  S\S\S\S\S\\   S\\   S\\   S\\   S\\   S\\   4S  jjr        S3S\R6                  S\R8                  R:                  S\\   S\\   S\\   S\\   S\\   S\\   S\\   4S! jjr!S"\S#\\\4   S$\	\   4S% jr"S&\S'\S$\4S( jr#S&\S)\\\4   S$\	\   4S* jr$S\R6                  S\R8                  R:                  4S+ jr%S\R6                  S\S\S,\&S-\\   4
S. jr'S\R6                  S\R8                  R:                  S/\4S0 jr(S\R6                  S\R8                  R:                  4S1 jr)g)4z+The BigQuery CLI connection client library.    )absolute_import)division)print_functionN)AnyDictListMappingOptional)	discovery)bq_api_utils)bq_error)bq_id_utils)bq_processor_utilsauthorization_codeversion_infoz[0-9A-Za-z_]+zconfiguration.parametersz'configuration.authentication.parametersz'configuration.authentication.profile_idzconfiguration.authenticationclient	referencec                     U R                  5       R                  5       R                  5       R                  UR	                  5       S9R                  5       $ )zGets connection with the given connection reference.

Arguments:
  client: the client used to make the request.
  reference: Connection to get.

Returns:
  Connection object with the given id.
name)projects	locationsconnectionsgetpathexecuter   r   s     (platform/bq/clients/client_connection.pyGetConnectionr   &   s;     ooy{{}
s	 s!wy    
project_idlocationconnection_type
propertiesconnection_credentialdisplay_namedescriptionconnection_idkms_key_nameconnector_configurationc                 V   0 nU(       a  XkS'   U(       a  X{S'   U	(       a  XS'   [         R                  R                  U5      nU(       a^  [         R                  " U5      X'   U(       a>  [	        X   [
        5      (       a  [         R                  " U5      X   S'   O=[        S5      eO1U
(       a  [         R                  " U
5      US'   OSU-  n[        U5      eSU< S	U< 3nU R                  5       R                  5       R                  5       R                  XUS
9R                  5       $ )a  Create a connection with the given connection reference.

Arguments:
  client: the client used to make the request.
  project_id: Project ID.
  location: Location of connection.
  connection_type: Type of connection, allowed values: ['CLOUD_SQL']
  properties: Connection properties in JSON format.
  connection_credential: Connection credentials in JSON format.
  display_name: Friendly name for the connection.
  description: Description of the connection.
  connection_id: Optional connection ID.
  kms_key_name: Optional KMS key name.
  connector_configuration: Optional configuration for connector.

Returns:
  Connection object that was created.
friendlyNamer'   
kmsKeyName
credentialz'The `properties` were not a dictionary.configurationzMconnection_type %s is unsupported or connector_configuration is not specified	projects//locations/)parentconnectionIdbody)r   CONNECTION_TYPE_TO_PROPERTY_MAPr   	ParseJson
isinstancer	   
ValueErrorr   r   r   creater   )r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   
connectionproperty_nameerrorr2   s                  r   CreateConnectionr=   <   s   @ *!-~ +}+|$DDHH-  2 < <Z HJ	J-w	7	72D2N2N!3

!,/ BCC  "4">">#J
	&	' 
 U
)3X>&ooy{{}vVjvIwyr    c	                    U(       d  U(       a  U(       d  [         R                  " S5      e0 n	/ n
U(       a  XYS'   U
R                  S5        U(       a  XiS'   U
R                  S5        Ub  U
R                  S5        U(       a  XyS'   US:X  a  U(       aD  [        R                  " U5      nXS'   U
R                  [        UR                  5       U5      5        O0 U	S'   U(       a-  [        R                  " U5      U	S   S'   U
R                  S	5        GOUS
:X  a  U(       a  [        R                  " U5      nXS'   UR                  S5      (       a*  US   R                  S5      (       a  U
R                  S5        UR                  S5      (       a*  US   R                  S5      (       a  U
R                  S5        O0 U	S'   U(       a-  [        R                  " U5      U	S   S'   U
R                  S5        GOUS:X  aq  U(       ah  [        R                  " U5      nXS'   UR                  S5      (       a  U
R                  S5        UR                  S5      (       a  U
R                  S5        GOUS:X  a  U(       aD  [        R                  " U5      nXS'   U
R                  [        UR                  5       U5      5        O0 U	S'   U(       a-  [        R                  " U5      U	S   S'   U
R                  S5        GOUS:X  aQ  U(       aD  [        R                  " U5      nXS'   U
R                  [        UR                  5       U5      5        O0 U	S'   OUS:X  aW  U(       aJ  [        R                  " U5      nUU	S'   SU;   a  U
R                  S 5        S!U;   a  U
R                  S"5        Om0 U	S'   OgU(       a`  [        R                  " U5      U	S#'   U
R                  [        S#U	S#   5      5        [        U
;   a  [        U
;  a  U
R                  [        5        U R                  5       R                  5       R                  5       R                  UR!                  5       S$R#                  U
5      U	S%9R%                  5       $ )&a  Update connection with the given connection reference.

Arguments:
  client: the client used to make the request.
  reference: Connection to update
  connection_type: Type of connection, allowed values: ['CLOUD_SQL']
  properties: Connection properties
  connection_credential: Connection credentials in JSON format.
  display_name: Friendly name for the connection
  description: Description of the connection
  kms_key_name: Optional KMS key name.
  connector_configuration: Optional configuration for connector

Raises:
  bq_error.BigqueryClientError: The connection type is not defined
    when updating
  connection_credential or properties.
Returns:
  Connection object that was created.
zMconnection_type is required when updating connection_credential or propertiesr,   r'   r)   r-   	CLOUD_SQLcloudSqlr.   zcloudSql.credentialAWSawscrossAccountRole	iamRoleIdzaws.crossAccountRole.iamRoleId
accessRolezaws.access_role.iam_role_idzaws.credentialAzureazurecustomerTenantIdzazure.customer_tenant_idfederatedApplicationClientIdz%azure.federated_application_client_idSQL_DATA_SOURCEsqlDataSourcezsqlDataSource.credentialCLOUD_SPANNERcloudSpannerSPARKsparksparkHistoryServerConfigz!spark.spark_history_server_configmetastoreServiceConfigzspark.metastore_service_configr/   ,)r   
updateMaskr4   )r   BigqueryClientErrorappendr   r6   extend_GetUpdateMasklowerr   _GetUpdateMaskRecursively_AUTH_PROFILE_ID_PATH
_AUTH_PATHr   r   r   patchr   joinr   )r   r   r#   r$   r%   r&   r'   r)   r*   r:   update_maskcloudsql_propertiesaws_propertiesazure_propertiessql_data_source_propertiescloudspanner_propertiesspark_propertiess                    r   UpdateConnectionre      s%   D z?

&
&	  *+!-~~& +}}%~&+|#.88D2
..02E
F
  "j-?-I-I
.j\* ./%)33J?n(			.	/	/N
5K5 	;<			L	)	)n\.J.N.N
/ / 	89j(:(D(D
)j% )*'!+55jA,			0	1	156			<	=	=BC++#5#?#?
#K $>!
..02L
M
 %'j!2D2N2N
3j!,/ 34/) 2 < <Z H#: 
..02I
J $&j '!+55jA,j	#'7	7>?	!%5	5;<j"4">">#J !/:o3NO +
+0M$ ooy{{}u~~XXk*  
 wy
r    	base_pathjson_propertiesreturnc                 f    U Vs/ s H  nU S-   [         R                  " U5      -   PM!     sn$ s  snf )zCreates an update mask from json_properties.

Arguments:
  base_path: 'cloud_sql'
  json_properties: { 'host': ... , 'instanceId': ... }

Returns:
    list of  paths in snake case:
    mask = ['cloud_sql.host', 'cloud_sql.instance_id']
.)
inflection
underscore)rf   rg   json_propertys      r   rW   rW   -  s=     +
*- #o
--m<<*
  
s   &.prefixr   c                     U [         ;   a*  [        R                  " [        U5      (       a  U$ SU-   S-   $ [        R
                  " U5      $ )a@  Escapes name if it points to a map key or converts it to snake case.

If name points to a map key:
1. Do not change the name.
2. Escape name with backticks if it is not a valid proto field name.

Args:
  prefix: field mask prefix to check if name points to a map key.
  name: name of the field.

Returns:
  escaped name
`)_MAP_KEY_PATHSre	fullmatch_VALID_FIELD_NAME_REGEXPrk   rl   )rn   r   s     r   _EscapeIfRequiredru   @  sP     ~ <<0$77 	 Dj3 
		t	$$r    
json_valuec                     [        U[        5      (       a  U(       d  U /$ / nU H?  nU S-   [        X5      -   nUR                  U5      nUR	                  [        XE5      5        MA     U$ )zRecursively traverses json_value and returns a list of update mask paths.

Args:
  prefix: current prefix of the json value.
  json_value: value to traverse.

Returns:
  a field mask containing all the set paths in the json value.
rj   )r7   dictru   r   rV   rY   )rn   rv   resultr   
new_prefixnew_json_values         r   rY   rY   Y  sd     
J	%	%Z8O&d# 1& ??J^^D)N
MM+JGH 
 
-r    c                     U R                  5       R                  5       R                  5       R                  UR	                  5       S9R                  5         g)zDelete a connection with the given connection reference.

Arguments:
  client: the client used to make the request.
  reference: Connection to delete.
r   N)r   r   r   deleter   r   r   s     r   DeleteConnectionr~   q  sA     	//++-44>> 5 GIr    max_results
page_tokenc                     SU< SU< 3nU R                  5       R                  5       R                  5       R                  XTUS9R	                  5       $ )a<  List connections in the project and location for the given reference.

Arguments:
  client: the client used to make the request.
  project_id: Project ID.
  location: Location.
  max_results: Number of results to show.
  page_token: Token to retrieve the next page of results.

Returns:
  List of connection objects
r0   r1   )r2   	pageTokenpageSize)r   r   r   listr   )r   r!   r"   r   r   r2   s         r   ListConnectionsr     s@    & *4X>&ooy{{}t6+tFwyr    policyc                    [         R                  " U[         R                  R                  SS9  U R	                  5       R                  5       R                  5       R                  UR                  5       SU0S9R                  5       $ )as  Sets IAM policy for the given connection resource.

Arguments:
  client: the client used to make the request.
  reference: the ConnectionReference for the connection resource.
  policy: The policy string in JSON format.

Returns:
  The updated IAM policy attached to the given connection resource.

Raises:
  BigqueryTypeError: if reference is not a ConnectionReference.
SetConnectionIAMPolicymethodr   )resourcer4   )
r   	typecheckApiClientHelperConnectionReferencer   r   r   setIamPolicyr   r   )r   r   r   s      r   r   r     sg    $ !!55% ooy{{}|Y^^-Xv4F|Gwyr    c                    [         R                  " U[         R                  R                  SS9  U R	                  5       R                  5       R                  5       R                  UR                  5       S9R                  5       $ )a?  Gets IAM policy for the given connection resource.

Arguments:
  client: the client used to make the request.
  reference: the ConnectionReference for the connection resource.

Returns:
  The IAM policy attached to the given connection resource.

Raises:
  BigqueryTypeError: if reference is not a ConnectionReference.
GetConnectionIAMPolicyr   )r   )
r   r   r   r   r   r   r   getIamPolicyr   r   r   s     r   r   r     s`      !!55% ooy{{}|Y^^-|.wyr    )NNNNNN)NNNNNNN)*__doc__
__future__r   r   r   rr   typingr   r   r   r	   r
   googleapiclientr   rk   utilsr   r   r   r   ServiceAUTHORIZATION_CODEVERSION_INFOrt   rq   rZ   r[   Resourcer   r   r   strr=   re   rW   ru   rY   r~   intr   r   r    r    r   <module>r      s   1 &  % 	 5 5 %     $


 *  ,  -
 B +
**>>8 ,0"&!%#'"&-1III I 	I
 I $C=I 3-I #I C=I 3-I &c]Ib 	 $+/"&!%"&-1bb**>>b b b $C=b 3-b #b 3-b &c]bJ%)#s(^	#Y&%c % % %2!#s(^	#Y0**>>  	
 :**>> @**>>r    