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/29 15:14:35 UTC

[GitHub] [airflow] turbaszek opened a new pull request #9566: Fix using .json template extension in GMP operators

turbaszek opened a new pull request #9566:
URL: https://github.com/apache/airflow/pull/9566


   #9541
   
   ---
   Make sure to mark the boxes below before creating PR: [x]
   
   - [x] Description above provides context of the change
   - [x] Unit tests coverage for changes (not needed for documentation changes)
   - [x] Target Github ISSUE in description if exists
   - [x] Commits follow "[How to write a good git commit message](http://chris.beams.io/posts/git-commit/)"
   - [x] Relevant documentation is updated including usage instructions.
   - [x] I will engage committers as explained in [Contribution Workflow Example](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#contribution-workflow-example).
   
   ---
   In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/master/UPDATING.md).
   Read the [Pull Request Guidelines](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#pull-request-guidelines) for more information.
   


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



[GitHub] [airflow] mik-laj commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447136211



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       You can because the templates are rendered recursively.
   https://github.com/apache/airflow/blob/1c48ffbe25c3e304660b7e75a49e88bd114dde46/airflow/models/baseoperator.py#L872-L929
   The problem only occurs when you use Jinja templates to generate new structures e.g. create new array elements.




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



[GitHub] [airflow] mik-laj commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447096913



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       You can create a task as below to create a task based on a JSON file from another source e.g. variable.
   ```
   create_report = GoogleCampaignManagerInsertReportOperator(
           task_id="create_report",
           gcp_conn_id=self.config.get("gcp_connection_id"),
           profile_id=self.config.get("cm_profile_id"),
           report=json.loads("""
               {
                  'name': '{{ params.title }}',
                  'accountId': '{{ params.account }}',
                  'fileName': '{{ params.file_name }}',
                  'type': 'STANDARD',
                  'criteria': {
                  ..."""),
           params=report_params,
           dag=self.dag)
   ```
   Otherwise we have ambiguity. The str type means the path to the file in one case and the contents of the file in other case.




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



[GitHub] [airflow] turbaszek edited a comment on pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
turbaszek edited a comment on pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#issuecomment-651194055


   @olchas @efolgar would you mind taking a look?


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



[GitHub] [airflow] turbaszek commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
turbaszek commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447092670



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       According to documentation user should provide a dictionary so I would say that passing `str` don't have to be supported. 

##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       According to documentation user should provide a dictionary so I would say that passing `str` doesn't have to be supported. 




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



[GitHub] [airflow] mik-laj edited a comment on pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
mik-laj edited a comment on pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#issuecomment-651813511


   Can you add unit test and some documentation?


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



[GitHub] [airflow] turbaszek merged pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
turbaszek merged pull request #9566:
URL: https://github.com/apache/airflow/pull/9566


   


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



[GitHub] [airflow] efolgar commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
efolgar commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447075206



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       This would work for templates passed in with a *.json extension, but doesn't cover the case where report is passed as a JSON string template.  For example:
   
   ```
   create_report = GoogleCampaignManagerInsertReportOperator(
           task_id="create_report",
           gcp_conn_id=self.config.get("gcp_connection_id"),
           profile_id=self.config.get("cm_profile_id"),
           report="""
               {
                  'name': '{{ params.title }}',
                  'accountId': '{{ params.account }}',
                  'fileName': '{{ params.file_name }}',
                  'type': 'STANDARD',
                  'criteria': {
                  ...""",
           params=report_params,
           dag=self.dag)
   ```
   
   Safest approach may be to parse the json string within the execute method.

##########
File path: airflow/providers/google/marketing_platform/operators/display_video.py
##########
@@ -75,6 +76,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:

Review comment:
       Issue should also be fixed in the GoogleSearchAdsInsertReportOperator.




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



[GitHub] [airflow] mik-laj commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447096913



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       You can create a task as below to create a task based on a JSON file from another source e.g. variable.
   ```
   create_report = GoogleCampaignManagerInsertReportOperator(
           task_id="create_report",
           gcp_conn_id=self.config.get("gcp_connection_id"),
           profile_id=self.config.get("cm_profile_id"),
           report=json.loads("""
               {
                  'name': '{{ params.title }}',
                  'accountId': '{{ params.account }}',
                  'fileName': '{{ params.file_name }}',
                  'type': 'STANDARD',
                  'criteria': {
                  ..."""),
           params=report_params,
           dag=self.dag)
   ```
   




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



[GitHub] [airflow] olchas commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
olchas commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447121531



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):
+            with open(self.report, 'r') as file:
+                self.report = json.loads(file.read())

Review comment:
       ```suggestion
                   self.report = json.load(file)
   ```
   WDYT?




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



[GitHub] [airflow] turbaszek commented on pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
turbaszek commented on pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#issuecomment-651194055


   @olchas @efolgart would you mind taking a look?


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



[GitHub] [airflow] efolgar commented on a change in pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
efolgar commented on a change in pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#discussion_r447129143



##########
File path: airflow/providers/google/marketing_platform/operators/campaign_manager.py
##########
@@ -298,6 +299,12 @@ def __init__(
         self.gcp_conn_id = gcp_conn_id
         self.delegate_to = delegate_to
 
+    def prepare_template(self) -> None:
+        # If .json is passed then we have to read the file
+        if isinstance(self.report, str) and self.report.endswith('.json'):

Review comment:
       True, although you can't take advantage of Airflow's [built-in macros](https://airflow.apache.org/docs/stable/macros-ref.html) like task_instance.  Also deviates from the behavior of other templated operators (e.g. [BashOperator](https://github.com/apache/airflow/blob/master/airflow/operators/bash.py)).




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



[GitHub] [airflow] mik-laj commented on pull request #9566: Fix using .json template extension in GMP operators

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #9566:
URL: https://github.com/apache/airflow/pull/9566#issuecomment-651813511


   Can you add unit test?


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