Source code for airflow.providers.google.cloud.hooks.cloud_run
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsimportitertoolsfromtypingimportTYPE_CHECKING,Any,Iterable,Sequencefromgoogle.cloud.run_v2import(CreateJobRequest,DeleteJobRequest,GetJobRequest,Job,JobsAsyncClient,JobsClient,ListJobsRequest,RunJobRequest,UpdateJobRequest,)fromgoogle.longrunningimportoperations_pb2fromairflow.exceptionsimportAirflowExceptionfromairflow.providers.google.common.constsimportCLIENT_INFOfromairflow.providers.google.common.hooks.base_googleimportPROVIDE_PROJECT_ID,GoogleBaseHookifTYPE_CHECKING:fromgoogle.api_coreimportoperationfromgoogle.cloud.run_v2.services.jobsimportpagers
[docs]classCloudRunHook(GoogleBaseHook):""" Hook for the Google Cloud Run service. :param gcp_conn_id: The connection ID to use when fetching connection info. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account. """def__init__(self,gcp_conn_id:str="google_cloud_default",impersonation_chain:str|Sequence[str]|None=None,)->None:super().__init__(gcp_conn_id=gcp_conn_id,impersonation_chain=impersonation_chain)self._client:JobsClient|None=None
[docs]defget_conn(self):""" Retrieves connection to Cloud Run. :return: Cloud Run Jobs client object. """ifself._clientisNone:self._client=JobsClient(credentials=self.get_credentials(),client_info=CLIENT_INFO)returnself._client
[docs]deflist_jobs(self,region:str,project_id:str=PROVIDE_PROJECT_ID,show_deleted:bool=False,limit:int|None=None,)->Iterable[Job]:iflimitisnotNoneandlimit<0:raiseAirflowException("The limit for the list jobs request should be greater or equal to zero")list_jobs_request:ListJobsRequest=ListJobsRequest(parent=f"projects/{project_id}/locations/{region}",show_deleted=show_deleted)jobs:pagers.ListJobsPager=self.get_conn().list_jobs(request=list_jobs_request)returnlist(itertools.islice(jobs,limit))
[docs]classCloudRunAsyncHook(GoogleBaseHook):""" Async hook for the Google Cloud Run service. :param gcp_conn_id: The connection ID to use when fetching connection info. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account. """def__init__(self,gcp_conn_id:str="google_cloud_default",impersonation_chain:str|Sequence[str]|None=None,):self._client:JobsAsyncClient|None=Nonesuper().__init__(gcp_conn_id=gcp_conn_id,impersonation_chain=impersonation_chain)