
    h1                        d Z ddl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 i Zej8                  j:                  j<                  ej>                  jA                  dddd      ej8                  jB                  j<                  dej8                  jD                  j<                  diZ#ejH                  jJ                  dejH                  jL                  dejH                  jN                  diZ(dZ)dZ* G d dejV                        Z, G d dejV                        Z-d Z.d)d Z/d! Z0	 d)d"Z1d# Z2d*d$Z3d% Z4d& Z5 G d' d(ejl                        Z7y)+z9Flink command library functions for the Flink cli binary.    N)parse)transfer)storage_api)storage_util)base)
exceptions)requests)java)binary_operations)config)	resources)
transports)	platformsz	%APPDATA%googleflinkzconfig.yamlz.~/Library/Preferences/google/flink/config.yamlz"~/.config/google/flink/config.yamlv1alphav1betav1zCould not locate managed flink client executable [{binary}] on the system PATH. Please ensure gcloud managed-flink-client component is properly installed. See https://cloud.google.com/sdk/docs/components for more details.i  0 c                       e Zd ZdZy)FileUploadErrorz*Exception raised when a file upload fails.N__name__
__module____qualname____doc__     =lib/googlecloudsdk/command_lib/managed_flink/flink_backend.pyr   r   B   s    2r   r   c                       e Zd ZdZy)FileDownloadErrorz,Exception raised when a file download fails.Nr   r   r   r   r    r    F   s    4r   r    c                      t         j                  j                  t        j                         j
                  dddd      S )zGet flink python jar location.platformmanaged-flink-clientlibzflink-python-1.19.0.jarospathjoinr   Pathssdk_rootr   r   r   DummyJarr+   J   s2    	lln
 r   c           	         |xs t        j                         }t               }| D ]x  }t        j                  j                  |      }t        j                  j                  ||      }t        j                  j                  |      }	 |j                  ||       |||<   z |S # t        j                  $ r0}	t        dj                  dj                  |       ||	            d}	~	ww xY w)zDUploads a list of files passed as strings to a Cloud Storage bucket.z(Failed to upload file ["{}"] to "{}": {},N)r   StorageClientdictr&   r'   basenamer(   r   ObjectReferenceFromUrlCopyFileToGCSr   BadFileExceptionr   format)
filesdestinationstorage_clientclientdestinationsfile_to_upload	file_namedest_urldest_objectes
             r   Uploadr@   U   s    8[668&,n  0Iww||K3H..66x@K>;7%-l>"  
 && 
4
;
;hhuo{A s   B""C%5+C  C%c                    	 t        j                  |       }|j
                  j                  d      }|D cg c]  }|s|	 }}|j                  r|j                  g|z   }t        |      dk  s|d   j                  d      s%t        j                  ddj	                  |             dj                  |dd       }|j                  dd	      j                  d
d      j                  dd      }|t        j                  j                  d|d   |d   |d   |      fS #  t        j                  ddj	                  |             xY wc c}w )zCreates a registry from an artifact URI.

  Args:
    artifact_uri:
      ar://<project>/<location>/<repository>/<file/path/version/file.jar>.

  Returns:
    Jar file name, The registry resource.
  z
JAR|PY|SQLz|Artifact URI [{0}] is invalid. Must be in the format of ar://<project>/<location>/<repository>/<file/path/version/file.jar>./   z.jar   Nz%2F+z%2B^z%5Ez6artifactregistry.projects.locations.repositories.filesr         )
projectsIdlocationsIdrepositoriesIdfilesId)r   urlparser   InvalidArgumentExceptionr5   r'   splitnetloclenendswithr(   replacer   REGISTRYCreate)artifact_uri
parsed_url
split_pathr'   cleaned_split_pathjar_filecleaned_jar_files          r   CreateRegistryFromArtifactUrir]   i   sd   -J $$S)*)3<t<$++,/AA	q (:2(>(G(G(O

-
-	P			  XX(,-(sE"**36>>sEJ  
9%%,,>#A&$Q''* -  
 /

-
-	P			  =s   D EE'Ec                    |xs t        j                         }t        j                         }|j                  |      }t        j
                  j                  | dt              }t        j                  d      |_
        	 |j                  j                  ||       	 |j                  j!                          y# t        $ r}t        dj                  |            d}~ww xY w# |j                  j!                          w xY w)z3Downloads a JAR file from Google Artifact Registry.)nameT)	chunksizeN)response_encoding)downloadz1Failed to download JAR from Artifact Registry: {})r	   	GetClientGetMessagesAArtifactregistryProjectsLocationsRepositoriesFilesDownloadRequestr   DownloadFromFileDEFAULT_CHUNK_SIZEr   GetApitoolsTransport
bytes_http%projects_locations_repositories_files	Exceptionr    r5   streamclose)	dest_pathartifact_jar_pathartifact_clientr9   messagesrequestdr?   s           r   DownloadJarFromArtifactRegistryru      s     2h002&!!#( VV W '   D<N O!004H!,
0099'A9N HHNN 
 
;BB1E 
 HHNNs$   B: :	C"CC""C% %Dc                     t         j                  j                  | d      }t        j                  |j                         t        j                         j                  |j                         y )NT)r   r1   r2   ValidateBucketUrlbucketr   r.   	GetBucket)staging_locationdests     r   CheckStagingLocationr|      sH    		%	%	-	-.>	E$  -''4r   c                     t        j                  t        j                        }|j	                  t
               | r|j	                  |        |r|D ]  }|j                  |d        |S )zDHelper function to add our environment variables to the environment.N)copydeepcopyr&   environupdateDEFAULT_ENV_ARGSpop)
extra_varsexclude_varsenvvars       r   GetEnvArgsForCommandr      sP    bjj!#**JJz	ggc4 	*r   c                      t         j                  j                  t        j                         j
                  dddd      S )z%Get the platform executable location.r"   r#   binr%   r   r   r   PlatformExecutabler      s2    	lln
 r   c                 f   | dk(  rd|rt        j                  dd      |st        j                  dd      |st        j                  dd      ||kD  rt        j                  dd      y|st        j                  dd	      |rt        j                  dd
      |rt        j                  dd      y)z#Validate autotuning configurations.elasticparallelismz8Parallelism must NOT be set for elastic autotuning mode.zmin-parallelismz8Min parallelism must be set for elastic autotuning mode.zmax-parallelismz8Max parallelism must be set for elastic autotuning mode.z>Min parallelism must be less than or equal to max parallelism.zMParallelism must be set to a value of 1 or greater for fixed autotuning mode.z:Min parallelism must NOT be set for fixed autotuning mode.z:Max parallelism must NOT be set for fixed autotuning mode.N)r   rO   )autotuning_modemin_parallelismmax_parallelismr   s       r   ValidateAutotuningr      s     	!//

D  //

D  //

D  (//

J  ) //
 
 //

F  //

F  r   c                   p     e Zd ZdZdZ fdZdej                  j                  dddddddddddfdZ	 xZ
S )FlinkClientWrapperz$Wrapper for the Flink client binary.Nc                 N   dt         j                  d      i}t        t        |   dd|d| t        j                  dd      | _        t        j                  j                  | j                        s2t               }t        j                  j                  |      r|| _        y y y )NMISSING_EXECr#   )binary)r   custom_errorszManaged Flink Client   r   )MISSING_BINARYr5   superr   __init__r
   RequireJavaInstalled
_java_pathr&   r'   exists_executabler   )selfkwargsr   component_executable	__class__s       r   r   zFlinkClientWrapper.__init__   s    --5K-LM 

d, %]FL //0FKDO
 77>>$**+/1	,	-/ 
. ,r   localc                    |dk7  r$t        j                  dj                  |            t               }|
r |j	                  dj                  |
             |r |j	                  dj                  |             |r |j	                  dj                  |             |	r |j	                  dj                  |	             |r |j	                  dj                  |             |sg }t               }|D ]6  }|j                  d      r|j	                  |       &|j	                  |       8 |d	k(  r[g }|r)|D ]$  }|j	                  d
       |j	                  |       & |ddj                  |      dj                  |      d|gz   |z   |z   S |dk(  rg }|r/|j	                  dj                  dj                  |                   |j                  d      d   }dj                  |      g}|r)|D ]$  }|j	                  d       |j	                  |       & |d|g|z   dj                  |      dj                  |      dj                  |      dj                  |      dj                  |      gz   |z   |z   d|gz   |z   S g }|rd|g}g }|r/|j	                  dj                  dj                  |                   |d|g|z   |z   dj                  |      dj                  |      gz   |z   |gz   |z   S )z+Parses the arguments for the given command.runz%Invalid operation [{}] for Flink CLI.z-Dgcloud.network={0}z-Dgcloud.subnetwork={0}z-Dgcloud.region={0}z-Dgcloud.deployment={0}z-Dgcloud.job.display-name={0}z-Dsqlz--jarz-Dexecution.target=gcloudz-Dgcloud.output-path={0}z!-Dgcloud.api.staging-location={0}z--filepythonz-Dgcloud.pipeline.jars={0}r-   rB   rD   z-Dpython.archives={0}z--targetz*-Dpython.client.executable={0}/bin/python3z#-Dpython.executable={0}/bin/python3z5-Dpython.pythonpath={0}/lib/python3.10/site-packages/z--pythonz--class)r   InvalidOperationForBinaryr5   listappend
startswithr(   rP   )r   commandjob_typejarrz   temp_dirtargetrelease_tracklocation
deploymentnetwork
subnetworkr_   
extra_jarsmanaged_kafka_clusters
main_class
extra_argsextra_archivespython_venvr   argsjob_argsargudfsj
env_folderarchivesarchive	class_args                                r   _ParseArgsForCommandz'FlinkClientWrapper._ParseArgsForCommand  sL   0 %77
1
8
8
A  6D
kk(//89
kk+22:>?
kk'..x89
kk+22:>?
kk188>? jvH		C	  5d	A
++g

++a. 
 )(//91889IJ  	 
X	d	0778LMN$$S)"-j)00=>h	%G
//#

//'
" &
 
  )//91889IJ:AA*M3:::FELL   !$ #* +2 i	
+	d	0778LMN J
' )//91889IJ  
 r   )r   r   r   r   r   r   r   ReleaseTrackALPHAr   __classcell__)r   s   @r   r   r      sK    ,*00 %%++!'Fr   r   )N)NN)8r   r~   r&   urllibr   apitools.base.pyr   googlecloudsdk.api_lib.storager   r   googlecloudsdk.callioper   r   $googlecloudsdk.command_lib.artifactsr	   googlecloudsdk.command_lib.utilr
   &googlecloudsdk.command_lib.util.anthosr   googlecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.credentialsr   googlecloudsdk.core.utilr   r   OperatingSystemWINDOWSidr'   r(   MACOSXLINUXDEFAULT_CONFIG_LOCATIONr   r   BETAGA_RELEASE_TRACK_TO_VERSIONr   rh   Errorr   r    r+   r@   r]   ru   r|   r   r   r   BinaryBackedOperationr   r   r   r   <module>r      sa   @  	  % 6 7 ( . 9 0 D & = ) 6 .  %%(("'',,Xw+ $$''8##&&(L  	YH$   % 3o++ 35-- 5()Z 3745	)X\*@@ \r   