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 2020/06/09 16:37:39 UTC

[GitHub] [airflow] mik-laj commented on a change in pull request #8575: [AIRFLOW-6981] Move Google Cloud Build from Discovery API to Python Library

mik-laj commented on a change in pull request #8575:
URL: https://github.com/apache/airflow/pull/8575#discussion_r437198960



##########
File path: airflow/providers/google/cloud/hooks/cloud_build.py
##########
@@ -47,90 +47,528 @@ class CloudBuildHook(GoogleBaseHook):
     :type delegate_to: str
     """
 
-    _conn = None  # type: Optional[Any]
+    def __init__(self, gcp_conn_id: str = "google_cloud_default", delegate_to: Optional[str] = None) -> None:
+        super().__init__(gcp_conn_id, delegate_to)
+        self._client: Optional[CloudBuildClient] = None
+
+    def get_conn(self) -> CloudBuildClient:
+        """
+        Retrieves the connection to Google Cloud Build.
+
+        :return: Google Cloud Build client object.
+        :rtype: `google.cloud.devtools.cloudbuild_v1.CloudBuildClient`
+        """
+        if not self._client:
+            self._client = CloudBuildClient(credentials=self._get_credentials(), client_info=self.client_info)
+        return self._client
+
+    @GoogleBaseHook.fallback_to_default_project_id
+    def cancel_build(
+        self,
+        id_: str,
+        project_id: str,
+        retry: Optional[Retry] = None,
+        timeout: Optional[float] = None,
+        metadata: Optional[Sequence[Tuple[str, str]]] = None,
+    ) -> Build:
+        """
+        Cancels a build in progress.
+
+        :param id_: The ID of the build.
+        :type id_: str
+        :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: Optional[str]
+        :param retry: Optional, a retry object used  to retry requests. If `None` is specified, requests
+            will not be retried.
+        :type retry: Optional[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: Optional[float]
+        :param metadata: Optional, additional metadata that is provided to the method.
+        :type metadata: Optional[Sequence[Tuple[str, str]]]
+
+        :rtype: `google.cloud.devtools.cloudbuild_v1.types.Build`
+        """
+        if not project_id:
+            raise ValueError("The project_id should be set")
+
+        client = self.get_conn()
+        return client.cancel_build(
+            project_id=project_id, id_=id_, retry=retry, timeout=timeout, metadata=metadata
+        )
+
+    @GoogleBaseHook.fallback_to_default_project_id
+    def create_build(
+        self,
+        build: Union[Dict, Build],
+        project_id: str,
+        wait: bool = True,
+        retry: Optional[Retry] = None,
+        timeout: Optional[float] = None,
+        metadata: Optional[Sequence[Tuple[str, str]]] = None,
+    ) -> Build:
+        """
+        Starts a build with the specified configuration.
+
+        :param build: The build resource to create. If a dict is provided, it must be of the same form
+            as the protobuf message `google.cloud.devtools.cloudbuild_v1.types.Build`
+        :type build: Union[dict, `google.cloud.devtools.cloudbuild_v1.types.Build`]
+        :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: Optional[str]
+        :param wait: Optional, wait for operation to finish.
+        :type wait: Optional[bool]
+        :param retry: Optional, a retry object used  to retry requests. If `None` is specified, requests
+            will not be retried.
+        :type retry: Optional[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: Optional[float]
+        :param metadata: Optional, additional metadata that is provided to the method.
+        :type metadata: Optional[Sequence[Tuple[str, str]]]
+
+        :rtype: `google.cloud.devtools.cloudbuild_v1.types.Build`
+        """
+        if not project_id:
+            raise ValueError("The project_id should be set")
+
+        client = self.get_conn()
+        operation = client.create_build(
+            project_id=project_id, build=build, retry=retry, timeout=timeout, metadata=metadata
+        )
+
+        operation_dict = MessageToDict(operation)
+        try:
+            id_ = operation_dict["metadata"]["build"]["id"]
+        except Exception:
+            raise AirflowException("Could not retrieve Build ID from Operation.")
 
-    def __init__(
+        if wait:
+            return self._wait_for_operation_to_complete(
+                func=self.get_build,
+                id_=id_,  # type: ignore
+                project_id=project_id  # type: ignore
+            )
+        else:
+            return self.get_build(id_=id_, project_id=project_id)

Review comment:
       ```suggestion
           if wait:
               self._wait_for_operation_to_complete(
                   func=self.get_build,
                   id_=id_,  # type: ignore
                   project_id=project_id  # type: ignore
               )
           return self.get_build(id_=id_, project_id=project_id)
   ```
   This solve issue. - `AttributeError: 'dict' object has no attribute 'DESCRIPTOR'`




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