
    k5                        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mZm	Z	m
Z
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 ej(                  ZdZdZdZddgZdZdZdej6                  dej8                  j:                  fdZ	 	 	 	 	 	 d2dej6                  dededededee   dee   dee   dee   dee   dee   fd Z 	 	 	 	 	 	 	 d3dej6                  dej8                  j:                  dee   dee   dee   dee   dee   dee   dee   fd!Z!d"ed#eeef   d$e	e   fd%Z"d&ed'ed$efd(Z#d&ed)eeef   d$e	e   fd*Z$dej6                  dej8                  j:                  fd+Z%dej6                  deded,e&d-ee   f
d.Z'dej6                  dej8                  j:                  d/efd0Z(dej6                  dej8                  j:                  fd1Z)y)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                     | j                         j                         j                         j                  |j	                               j                         S )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                    i }|r||d<   |r||d<   |	r|	|d<   t         j                  j                  |      }|rTt        j                  |      ||<   |ret	        ||   t
              rt        j                  |      ||   d<   n6t        d      |
rt        j                  |
      |d<   nd|z  }t        |      d|d	|}| j                         j                         j                         j                  |||
      j                         S )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   @ *!-J~ +J}+J|$DDHH-  2 < <Z HJ}	J}-w	72D2N2N!3

=!,/ BCC"4">">#J
	&	' 
 U
)3X>&ooy{{}vV-jvIwyr    c	                    |s|r|st        j                  d      i }	g }
|r||	d<   |
j                  d       |r||	d<   |
j                  d       ||
j                  d       |r||	d<   |dk(  r||rDt        j                  |      }||	d<   |
j                  t        |j                         |             ni |	d<   |rt        j                  |      |	d   d<   |
j                  d	       n|d
k(  r|rt        j                  |      }||	d<   |j                  d      r%|d   j                  d      r|
j                  d       |j                  d      r+|d   j                  d      r|
j                  d       ni |	d<   |rt        j                  |      |	d   d<   |
j                  d       n|dk(  rd|rt        j                  |      }||	d<   |j                  d      r|
j                  d       |j                  d      r|
j                  d       n|dk(  r||rDt        j                  |      }||	d<   |
j                  t        |j                         |             ni |	d<   |r.t        j                  |      |	d   d<   |
j                  d       n |dk(  rL|rDt        j                  |      }||	d<   |
j                  t        |j                         |             ni |	d<   n|dk(  rM|rEt        j                  |      }||	d<   d|v r|
j                  d        d!|v ru|
j                  d"       nci |	d<   n]|r[t        j                  |      |	d#<   |
j                  t        d#|	d#                t        |
v rt        |
vr|
j                  t               | j                         j                         j                         j                  |j!                         d$j#                  |
      |	%      j%                         S )&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?

&
&	  *+!-J~~& +J}}%~&+J|#.88D2j
..02E
F
  "j-?-I-I
.j\* ./%)33J?n(j			.	/N
5K5 	;<			L	)n\.J.N.N
/ 	89j(:(D(D
)j% )*'!+55jA,j			0	156			<	=BC++#5#?#?
#K $>j!
..02L
M
 %'j!2D2N2N
3j!,/ 34/) 2 < <Z H#:j 
..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                 ^    |D cg c]  }| dz   t        j                  |      z    c}S c c}w )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                     | t         v r$t        j                  t        |      r|S d|z   dz   S t	        j
                  |      S )aR  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   @  sM     ~ <<0$7 	 Dj3 
		t	$$r    
json_valuec                     t        |t              r|s| gS g }|D ]@  }| dz   t        | |      z   }|j                  |      }|j	                  t        ||             B |S )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  se     
J	%Z8O&d# 1&$ ??J^^D)N
MM+JGH 
 
-r    c                     | j                         j                         j                         j                  |j	                               j                          y)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                     d|d|}| j                         j                         j                         j                  |||      j	                         S )aN  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     sB    & *4X>&ooy{{}t6Z+tFwyr    policyc                    t        j                  |t         j                  j                  d       | j	                         j                         j                         j                  |j                         d|i      j                         S )a  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                    t        j                  |t         j                  j                  d       | j	                         j                         j                         j                  |j                               j                         S )aO  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    