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/05/08 06:19:20 UTC

[GitHub] [airflow] milton0825 commented on a change in pull request #5251: [AIRFLOW-4135] Add Google Cloud Build operator and hook

milton0825 commented on a change in pull request #5251: [AIRFLOW-4135] Add Google Cloud Build operator and hook
URL: https://github.com/apache/airflow/pull/5251#discussion_r281926818
 
 

 ##########
 File path: airflow/contrib/hooks/gcp_cloud_build_hook.py
 ##########
 @@ -0,0 +1,134 @@
+# -*- 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.
+
+import time
+
+from googleapiclient.discovery import build
+
+from airflow import AirflowException
+from airflow.contrib.hooks.gcp_api_base_hook import GoogleCloudBaseHook
+
+# Number of retries - used by googleapiclient method calls to perform retries
+# For requests that are "retriable"
+NUM_RETRIES = 5
+
+# Time to sleep between active checks of the operation results
+TIME_TO_SLEEP_IN_SECONDS = 5
+
+
+# noinspection PyAbstractClass
+class CloudBuildHook(GoogleCloudBaseHook):
+    """
+    Hook for the Google Cloud Build APIs.
+
+    All the methods in the hook where project_id is used must be called with
+    keyword arguments rather than positional.
+
+    :param api_version: API version used (for example v1 or v1beta1).
+    :type api_version: str
+    :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
+    """
+
+    _conn = None
+
+    def __init__(self, api_version="v1", gcp_conn_id="google_cloud_default", delegate_to=None):
+        super(CloudBuildHook, self).__init__(gcp_conn_id, delegate_to)
+        self.api_version = api_version
+
+    def get_conn(self):
+        """
+        Retrieves the connection to Cloud Functions.
+
+        :return: Google Cloud Function services object.
+        :rtype: dict
+        """
+        if not self._conn:
+            http_authorized = self._authorize()
+            self._conn = build("cloudbuild", self.api_version, http=http_authorized, cache_discovery=False)
+        return self._conn
+
+    @GoogleCloudBaseHook.fallback_to_default_project_id
+    def create_build(self, body, project_id=None):
+        """
+        Starts a build with the specified configuration.
+
+        :param body: The request body.
+            See: https://cloud.google.com/cloud-build/docs/api/reference/rest/Shared.Types/Build
+        :type body: dict
+        :param project_id: Optional, Google Cloud Project project_id where the function belongs.
+            If set to None or missing, the default project_id from the GCP connection is used.
+        :type project_id: str
+        :return: None
+        """
+        service = self.get_conn()
+
+        # Create build
+        response = (
+            service.projects()
+            .builds()
+            .create(projectId=project_id, body=body)
+            .execute(num_retries=NUM_RETRIES)
 
 Review comment:
   Should we allow users to override the default `NUM_RETRIES`?

----------------------------------------------------------------
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