You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/06/10 13:30:52 UTC
[GitHub] [airflow] mik-laj commented on a change in pull request #5402:
[AIRFLOW-4746] Implement GCP Cloud Tasks' Hook and Operators
mik-laj commented on a change in pull request #5402: [AIRFLOW-4746] Implement GCP Cloud Tasks' Hook and Operators
URL: https://github.com/apache/airflow/pull/5402#discussion_r292010982
##########
File path: airflow/contrib/hooks/gcp_tasks_hook.py
##########
@@ -0,0 +1,690 @@
+# -*- coding: utf-8 -*-
+#
+# 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 airflow import AirflowException
+from airflow.contrib.hooks.gcp_api_base_hook import GoogleCloudBaseHook
+from google.cloud.tasks_v2 import CloudTasksClient
+from google.cloud.tasks_v2.types import Queue, Task
+
+
+class CloudTasksHook(GoogleCloudBaseHook):
+ """
+ Hook for Google Cloud Tasks APIs. Cloud Tasks allows developers to manage
+ the execution of background work in their applications.
+
+ All the methods in the hook where project_id is used must be called with
+ keyword arguments rather than positional.
+
+ :param gcp_conn_id: The connection ID to use when fetching connection info.
+ :type gcp_conn_id: str
+ :param delegate_to: The account to impersonate, if any.
+ For this to work, the service account making the request must have
+ domain-wide delegation enabled.
+ :type delegate_to: str
+ """
+
+ _client = None
+
+ def __init__(self, gcp_conn_id="google_cloud_default", delegate_to=None):
+ super().__init__(gcp_conn_id, delegate_to)
+
+ def get_conn(self):
+ """
+ Provides a client for interacting with the Cloud Tasks API.
+
+ :return: google.cloud.tasks_v2.CloudTasksClient
+ :rtype: object
+ """
+ if not self._client:
+ self._client = CloudTasksClient(credentials=self._get_credentials())
+ return self._client
+
+ @GoogleCloudBaseHook.catch_http_exception
+ @GoogleCloudBaseHook.fallback_to_default_project_id
+ def create_queue(
+ self,
+ location,
+ task_queue,
+ project_id=None,
+ queue_name=None,
+ retry=None,
+ timeout=None,
+ metadata=None,
+ ):
+ """
+ Creates a queue in Cloud Tasks.
+
+ :param location: The location name in which the queue will be created.
+ :type location: str
+ :param task_queue: The task queue to create.
+ Queue's name cannot be the same as an existing queue.
+ If a dict is provided, it must be of the same form as the protobuf message Queue.
+ :type task_queue: dict or class google.cloud.tasks_v2.types.Queue
+ :param project_id: (Optional) The ID of the GCP project that owns the Cloud Tasks.
+ If set to None or missing, the default project_id from the GCP connection is used.
+ :type project_id: str
+ :param queue_name: (Optional) The queue's name.
+ If provided, it will be used to construct the full queue path.
+ :type queue_name: str
+ :param retry: (Optional) A retry object used to retry requests.
+ If None is specified, requests will not be retried.
+ :type retry: google.api_core.retry.Retry
+ :param timeout: (Optional) The amount of time, in seconds, to wait for the request
+ to complete. Note that if retry is specified, the timeout applies to each
+ individual attempt.
+ :type timeout: float
+ :param metadata: (Optional) Additional metadata that is provided to the method.
+ :type metadata: sequence[tuple[str, str]]]
+ :rtype: google.cloud.tasks_v2.types.Queue
+ """
+
+ client = self.get_conn()
+
+ if queue_name:
+ full_queue_name = CloudTasksClient.queue_path(project_id, location, queue_name)
+ if type(task_queue) is Queue:
Review comment:
```suggestion
if isistance(task_queue, Queue) is :
```
Pylint will be sad here.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services