wojsamjan commented on a change in pull request #18945:
URL: https://github.com/apache/airflow/pull/18945#discussion_r728871371



##########
File path: airflow/providers/google/cloud/hooks/dataproc_metastore.py
##########
@@ -0,0 +1,673 @@
+#
+# 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.
+#
+"""This module contains a Google Cloud Dataproc Metastore hook."""
+
+from time import sleep
+from typing import Dict, Optional, Sequence, Tuple, Union
+
+from google.api_core.operation import Operation
+from google.api_core.retry import Retry, exponential_sleep_generator
+from google.cloud.metastore_v1 import DataprocMetastoreClient
+from google.cloud.metastore_v1.types import Backup, MetadataImport, Service
+from google.cloud.metastore_v1.types.metastore import DatabaseDumpSpec, Restore
+from google.protobuf.field_mask_pb2 import FieldMask
+
+from airflow.exceptions import AirflowException
+from airflow.providers.google.common.hooks.base_google import GoogleBaseHook
+
+
+class DataprocMetastoreHook(GoogleBaseHook):
+    """Hook for Google Cloud Dataproc Metastore APIs."""
+
+    def get_dataproc_metastore_client(self, region: Optional[str] = None) -> 
DataprocMetastoreClient:
+        """Returns DataprocMetastoreClient."""
+        client_options = None
+        if region and region != 'global':
+            client_options = {'api_endpoint': 
f'{region}-metastore.googleapis.com:443'}
+
+        return DataprocMetastoreClient(
+            credentials=self._get_credentials(), client_info=self.client_info, 
client_options=client_options
+        )
+
+    def wait_for_operation(self, operation: Operation):
+        """Waits for long-lasting operation to complete."""
+        for time_to_wait in exponential_sleep_generator(initial=10, 
maximum=120):
+            sleep(time_to_wait)
+            if operation.done():
+                break
+
+        error = operation.exception()
+        if error:
+            raise AirflowException(error)
+        return operation.result()
+
+    def create_backup(
+        self,
+        project_id: str,
+        region: str,
+        service_id: str,
+        backup: Backup,
+        backup_id: str,
+        request_id: Optional[str] = None,
+        retry: Optional[Retry] = None,
+        timeout: Optional[float] = None,
+        metadata: Optional[Sequence[Tuple[str, str]]] = None,
+    ):
+        """
+        Creates a new backup in a given project and location.
+
+        :param project_id: Required. The ID of the Google Cloud project that 
the service belongs to.
+        :type project_id: str
+        :param region: Required. The ID of the Google Cloud region that the 
service belongs to.
+        :type region: str
+        :param service_id:  Required. The ID of the metastore service, which 
is used as the final component of
+            the metastore service's name. This value must be between 2 and 63 
characters long inclusive, begin
+            with a letter, end with a letter or number, and consist of 
alpha-numeric ASCII characters or
+            hyphens.
+
+            This corresponds to the ``service_id`` field on the ``request`` 
instance; if ``request`` is
+            provided, this should not be set.
+        :type service_id: str
+        :param backup:  Required. The backup to create. The ``name`` field is 
ignored. The ID of the created
+            backup must be provided in the request's ``backup_id`` field.
+
+            This corresponds to the ``backup`` field on the ``request`` 
instance; if ``request`` is provided,
+            this should not be set.
+        :type backup: google.cloud.metastore_v1.types.Backup
+        :param backup_id:  Required. The ID of the backup, which is used as 
the final component of the
+            backup's name. This value must be between 1 and 64 characters 
long, begin with a letter, end with
+            a letter or number, and consist of alpha-numeric ASCII characters 
or hyphens.
+
+            This corresponds to the ``backup_id`` field on the ``request`` 
instance; if ``request`` is
+            provided, this should not be set.
+        :type backup_id: str
+        :param request_id: Optional. A unique id used to identify the request.
+        :type request_id: str
+        :param retry: Designation of what errors, if any, should be retried.
+        :type retry: google.api_core.retry.Retry
+        :param timeout: The timeout for this request.
+        :type timeout: float
+        :param metadata: Strings which should be sent along with the request 
as metadata.
+        :type metadata: Sequence[Tuple[str, str]]
+        """
+        parent = 
f'projects/{project_id}/locations/{region}/services/{service_id}'
+
+        client = self.get_dataproc_metastore_client(region=region)
+        result = client.create_backup(
+            request={
+                'parent': parent,
+                'backup': backup,
+                'backup_id': backup_id,
+                'request_id': request_id,
+            },
+            retry=retry,
+            timeout=timeout,
+            metadata=metadata,
+        )
+        return result
+
+    def create_metadata_import(

Review comment:
       Should I decorate another Hook´s methods for services and backup as well?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to