
                             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 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j(                        Z G d de      Zy)z3Database Migration Service connection profiles API.    )absolute_import)division)unicode_literalsN)Any)
list_pager)api_util)base)
exceptions)labels_utilc                       e Zd ZdZy)'UnsupportedConnectionProfileDBTypeErrorzFError raised when the connection profile database type is unsupported.N)__name__
__module____qualname____doc__     Dlib/googlecloudsdk/api_lib/database_migration/connection_profiles.pyr   r   !   s    Nr   r   c                      e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdAdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d% Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4d4 Z5d5 Z6d6 Z7d7 Z8d8 Z9d9 Z:dAd:Z;dAd;Z<d<e=d=e>fd>Z?d? Z@d@ ZAy)BConnectionProfilesClientz2Client for connection profiles service in the API.c                    t        j                  |      | _        t        j                  |      | _        t        j
                  |      | _        | j                  j                  | _        t        j                  |      | _
        || _        y N)r   GetApiVersion_api_versionGetClientInstanceclientGetMessagesModulemessages%projects_locations_connectionProfiles_serviceGetResourceParserresource_parser_release_track)selfrelease_tracks     r   __init__z!ConnectionProfilesClient.__init__(   se     ..}=D,,];DK..}=DMKKEEDM#55mDD'Dr   c                     |j                   j                  r|j                   j                  n|j                   j                  }|r#dj                  |      j	                  d      d   S y)zGets the SQL engine from the Cloud SQL version.

    Args:
      cloudsql: Cloud SQL connection profile

    Returns:
      A string representing the SQL engine
    z{}_r    )settingsdatabaseVersiondatabaseVersionNameformatsplit)r$   cloudsqldatabase_versoins      r   _GetEngineFromCloudSqlz/ConnectionProfilesClient._GetEngineFromCloudSql0   s`     ,, 	))22 
 [[)*005a88r   c                    	 |j                   ry|j                  r| j                  |j                        S |j                  ry|j                  ry|j
                  ry|j                  ryy# t        $ r
}Y d}~yd}~ww xY w)zGets the SQL engine name from the connection profile.

    Args:
      profile: the connection profile

    Returns:
      A string representing the SQL engine
    MYSQLPOSTGRESr)   ORACLE	SQLSERVERN)mysqlr/   r1   
postgresqlalloydboracle	sqlserverAttributeError)r$   profiler(   s      r   GetEngineNamez&ConnectionProfilesClient.GetEngineNameE   sw    				**7+;+;<< 
							  	s-   A+ &A+ A+ A+ A+ A+ +	A>9A>c                 $    | j                   dk(  ryy)Nv1alpha2certificateclient_certificater   r$   s    r   _ClientCertificateArgNamez2ConnectionProfilesClient._ClientCertificateArgNamee   s    J&r   c                 $    | j                   dk(  ryy)Nr@   instancecloudsql_instancerC   rD   s    r   _InstanceArgNamez)ConnectionProfilesClient._InstanceArgNamej   s    J&r   c                 P    | j                   t        j                  j                  k(  S r   r#   r	   ReleaseTrackGArD   s    r   _SupportsPostgresqlz,ConnectionProfilesClient._SupportsPostgresqlo       $"3"3"6"666r   c                 P    | j                   t        j                  j                  k(  S r   rK   rD   s    r   _SupportsOraclez(ConnectionProfilesClient._SupportsOracler   rO   r   c                 P    | j                   t        j                  j                  k(  S r   rK   rD   s    r   _SupportsSqlServerz+ConnectionProfilesClient._SupportsSqlServeru   rO   r   c                 P    | j                   t        j                  j                  k(  S r   rK   rD   s    r   _SupportsSslTypez)ConnectionProfilesClient._SupportsSslTypex   rO   r   Nc                 J    | j                  |       | j                  ||       y r   )_ValidateHostArgs_ValidateSslConfigArgsr$   argscp_types      r   _ValidateArgsz&ConnectionProfilesClient._ValidateArgs{   s     4 g.r   c                     |j                  d      syt        j                  d      }|j                  |j                        st        j                  dd      y )NhostTz$[a-zA-Z0-9][-.a-zA-Z0-9]*[a-zA-Z0-9]zUHostname and IP can only include letters, numbers, dots, hyphens and valid IP ranges.)IsKnownAndSpecifiedrecompilematchr^   calliope_exceptionsInvalidArgumentException)r$   rZ   patterns      r   rW   z*ConnectionProfilesClient._ValidateHostArgs   sR    ##F+jj?@G==#88
  $r   c                     |dk(  s|dk(  r| j                  |       | j                  |d       | j                  || j                                | j                  |d       y )Nr3   r4   ca_certificateprivate_key)0_ValidateSslConfigCombinationForPostgresAndMySql_ValidateCertificateFormatrE   rY   s      r   rX   z/ConnectionProfilesClient._ValidateSslConfigArgs   s[    'W
2
 ;;DA##D*:;##D$*H*H*JK##D-8r   c                 4   t        ||      r|j                  |      syt        ||      }|j                         }|j	                  d      }|d   j                  d      r|d   j                  d      s%t        j                  |dj                  |            y )NT
r   z-----z8The certificate does not appear to be in PEM format:
{0})	hasattrIsSpecifiedgetattrstripr.   
startswithrc   rd   r-   )r$   rZ   fieldrA   cert
cert_liness         r   rj   z3ConnectionProfilesClient._ValidateCertificateFormat   s    4t'7'7'>$&KDD!JqM$$W-"~((188

E6$<  2r   c                 
   | j                         r|j                  d      sy|j                  dk(  rJ|j                  d      r#|j                  d      s|j                  d      syt        j                  dd      |j                  d	k(  rJ|j                  d      r#|j                  d      r|j                  d      ryt        j                  dd
      |j                  dk(  s|j                  dk(  rc|j                  d      s#|j                  d      s|j                  d      syt        j                  ddj                  |j                              t        j                  ddj                  |j                              )aR  Validates the SSL config combination for PostgreSQL and MySQL when the ssl_type flag is specified.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Raises:
      calliope_exceptions.InvalidArgumentException: If the specified
      certificates and keys combination is invalid.
    ssl_typeNSERVER_ONLYrg   rh   rB   sslz>Only ca_certificate must be provided for SSL type SERVER_ONLY.SERVER_CLIENTz_ca_certificate, client_certificate and private_key must be provided for SSL type SERVER_CLIENT.REQUIREDNONEzOCannot set ca_certificate, client_certificate and private_key for SSL type {0}.zUnsupported SSL type {0}.)rU   ro   rw   rc   rd   r-   r$   rZ   s     r   ri   zIConnectionProfilesClient._ValidateSslConfigCombinationForPostgresAndMySql   sg      "$*:*::*F}}%


+
,""=1""#78!::L
 	
 }}'


+
,}-34!::4
 	

 
*	$(?/0""=1""#78!::!!'!6
 	
  88

%
,
,T]]
; r   c                 N    | j                   j                  |j                        S )N)caCertificate)r   	SslConfigrg   r}   s     r   _GetSslServerOnlyConfigz0ConnectionProfilesClient._GetSslServerOnlyConfig   s     ==""1D1D"EEr   c                     |j                  d      rO| j                  j                  | j                  j                  j                  j                  |j
                        S t        j                  dd      )Nrg   )typer   4ca_certificate is required for SERVER_ONLY SSL type.)ro   r   r   TypeValueValuesEnumrx   rg   rc   rd   r}   s     r   _GetSslServerOnlyConfigWithTypez8ConnectionProfilesClient._GetSslServerOnlyConfigWithType   sj    ()]]$$}}&&::FF++ %  
  88

@ r   c                     | j                   j                  | j                   j                  j                  j                        S N)r   )r   r   r   r{   rD   s    r   _GetSslRequiredConfigz.ConnectionProfilesClient._GetSslRequiredConfig   s7    ==""]]$$88AA #  r   c                     | j                   j                  | j                   j                  j                  j                        S r   )r   r   r   r|   rD   s    r   _GetSslNoneConfigz*ConnectionProfilesClient._GetSslNoneConfig   s7    ==""]]$$88== #  r   c                 >   |j                  d      sy|j                  }g }|j                         D ]E  \  }}|j                  | j                  j
                  j                  j                  ||             G | j                  j
                  j                  |      S )zGets the SSL flags.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      SslFlagsValue, to use when creating the connection profile.
    	ssl_flagsN)keyvalue)additionalProperties)ro   r   itemsappendr   r   SslFlagsValueAdditionalProperty)r$   rZ   ssl_flags_dictflagsr   r   s         r   _GetSslFlagsz%ConnectionProfilesClient._GetSslFlags   s     K(^^NE$**,
Ull
--
!
!
/
/
B
BU C  - ==""00e0LLr   c                    d}|j                  d      ra|j                  dk(  r| j                  |      }nb|j                  dk(  r| j                         }nB|j                  dk(  r3| j	                         }n"|j                  d      r| j                  |      }|A| j                  j                  |j                  |j                  | j                  |            S y)a]  Gets the SSL config based on the specified SSL type.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      SslConfig, to use when creating the connection profile.

    Raises:
      calliope_exceptions.InvalidArgumentException: If the specified SSL type is
        unsupported.
    Nrw   rx   r{   r|   rg   )r   r   sslFlags)ro   rw   r   r   r   r   r   r   r   r   r   r$   rZ   
ssl_configs      r   !_GetSslServerOnlyOrRequiredConfigz:ConnectionProfilesClient._GetSslServerOnlyOrRequiredConfig  s     J
#	-	'99$?
==J&//1
==F"++-
			*	+//5j]]$$"00$$T* %   r   c                 `   | j                   j                  |j                  |j                  | j	                               |j
                  | j                         rO|j                  d      r>| j                   j                  j                  j                  |j                              S d       S )Nrw   )	clientKeyclientCertificater   r   )r   r   rh   GetValuerE   rg   rU   ro   r   lookup_by_namerw   r}   s     r   _GetSslConfigz&ConnectionProfilesClient._GetSslConfig-  s    ==""""--(F(F(HI))   "t'7'7
'C ]]$$88GGMM
	 # 	 	  # 	 	r   c                 h   | j                         r|j                  d      s|j                  d      r6|j                  |j                  j                  _        |j                  d       |j                  d      r6|j                  |j                  j                  _        |j                  d       |j                  | j                               rJ|j                  | j                               |j                  j                  _        |j                  d       y	y	| j                  |       |j                  d       |j                  d       |j                  d       |j                  d       |j                  dk(  r|j                  |j                  j                  _        d	|j                  j                  _        d	|j                  j                  _        | j                  j                  j                   j"                  |j                  j                  _        |j                  d
k(  r|j                  |j                  j                  _        |j                  |j                  j                  _        |j                  | j                               |j                  j                  _        | j                  j                  j                   j&                  |j                  j                  _        y	|j                  dk(  s|j                  dk(  rd	|j                  j                  _        d	|j                  j                  _        d	|j                  j                  _        | j                  j                  j                   j)                  |j                        |j                  j                  _        y	y	)zIFills connection_profile and update_fields with MySQL SSL data from args.rw   rg   zmysql.ssl.caCertificaterh   zmysql.ssl.clientKeyzmysql.ssl.clientCertificatezmysql.ssl.typerx   Nrz   r{   r|   )rU   ro   rg   r7   ry   r   r   rh   r   rE   r   r   ri   rw   r   r   r   rx   r   rz   r   r$   connection_profilerZ   update_fieldss       r   _UpdateMySqlSslConfigz.ConnectionProfilesClient._UpdateMySqlSslConfig9  s     "$*:*::*F 
		*	+595H5H  $$267			-	(151A1A  $$.23			$88:	;9=**,:
  $$6 	:;	 
< ;;DA458901+,	-	'595H5H  $$215  $$.9=  $$6MM##77CC 	  $$) 
/	)595H5H  $$2151A1A  $$.9=**,:
  $$6 MM##77EE 	  $$) ==J&$--6*A59  $$215  $$.9=  $$6MM##77FF 	  $$)	 +Br   c                     | j                  |      }| j                  j                  |j                  |j                  |j
                  |j                  ||j                  | j                                     S )N)r^   portusernamepasswordry   
cloudSqlId)	r   r   MySqlConnectionProfiler^   r   r   r   r   rI   r   s      r   _GetMySqlConnectionProfilez3ConnectionProfilesClient._GetMySqlConnectionProfilej  sb    ##D)J==//YYYY==!6!6!89 0 ; ;r   c                    |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j
                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  | j                               r?|j                  | j                               |j                  _	        |j                  d	       | j                  |||       y
)z!Updates MySQL connection profile.r^   z
mysql.hostr   z
mysql.portr   zmysql.usernamer   zmysql.passwordzmysql.instanceN)ro   r^   r7   r   r   r   r   rI   r   r   r   r   s       r   _UpdateMySqlConnectionProfilez6ConnectionProfilesClient._UpdateMySqlConnectionProfilet  s    &*ii#<(&*ii#<(
#*.--'+,
#*.--'+,--/0,0MM



!-#)+,14Gr   c                 h   | j                         r|j                  d      s|j                  d      r6|j                  |j                  j                  _        |j                  d       |j                  d      r6|j                  |j                  j                  _        |j                  d       |j                  | j                               rJ|j                  | j                               |j                  j                  _        |j                  d       y	y	| j                  |       |j                  d       |j                  d       |j                  d       |j                  d       |j                  dk(  r|j                  |j                  j                  _        d	|j                  j                  _        d	|j                  j                  _        | j                  j                  j                   j"                  |j                  j                  _        |j                  d
k(  r|j                  |j                  j                  _        |j                  |j                  j                  _        |j                  | j                               |j                  j                  _        | j                  j                  j                   j&                  |j                  j                  _        y	|j                  dk(  s|j                  dk(  rd	|j                  j                  _        d	|j                  j                  _        d	|j                  j                  _        | j                  j                  j                   j)                  |j                        |j                  j                  _        y	y	)zNFills connection_profile and update_fields with PostgreSQL SSL data from args.rw   rg   zpostgresql.ssl.caCertificaterh   zpostgresql.ssl.clientKeyz postgresql.ssl.clientCertificatezpostgresql.ssl.typerx   Nrz   r{   r|   )rU   ro   rg   r8   ry   r   r   rh   r   rE   r   r   ri   rw   r   r   r   rx   r   rz   r   r   s       r   _UpdatePostgreSqlSslConfigz3ConnectionProfilesClient._UpdatePostgreSqlSslConfig  s     "$*:*::*F 
		*	+:>:M:M%%))7;<			-	(6:6F6F%%))378			$88:	;>Bmm**,?
%%)); 	?@	 
< ;;DA9:=>5601	-	':>:M:M%%))76:%%))3>B%%));MM##77CC 	%%)). 
/	):>:M:M%%))76:6F6F%%))3>Bmm**,?
%%)); MM##77EE 	%%)). ==J&$--6*A:>%%))76:%%))3>B%%));MM##77FF 	%%)).	 +Br   c                    | j                  |      }| j                  dk(  r|j                  nd}| j                  j	                  |j
                  |j                  |j                  |j                  |j                  ||j                  | j                               |      }|j                  j                  j                         }|r3|j                         }| j                  j!                  |      |_        |S |j$                  ri |_        |S )a  Creates a Postgresql connection profile according to the given args.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      PostgreSqlConnectionProfile, to use when creating the connection profile.
    v1r)   )r^   r   r   r   databasery   r   alloydbClusterId)serviceAttachment)r   r   alloydb_clusterr   PostgreSqlConnectionProfiler^   r   r   r   r   r   rI   CONCEPTSpsc_service_attachmentParseRelativeName!PrivateServiceConnectConnectivity!privateServiceConnectConnectivitystatic_ip_connectivitystaticIpConnectivity)r$   rZ   r   r   connection_profile_obj(private_service_connect_connectivity_refpsc_relative_names          r   _GetPostgreSqlConnectionProfilez8ConnectionProfilesClient._GetPostgreSqlConnectionProfile  s     ##D)J.2.?.?4.Gd**RO!]]FFYYYY==!6!6!89( G 	 	,,224 - 0
2
?
?
A  --
9
9 1 :  > "! 
	$	$461!!r   c                    |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j
                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d	      r,|j                  |j                  _        |j                  d
       |j                  | j                               r?|j                  | j                               |j                  _
        |j                  d       | j                  dk(  r=|j                  d      r,|j                  |j                  _        |j                  d       | j                  |||       y)z&Updates PostgreSQL connection profile.r^   zpostgresql.hostr   zpostgresql.databaser   zpostgresql.portr   zpostgresql.usernamer   zpostgresql.passwordzpostgresql.instancer   r   zpostgresql.alloydb_clusterN)ro   r^   r8   r   r   r   r   r   rI   r   r   r   r   r   r   r   s       r   "_UpdatePostgreSqlConnectionProfilez;ConnectionProfilesClient._UpdatePostgreSqlConnectionProfile  s    +/99##(,-
#/3}}##,01+/99##(,-
#/3}}##,01
#/3}}##,01--/015



!2###.01D T%5%56G%H7;7K7K##478##$6mLr   c                     |j                  d      r7|j                  |j                  j                  _        |j                  d       yy)zJFills connection_profile and update_fields with Oracle SSL data from args.rg   zoracle.ssl.caCertificateN)ro   rg   r:   ry   r   r   r   s       r   _UpdateOracleSslConfigz/ConnectionProfilesClient._UpdateOracleSslConfig  s@    ()484G4G##156 *r   c                    |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j
                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d	      r,|j                  |j                  _        |j                  d
       | j                  |||       y)z"Updates Oracle connection profile.r^   zoracle.hostr   zoracle.portr   zoracle.usernamer   zoracle.passworddatabase_servicezoracle.databaseServiceN)
ro   r^   r:   r   r   r   r   r   databaseServicer   r   s       r   _UpdateOracleConnectionProfilez7ConnectionProfilesClient._UpdateOracleConnectionProfile  s    '+yy$=)'+yy$=)
#+/==(,-
#+/==(,-*+262G2G/34 2D-Hr   c                    |j                   |j                  j                  _        | j                  j
                  j                  j                  |j                  j                  _        |j                  d       |j                  d       y )Nsqlserver.ssl.caCertificatesqlserver.ssl.type)
rg   r;   ry   r   r   r   r   rx   r   r   r   s       r   #_UpdateSqlServerSslConfigServerOnlyz<ConnectionProfilesClient._UpdateSqlServerSslConfigServerOnly  sl     6:5H5H  $$233??   $$) 67-.r   c                    |j                  d      rE|j                  dk(  r;|j                  d      r| j                  |||       yt        j                  dd      |j                  dk(  s|j                  dk(  r|j                  d      r;|j
                  r/t        j                  ddj                  |j                              d|j                  j                  _	        | j                  j                  j                  j                  |j                        |j                  j                  _        |j                  d	       |j                  d
       yy|j                  d      r| j                  |||       yy)zMFills connection_profile and update_fields with SqlServer SSL data from args.rw   rx   rg   r   r{   r|   z+Cannot set CA certificate for SSL type {0}.Nr   r   )ro   rw   r   rc   rd   rg   r-   r;   ry   r   r   r   r   r   r   r   r   s       r   _UpdateSqlServerSslConfigz2ConnectionProfilesClient._UpdateSqlServerSslConfig"  sW   
#	-	',-

2
2 $ $<<D  ==J&$--6*A,-$2E2E#<<;BB--  :>$$((6MM##77FF 	$$((-
 	:;12 +B  
		*	+
..
dM 
,r   c                    |j                  d      r?|j                  | j                               |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _	        |j                  d       |j                  d	      r,|j                  |j                  _        |j                  d
       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r,|j                  |j                  _        |j                  d       |j                  d      r]|j                  j                  t!        j"                  dd      | j%                  |      |j                  _        |j                  d       | j'                  |||       y)z%Updates SqlServer connection profile.rH   zsqlserver.cloudSqlIdcloudsql_project_idzsqlserver.cloudSqlProjectIdr^   zsqlserver.hostr   zsqlserver.portdbm_portzsqlserver.dbmPortr   zsqlserver.usernamer   zsqlserver.passwordr   zsqlserver.database
gcs_bucketNzWCannot update GCS Bucket or GCS Prefix for a destination SQL Server connection profile.zsqlserver.backups)ro   r   rI   r;   r   r   r   cloudSqlProjectIdr^   r   r   dbmPortr   r   r   backupsrc   rd   _GetSqlServerBackupsr   r   s       r   !_UpdateSqlServerConnectionProfilez:ConnectionProfilesClient._UpdateSqlServerConnectionProfileD  s    +,04



!1""- 12-.7;7O7O""489*.))""'+,*.))""'+,
#-1]]""*./
#.2mm""+/0
#.2mm""+/0
#.2mm""+/0%		%	%	-	-	5!::*
 	

 .2-F-Ft-L""*./""#5t]Kr   c                 h    ||j                   j                  S |j                   j                  |      S r   )ProviderValueValuesEnumDATABASE_PROVIDER_UNSPECIFIEDr   )r$   r[   providers      r   _GetProviderz%ConnectionProfilesClient._GetProviderm  s1    ,,JJJ**99(CCr   c                 8    |j                   j                  |      S r   )RoleValueValuesEnumr   )r$   r[   roles      r   _GetRolez!ConnectionProfilesClient._GetRoler  s    &&55d;;r   c                 h    ||j                   j                  S |j                   j                  |      S r   )ActivationPolicyValueValuesEnum!SQL_ACTIVATION_POLICY_UNSPECIFIEDr   )r$   r[   policys      r   _GetActivationPolicyz-ConnectionProfilesClient._GetActivationPolicyu  s4    ~

1
1
S
S 22AA&IIr   c                 8    |j                   j                  |      S )zReturns the database version.

    Args:
      cp_type: str, the connection profile type.
      version: database version.
    )DatabaseVersionValueValuesEnumr   )r$   r[   versions      r   _GetDatabaseVersionz,ConnectionProfilesClient._GetDatabaseVersion|  s     11@@IIr   c                 z    | j                   j                  }|j                  D cg c]  } ||       c}S c c}w )N)	cidrRange)r   AuthorizedNetworkauthorized_network_cidr_ranges)r$   rZ   authorized_networkrs       r   _GetAlloyDbAuthorizedNetworksz6ConnectionProfilesClient._GetAlloyDbAuthorizedNetworks  sD    88 444A 	Q'4  s   8c                 f    | j                   j                  }|D cg c]  } ||       c}S c c}w )N)r   )r   SqlAclEntry)r$   networks	acl_entrynetworks       r   _GetAuthorizedNetworksz/ConnectionProfilesClient._GetAuthorizedNetworks  s=    ))I  G 	   s   .c                     | j                   j                  |j                  |j                  |j                  | j                  |j                              }| j                  dk(  r|j                  |_	        |S )N)
enableIpv4privateNetwork
requireSslauthorizedNetworksr   )
r   SqlIpConfigenable_ip_v4private_networkrequire_sslr   authorized_networksr   allocated_ip_rangeallocatedIpRange)r$   rZ   	ip_configs      r   _GetIpConfigz%ConnectionProfilesClient._GetIpConfig  sn    ))$$++##66t7O7OP	 * I D #'#:#:i r   c                 h    ||j                   j                  S |j                   j                  |      S r   )DataDiskTypeValueValuesEnumSQL_DATA_DISK_TYPE_UNSPECIFIEDr   )r$   r[   data_disk_types      r   _GetDataDiskTypez)ConnectionProfilesClient._GetDataDiskType  s1    11PPP..==nMMr   c                 h    ||j                   j                  S |j                   j                  |      S r   )AvailabilityTypeValueValuesEnum!SQL_AVAILABILITY_TYPE_UNSPECIFIEDr   )r$   r[   availability_types      r   _GetAvailabilityTypez-ConnectionProfilesClient._GetAvailabilityType  s:     

1
1
S
S 22AA r   c                     |j                  d      rW| j                  j                  j                  j	                  |j
                  j                  dd      j                               S y )Nedition-r(   )r_   r   CloudSqlSettingsEditionValueValuesEnumr   r  replaceupperr}   s     r   _GetEditionz$ConnectionProfilesClient._GetEdition  sU    	*
--
(
(
?
?
N
Nll""3,224 r   c                 x    |j                  d      r)| j                  j                  } ||j                        S y )Nenable_data_cache)dataCacheEnabled)r_   r   DataCacheConfigr  )r$   rZ   data_cache_config_objs      r   _GetDataCacheConfigz,ConnectionProfilesClient._GetDataCacheConfig  s<     34"mm;;"11  r   c                    | j                   j                  }|j                  j                  j	                         j                         }t        j                  ||j                  d      }t        j                  ||j                  d      }| j                   j                  ||j                  |j                  | j                  ||j                        | j                  |      |j                  || j!                  ||j"                        |j$                  |j&                  |j(                  |      }|j*                  r"| j-                  ||j*                        |_        n|j0                  |_        | j4                  dk(  r^| j7                  ||j8                        |_        |j<                  |_        | jA                  |      |_!        | jE                  |      |_#        | jH                  tJ        jL                  jN                  k(  r[|j                  jP                  j	                         7|j                  jP                  j	                         j                         |_)        |S )a	  Creates a Cloud SQL connection profile according to the given args.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      CloudSqlConnectionProfile, to use when creating the connection profile.
    user_labelsdatabase_flags)
userLabelstierstorageAutoResizeLimitactivationPolicyipConfigautoStorageIncreasedatabaseFlagsdataDiskTypedataDiskSizeGbzonerootPasswordsourceIdr   )*r   r  r   	source_idr   r   r   ParseCreateArgsUserLabelsValueDatabaseFlagsValuer&  storage_auto_resize_limitr   activation_policyr	  auto_storage_increaser  r  data_disk_sizer.  root_passworddatabase_versionr   r+   database_version_namer,   r   r  r  availabilityTypesecondary_zonesecondaryZoner  r  r!  dataCacheConfigr#   r	   rL   rM   cmek_keycmekKeyName)r$   rZ   r[   r1  user_labels_valuer$  cloud_sql_settingss          r   _GetCloudSqlSettingsz-ConnectionProfilesClient._GetCloudSqlSettings  s	    mm,,G''--/<<>I#33g%%}6 00g((*:<N77$YY#==22T++
 ""4( 66$**7D4G4GH**YY'' 8   +/+C+C
4((,( 04/I/I,D ,0,E,E
4))-) *.)<)<&#'#3#3D#9 +/+C+CD+I(t00333MM""((*6 --
 
 
&
&
(
5
5
7 $ r   c                 \    | j                  |      }| j                  j                  |      S )N)r*   )rD  r   CloudSqlConnectionProfile)r$   rZ   r*   s      r   _GetCloudSqlConnectionProfilez6ConnectionProfilesClient._GetCloudSqlConnectionProfile  s*    ((.H==22H2EEr   c                     |j                  d      r9| j                  j                  j                  j	                  |j
                        S y )Nr:  )r_   r   AlloyDbSettingsr   r   r:  r}   s     r   _GetAlloyDBDatabaseVersionz3ConnectionProfilesClient._GetAlloyDBDatabaseVersion  s>     23
--
'
'
F
F>$//0
 r   c                    | j                   j                  }| j                   j                  }t        j                  ||j
                  d      }t        j                  ||j
                  d      }t        j                  ||j                  d      } ||j                  | j                   j                  |j                        | j                   j                  |j                  |j                  | j                  |            ||      } || j                   j                  d|j                        |j                   ||	      }| j#                  |      |_        |j&                  j(                  j+                         }|/| j                   j-                  |j/                         
      |_        | j                   j3                  ||      S )a  Creates an AlloyDB connection profile according to the given args.

    Uses the connection profile ID as the cluster ID, and also sets "postgres"
    as the initial user of the cluster.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.
      connection_profile_id: str, the ID of the connection profile.

    Returns:
      AlloyDBConnectionProfile, to use when creating the connection profile.
    cluster_labelsprimary_labelsr$  )cpuCount)enablePublicIpenableOutboundPublicIpauthorizedExternalNetworks)idmachineConfiginstanceNetworkConfigr+  labelspostgres)userr   )initialUser
vpcNetworkrU  primaryInstanceSettings)
kmsKeyName)	clusterIdr*   )r   rI  PrimaryInstanceSettingsr   r2  LabelsValuer4  
primary_idMachineConfig	cpu_countInstanceNetworkConfigenable_public_ipenable_outbound_public_ipr   UserPasswordr   r   rJ  r+   r   kms_keyr   EncryptionConfigr   encryptionConfigAlloyDbConnectionProfile)	r$   rZ   connection_profile_idcluster_settingsprimary_settingsrL  rM  r$  kms_key_refs	            r   _GetAlloyDBConnectionProfilez5ConnectionProfilesClient._GetAlloyDBConnectionProfile  s    }}44}}<< 001A1M1M1ACN !001A1M1M1ACN !00113CEN (??mm114>>1J"mmAA00#'#A#A'+'I'I$'O B 

 %
 (MM..dmm / 5<< 02 (,'F'Ft'L$--''--/K*.--*H*H --/ +I +' ==11'2B 2 D Dr   c                     | j                   j                  |j                  t        |j                        |j
                  |j                  |j                        S )N)hostnamer   r   
privateKeyr   )r   ForwardSshTunnelConnectivityforward_ssh_hostnameintforward_ssh_portforward_ssh_usernameforward_ssh_private_keyforward_ssh_passwordr}   s     r    _GetForwardSshTunnelConnectivityz9ConnectionProfilesClient._GetForwardSshTunnelConnectivity9  sP    ==55**&&'**//** 6 , ,r   c                    | j                  |      }| j                  j                  |j                  |j                  |j
                  |j                  ||j                        }|j                  j                  j                         }|r1| j                  j                  |j                               |_        |S |j                  r| j                  |      |_        |S |j"                  ri |_        |S )a  Creates an Oracle connection profile according to the given args.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      OracleConnectionProfile, to use when creating the connection profile.
    )r^   r   r   r   ry   r   privateConnection)r   r   OracleConnectionProfiler^   r   r   r   r   r   private_connectionr   PrivateConnectivityr   privateConnectivityrs  ry  forwardSshConnectivityr   staticServiceIpConnectivity)r$   rZ   r   r   private_connectivity_refs        r   _GetOracleConnectionProfilez4ConnectionProfilesClient._GetOracleConnectionProfileA  s     --d3J!]]BBYYYY-- C   $}}??EEG
--
+
+ 8 E E G ,  0 "! 
	"	"

/
/
5 3
 "! 
	$	$;=8!!r   c                     | j                   j                  |j                        }|j                  d      r|j                  |_        |S )N)	gcsBucket
gcs_prefix)r   SqlServerBackupsr   r_   r  	gcsPrefix)r$   rZ   backups_objs      r   r   z-ConnectionProfilesClient._GetSqlServerBackupsd  s=    --004??0KK-"ookr   c                    | j                   j                  | j                  |            }|j                  d      rE|j                  |_        |j
                  |_        |j                  d      r9|j                  |_        n'|j                  d      r| j                  |      |_	        |j                  d      ry|j                  |_
        |j                  |_        |j                  d      r$|j                  | j                               |_        |j                  d      r|j                  |_        |j                  d      r|j"                  |_        |j                  d	      rU|j$                  j&                  j)                         }| j                   j+                  |j-                         
      |_        |S |j                  d      r| j1                  |      |_        |S |j                  d      ri |_        |S )a  Creates an SQL Server connection profile according to the given args.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      SqlServerConnectionProfile, to use when creating the connection profile.
    )ry   r^   r   r   r   rH   r   r   r~  r{  rs  r   )r   SqlServerConnectionProfiler   r_   r^   r   r   r   r   r   r   r   r   rI   r   r   r   r   r   r~  r   r  r   r  ry  r  r   )r$   rZ   r   private_connectivitys       r   _GetSqlServerConnectionProfilez7ConnectionProfilesClient._GetSqlServerConnectionProfilej  s    "]]EE2248 F  '$(II!$(II!		!	!*	-)-&		!	!,	/'+'@'@'F$
+(,%(,%		!	!"5	6,0MM!!#-
) 
	!	!"7	8373K3K0
+(,% 45!]]==CCE
--
+
+ 4 A A C ,  0 "! 
	!	!"8	9

/
/
5 3
 "! 
	!	!":	;461!!r   c                 0   | j                   j                  }t        j                  ||j                        }i }|dk(  r6| j                  |      }||d<   | j                  ||j                        |d<   n|dk(  r6| j                  |      }||d<   | j                  ||j                        |d<   n|dk(  r| j                  |      }	|	|d<   ns|dk(  r| j                  ||      }
|
|d	<   nV|d
k(  r| j                  |      }||d<   n:|dk(  r5| j                  |      }||d<   | j                  ||j                        |d<   |j                  d      r| j                  ||j                        |d<    |d||j                   j"                  |j$                  d|S )z/Returns a connection profile according to type.r3   r7   r   CLOUDSQLr/   
POSTGRESQLr8   ALLOYDBr9   r5   r:   r6   r;   r   )rU  statedisplayNamer   )r   ConnectionProfiler   r2  r^  r   r   r   rG  r   rn  r  r  r_   r   r   StateValueValuesEnumCREATINGdisplay_name)r$   r[   rZ   rj  connection_profile_typerU  paramsmysql_connection_profilecloudsql_connection_profilepostgresql_connection_profilealloydb_connection_profileoracle_connection_profilesqlserver_connection_profiles                r   _GetConnectionProfilez.ConnectionProfilesClient._GetConnectionProfile  s   "mm==((%11F F'!%!@!@!F0fWo,,
!4==fZ 
J	$($F$Ft$L!6fZ,,
!4==fZ 
L	 &*&J&J4&P#:f\	I	#'#D#D
%$  5fY	H	"&"B"B4"H2fX	K	%)%H%H%N"8f[,,
!4==fZ '}}%<diiHfVn" %::CC%% 	 r   c                 p    | j                   j                  |      }| j                  j                  |      S )N)name)r   :DatamigrationProjectsLocationsConnectionProfilesGetRequestr    Get)r$   r  get_reqs      r   _GetExistingConnectionProfilez6ConnectionProfilesClient._GetExistingConnectionProfile  s6    mmVV W G ==W%%r   c                 L   t        j                  |      }t        j                  |      }| j                  j                  j
                  }t        j                  |||j                        j                  ||j                        }|j                  r|j                  |_	        yy)z)Updates labels of the connection profile.)	additionssubtractionsclearN)r   GetUpdateLabelsDictFromArgsGetRemoveLabelsListFromArgsr   r  r^  Diffclear_labelsApplyrU  needs_update)r$   r   rZ   
add_labelsremove_labels
value_typeupdate_results          r   _UpdateLabelsz&ConnectionProfilesClient._UpdateLabels  s    88>J;;DAM00<<J$$" eJ*112	 
 !!"/"6"6 "r   c                    g }|j                  d      r"|j                  |_        |j                  d       |j                  | j                  |||       n| j                         r |j                  | j                  |||       nk| j                         r |j                  | j                  |||       n;| j                         r |j                  | j                  |||       nt        d      | j!                  ||       ||fS )z>Returns updated connection profile and list of updated fields.r  r  zThe requested connection profile does not contain a MySQL, PostgreSQL or Oracle object. Currently only MySQL, PostgreSQL and Oracle connection profiles are supported.)ro   r  r  r   r7   r   rN   r8   r   rQ   r:   r   rS   r;   r   r   r  r   s       r   _GetUpdatedConnectionProfilez5ConnectionProfilesClient._GetUpdatedConnectionProfile  s   M''+'8'8$=)+
(();)-)68 
"
"
$

'
'
3
--.@$.;=				$6$=$=$I
))
dM 
	 	 	"'9'C'C'O
,,
dM 47  	)40},,r   c                     | j                  ||       | j                  |||      }t        j                         }| j                  j
                  } |||||      }| j                  j                  |      S )a  Creates a connection profile.

    Args:
      parent_ref: a Resource reference to a parent
        datamigration.projects.locations resource for this connection profile.
      connection_profile_id: str, the name of the resource to create.
      cp_type: str, the type of the connection profile ('MYSQL', 'POSTGRESQL',
        ''
      args: argparse.Namespace, The arguments that this command was invoked
        with.

    Returns:
      Operation: the operation for creating the connection profile.
    )connectionProfileconnectionProfileIdparent	requestId)r\   r  r   GenerateRequestIdr   =DatamigrationProjectsLocationsConnectionProfilesCreateRequestr    Create)	r$   
parent_refrj  r[   rZ   r   
request_idcreate_req_type
create_reqs	            r   r  zConnectionProfilesClient.Create  s~     	tW%33GT4IK ++-JSS  !,1	J ==
++r   c                 D   | j                  |       | j                  |      }| j                  ||      \  }}t        j                         }| j
                  j                  } |||j                  dj                  |      |      }| j                  j                  |      S )a)  Updates a connection profile.

    Args:
      name: str, the reference of the connection profile to
          update.
      args: argparse.Namespace, The arguments that this command was
          invoked with.

    Returns:
      Operation: the operation for updating the connection profile.
    ,)r  r  
updateMaskr  )r\   r  r  r   r  r   <DatamigrationProjectsLocationsConnectionProfilesPatchRequestr  joinr    Patch)	r$   r  rZ   
current_cp
updated_cpr   r  update_req_type
update_reqs	            r   UpdatezConnectionProfilesClient.Update  s     	t33D9J $ A AD!J ++-JRR  !$__88M*	J ==z**r   r  returnc                     | j                  |      }t        j                         }| j                  j                  } |||j
                  dd|      }| j                  j                  |      S )zTest a connection profile.

    Args:
      name: str, the reference of the connection profile to test.

    Returns:
      Operation: the operation for testing the connection profile.
    r  T)r  r  r  validateOnlyr  )r  r   r  r   r  r  r    r  )r$   r  r  r  r  r  s         r   TestzConnectionProfilesClient.Test:  sj     33D9J++-JRR  !$__ J ==z**r   c                    | j                   j                  d||j                  d      r|j                  nd      }| j                  j
                  } ||j                         |j                  |j                  rdj                  |j                        nd      }t        j                  | j                  j                  ||j                  |j                  dd	
      S )zGet the list of connection profiles in a project.

    Args:
      project_id: The project ID to retrieve
      args: parsed command line arguments

    Returns:
      An iterator over all the matching connection profiles.
    z datamigration.projects.locationsregionr  )
projectsIdlocationsIdr  N)r  filterorderByconnectionProfilespageSize)servicerequestlimit
batch_sizers   batch_size_attribute)r"   r  r_   r  r   ;DatamigrationProjectsLocationsConnectionProfilesListRequestr   r  sort_byr  r   YieldFromListr   r   r  	page_size)r$   
project_idrZ   location_reflist_req_typelist_reqs         r   ListzConnectionProfilesClient.ListT  s     ''..*#'#;#;H#EDKK3 / PL 	QQ  ((*{{*.,,&DBH
 ##AAjj>>"') )r   c                 \    | j                   j                  |d      }|j                         S )zGet the URL string for a connection profile.

    Args:
      name: connection profile's full name.

    Returns:
      URL of the connection profile resource
    z3datamigration.projects.locations.connectionProfiles)
collection)r"   ParseRelativeNameSelfLink)r$   r  uris      r   GetUrizConnectionProfilesClient.GetUris  s4     


0
0H 1 JC <<>r   r   )Br   r   r   r   r&   r1   r>   rE   rI   rN   rQ   rS   rU   r\   rW   rX   rj   ri   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  r!  rD  rG  rJ  rn  ry  r  r   r  r  r  r  r  r  r  strr   r  r  r  r   r   r   r   r   %   sZ   :(*@ 

7777/		98tF


M2>
/
b;H*/
b&"PM67I*/ D'LRD
<JJ	N
5nF3Dj,!"F/"b*X&7-@,@+@+s +s +4)>r   r   )r   
__future__r   r   r   r`   typingr   apitools.base.pyr   )googlecloudsdk.api_lib.database_migrationr   googlecloudsdk.callioper	   r
   rc   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corecore_exceptionsErrorr   objectr   r   r   r   <module>r     sM    : &  ' 	  ' > ( E < =Oo.C.C O[v [r   