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/01/12 20:36:08 UTC

[GitHub] potiuk commented on a change in pull request #4500: [AIRFLOW-3681] All GCP operators have now optional GCP Project ID

potiuk commented on a change in pull request #4500: [AIRFLOW-3681] All GCP operators have now optional GCP Project ID
URL: https://github.com/apache/airflow/pull/4500#discussion_r247325348
 
 

 ##########
 File path: airflow/contrib/hooks/gcp_api_base_hook.py
 ##########
 @@ -144,11 +145,46 @@ def _get_field(self, f, default=None):
         key_path, etc. They get formatted as shown below.
         """
         long_f = 'extra__google_cloud_platform__{}'.format(f)
-        if long_f in self.extras:
+        if hasattr(self, 'extras') and long_f in self.extras:
             return self.extras[long_f]
         else:
             return default
 
     @property
     def project_id(self):
         return self._get_field('project')
+
+    def fallback_to_default_project_id(func):
+        """
+        Decorator that provides fallback for Google Cloud Platform project id. If
+        the project is None it will be replaced with the project_id from the
+        service account the Hook is authenticated with. Project id can be specified
+        either via project_id kwarg or via first parameter in positional args.
+        :param func: function to wrap
+        :return: result of the function call
+        """
+        @functools.wraps(func)
+        def inner_wrapper(self, *args, **kwargs):
+            if 'project_id' in kwargs:
+                kwargs['project_id'] = self._get_project_id(kwargs['project_id'])
+            else:
+                # assume project_id is the first non-keyword parameter if
+                # no project_id in keyword params
+                largs = list(args)
+                largs[0] = self._get_project_id(largs[0])
+                args = tuple(largs)
+                self.log.warning("%s", args)
+            return func(self, *args, **kwargs)
+        return inner_wrapper
+
+    fallback_to_default_project_id = staticmethod(fallback_to_default_project_id)
+
+    def _get_project_id(self, project_id):
+        """
+        In case project_id is None, overrides it with default project_id from
+        the service account that is authorized.
+        :param project_id: project id to
 
 Review comment:
   OK

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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