Source code for airflow.providers.google.cloud.hooks.cloud_composer
## 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__importannotationsfromtypingimportSequencefromgoogle.api_core.client_optionsimportClientOptionsfromgoogle.api_core.gapic_v1.methodimportDEFAULT,_MethodDefaultfromgoogle.api_core.operationimportOperationfromgoogle.api_core.operation_asyncimportAsyncOperationfromgoogle.api_core.retryimportRetryfromgoogle.cloud.orchestration.airflow.service_v1import(EnvironmentsAsyncClient,EnvironmentsClient,ImageVersionsClient,)fromgoogle.cloud.orchestration.airflow.service_v1.services.environments.pagersimportListEnvironmentsPagerfromgoogle.cloud.orchestration.airflow.service_v1.services.image_versions.pagersimport(ListImageVersionsPager,)fromgoogle.cloud.orchestration.airflow.service_v1.typesimportEnvironmentfromgoogle.protobuf.field_mask_pb2importFieldMaskfromairflowimportAirflowExceptionfromairflow.providers.google.common.constsimportCLIENT_INFOfromairflow.providers.google.common.hooks.base_googleimportGoogleBaseHook
[docs]classCloudComposerHook(GoogleBaseHook):"""Hook for Google Cloud Composer APIs."""
def__init__(self,**kwargs):ifkwargs.get("delegate_to")isnotNone:raiseRuntimeError("The `delegate_to` parameter has been deprecated before and finally removed in this version"" of Google Provider. You MUST convert it to `impersonate_chain`")super().__init__(**kwargs)
[docs]defget_environment_client(self)->EnvironmentsClient:"""Retrieves client library object that allow access Environments service."""returnEnvironmentsClient(credentials=self.get_credentials(),client_info=CLIENT_INFO,client_options=self.client_options,)
[docs]defget_image_versions_client(self)->ImageVersionsClient:"""Retrieves client library object that allow access Image Versions service."""returnImageVersionsClient(credentials=self.get_credentials(),client_info=CLIENT_INFO,client_options=self.client_options,)
[docs]defwait_for_operation(self,operation:Operation,timeout:float|None=None):"""Waits for long-lasting operation to complete."""try:returnoperation.result(timeout=timeout)exceptException:error=operation.exception(timeout=timeout)raiseAirflowException(error)
[docs]defcreate_environment(self,project_id:str,region:str,environment:Environment|dict,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->Operation:""" Create a new environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment: The environment to create. This corresponds to the ``environment`` field on the ``request`` instance; if ``request`` is provided, this should not be set. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()result=client.create_environment(request={"parent":self.get_parent(project_id,region),"environment":environment},retry=retry,timeout=timeout,metadata=metadata,)returnresult
@GoogleBaseHook.fallback_to_default_project_id
[docs]defdelete_environment(self,project_id:str,region:str,environment_id:str,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->Operation:""" Delete an environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()name=self.get_environment_name(project_id,region,environment_id)result=client.delete_environment(request={"name":name},retry=retry,timeout=timeout,metadata=metadata)returnresult
@GoogleBaseHook.fallback_to_default_project_id
[docs]defget_environment(self,project_id:str,region:str,environment_id:str,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->Environment:""" Get an existing environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()result=client.get_environment(request={"name":self.get_environment_name(project_id,region,environment_id)},retry=retry,timeout=timeout,metadata=metadata,)returnresult
@GoogleBaseHook.fallback_to_default_project_id
[docs]deflist_environments(self,project_id:str,region:str,page_size:int|None=None,page_token:str|None=None,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->ListEnvironmentsPager:""" List environments. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param page_size: The maximum number of environments to return. :param page_token: The next_page_token value returned from a previous List request, if any. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()result=client.list_environments(request={"parent":self.get_parent(project_id,region),"page_size":page_size,"page_token":page_token,},retry=retry,timeout=timeout,metadata=metadata,)returnresult
@GoogleBaseHook.fallback_to_default_project_id
[docs]defupdate_environment(self,project_id:str,region:str,environment_id:str,environment:Environment|dict,update_mask:dict|FieldMask,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->Operation:r""" Update an environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. :param environment: A patch environment. Fields specified by the ``updateMask`` will be copied from the patch environment into the environment under update. This corresponds to the ``environment`` field on the ``request`` instance; if ``request`` is provided, this should not be set. :param update_mask: Required. A comma-separated list of paths, relative to ``Environment``, of fields to update. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.protobuf.field_mask_pb2.FieldMask` :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()name=self.get_environment_name(project_id,region,environment_id)result=client.update_environment(request={"name":name,"environment":environment,"update_mask":update_mask},retry=retry,timeout=timeout,metadata=metadata,)returnresult
@GoogleBaseHook.fallback_to_default_project_id
[docs]deflist_image_versions(self,project_id:str,region:str,page_size:int|None=None,page_token:str|None=None,include_past_releases:bool=False,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->ListImageVersionsPager:""" List ImageVersions for provided location. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param page_size: The maximum number of environments to return. :param page_token: The next_page_token value returned from a previous List request, if any. :param include_past_releases: Flag to include past releases :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_image_versions_client()result=client.list_image_versions(request={"parent":self.get_parent(project_id,region),"page_size":page_size,"page_token":page_token,"include_past_releases":include_past_releases,},retry=retry,timeout=timeout,metadata=metadata,)returnresult
[docs]classCloudComposerAsyncHook(GoogleBaseHook):"""Hook for Google Cloud Composer async APIs."""def__init__(self,**kwargs):ifkwargs.get("delegate_to")isnotNone:raiseRuntimeError("The `delegate_to` parameter has been deprecated before and finally removed in this version"" of Google Provider. You MUST convert it to `impersonate_chain`")super().__init__(**kwargs)
[docs]asyncdefcreate_environment(self,project_id:str,region:str,environment:Environment|dict,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->AsyncOperation:""" Create a new environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment: The environment to create. This corresponds to the ``environment`` field on the ``request`` instance; if ``request`` is provided, this should not be set. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()returnawaitclient.create_environment(request={"parent":self.get_parent(project_id,region),"environment":environment},retry=retry,timeout=timeout,metadata=metadata,)
@GoogleBaseHook.fallback_to_default_project_id
[docs]asyncdefdelete_environment(self,project_id:str,region:str,environment_id:str,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->AsyncOperation:""" Delete an environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()name=self.get_environment_name(project_id,region,environment_id)returnawaitclient.delete_environment(request={"name":name},retry=retry,timeout=timeout,metadata=metadata)
@GoogleBaseHook.fallback_to_default_project_id
[docs]asyncdefupdate_environment(self,project_id:str,region:str,environment_id:str,environment:Environment|dict,update_mask:dict|FieldMask,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->AsyncOperation:r""" Update an environment. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. :param environment: A patch environment. Fields specified by the ``updateMask`` will be copied from the patch environment into the environment under update. This corresponds to the ``environment`` field on the ``request`` instance; if ``request`` is provided, this should not be set. :param update_mask: Required. A comma-separated list of paths, relative to ``Environment``, of fields to update. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.protobuf.field_mask_pb2.FieldMask` :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. """client=self.get_environment_client()name=self.get_environment_name(project_id,region,environment_id)returnawaitclient.update_environment(request={"name":name,"environment":environment,"update_mask":update_mask},retry=retry,timeout=timeout,metadata=metadata,)