eladkal commented on a change in pull request #16915:
URL: https://github.com/apache/airflow/pull/16915#discussion_r669388777



##########
File path: airflow/providers/tableau/operators/tableau.py
##########
@@ -0,0 +1,144 @@
+# 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 typing import Optional
+
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.tableau.hooks.tableau import TableauHook
+from airflow.providers.tableau.sensors.tableau_job_status import 
TableauJobStatusSensor
+
+RESOURCES_METHODS = {
+    'datasources': ['delete', 'download', 'refresh'],
+    'groups': ['delete'],
+    'projects': ['delete'],
+    'schedule': ['delete'],
+    'sites': ['delete'],
+    'subscriptions': ['delete'],
+    'tasks': ['delete', 'run'],
+    'users': ['remove'],
+    'workbooks': ['delete', 'download', 'refresh'],
+}
+
+
+class TableauOperator(BaseOperator):
+    """
+    Exectues a Tableau API Resource
+
+    .. seealso:: https://tableau.github.io/server-client-python/docs/api-ref
+
+    :param resource: The name of the resource to use.
+    :type resource: str
+    :param method: The name of the resource's method to execute.
+    :type method: str
+    :param resource_id: The id of resource wich will recive the action.
+    :type resource_id: str
+    :param resource_id_by: The resource field to be used as an id.
+    :type resource_id_by: Optional[str]
+    :param site_id: The id of the site where the workbook belongs to.
+    :type site_id: Optional[str]
+    :param blocking_refresh: By default the extract refresh will be blocking 
means it will wait until it has finished.
+    :type blocking_refresh: bool
+    :param tableau_conn_id: The :ref:`Tableau Connection id 
<howto/connection:tableau>`
+        containing the credentials to authenticate to the Tableau Server.
+    :type tableau_conn_id: str
+    """
+
+    def __init__(
+        self,
+        *,
+        resource: str,
+        method: str,
+        resource_id: str,
+        resource_id_by: str = 'id',
+        site_id: Optional[str] = None,
+        blocking_refresh: bool = True,
+        tableau_conn_id: str = 'tableau_default',
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+        self.resource = resource
+        self.method = method
+        self.resource_id = resource_id
+        self.resource_id_by = resource_id_by
+        self.site_id = site_id
+        self.blocking_refresh = blocking_refresh
+        self.tableau_conn_id = tableau_conn_id
+
+    def execute(self, context: dict) -> str:
+        """
+        Executes the Tableau API resource and pushes the job id or downloaded 
file URI to xcom.
+        :param context: The task context during execution.
+        :type context: dict
+        :return: the id of the job that executes the extract refresh or 
downloaded file URI.
+        :rtype: str
+        """
+
+        available_resources = RESOURCES_METHODS.keys()
+        if self.resource not in available_resources:
+            error_message = f'Resource not found! Available Resources: 
{available_resources}'
+            raise AirflowException(error_message)
+
+        available_methods = RESOURCES_METHODS[self.resource]
+        if self.method in available_methods:
+            error_message = f'Method not found! Available methods for 
{self.resource}: {available_methods}'
+            raise AirflowException(error_message)
+
+        with TableauHook(self.site_id, self.tableau_conn_id) as tableau_hook:
+
+            resource = getattr(tableau_hook.server, self.resource)
+            method = getattr(resource, self.resource)
+
+            resource_id = self._get_resource_id_by(tableau_hook)
+
+            response = method(resource_id)
+
+        if self.method == 'download':
+            return response

Review comment:
       If I understand it right this means that you push to xcom the downloaded 
data source. I'm not tableau expert but isn't it may be massive amount of data? 
Should we apply limitation to it like `MAX_XCOM_SIZE` ? example in 
`GCSToLocalFilesystemOperator`: 
   
   
https://github.com/apache/airflow/blob/866a601b76e219b3c043e1dbbc8fb22300866351/airflow/providers/google/cloud/transfers/gcs_to_local.py#L134-L140
   
   (Also another thought maybe we don't need to return anything when `method == 
'download' `?)
   WDYT?

##########
File path: airflow/providers/tableau/operators/tableau.py
##########
@@ -0,0 +1,144 @@
+# 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 typing import Optional
+
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.tableau.hooks.tableau import TableauHook
+from airflow.providers.tableau.sensors.tableau_job_status import 
TableauJobStatusSensor
+
+RESOURCES_METHODS = {
+    'datasources': ['delete', 'download', 'refresh'],
+    'groups': ['delete'],
+    'projects': ['delete'],
+    'schedule': ['delete'],
+    'sites': ['delete'],
+    'subscriptions': ['delete'],
+    'tasks': ['delete', 'run'],
+    'users': ['remove'],
+    'workbooks': ['delete', 'download', 'refresh'],
+}
+
+
+class TableauOperator(BaseOperator):
+    """
+    Exectues a Tableau API Resource
+
+    .. seealso:: https://tableau.github.io/server-client-python/docs/api-ref
+
+    :param resource: The name of the resource to use.
+    :type resource: str
+    :param method: The name of the resource's method to execute.
+    :type method: str
+    :param resource_id: The id of resource wich will recive the action.
+    :type resource_id: str
+    :param resource_id_by: The resource field to be used as an id.
+    :type resource_id_by: Optional[str]
+    :param site_id: The id of the site where the workbook belongs to.
+    :type site_id: Optional[str]
+    :param blocking_refresh: By default the extract refresh will be blocking 
means it will wait until it has finished.
+    :type blocking_refresh: bool
+    :param tableau_conn_id: The :ref:`Tableau Connection id 
<howto/connection:tableau>`
+        containing the credentials to authenticate to the Tableau Server.
+    :type tableau_conn_id: str
+    """
+
+    def __init__(
+        self,
+        *,
+        resource: str,
+        method: str,
+        resource_id: str,
+        resource_id_by: str = 'id',

Review comment:
       What it means when you provide both `resource_id` & `resource_id_by` 
(Like shown in the example dag you added)? From what I see you need only 1 of 
them (as if user provided `resource_id_by` then you search for the id and then 
execute the method ). It can be confusing when both are provided isn't it?
   

##########
File path: airflow/providers/tableau/provider.yaml
##########
@@ -37,6 +37,7 @@ integrations:
 operators:
   - integration-name: Tableau
     python-modules:
+      - airflow.providers.tableau.operators.tableau

Review comment:
       Would be nice also to add docs about the operator usage.
   This needs to be added to:
   
https://github.com/apache/airflow/tree/main/docs/apache-airflow-providers-tableau
   
   Examples from other operators:
   
https://github.com/apache/airflow/blob/main/docs/apache-airflow-providers-amazon/operators/emr.rst
   
https://github.com/apache/airflow/blob/main/docs/apache-airflow-providers-mysql/operators.rst
   
https://github.com/apache/airflow/blob/main/docs/apache-airflow-providers-asana/operators/asana.rst
   
   

##########
File path: airflow/providers/tableau/operators/tableau_refresh_workbook.py
##########
@@ -65,31 +76,16 @@ def execute(self, context: dict) -> str:
         :rtype: str
         """
         with TableauHook(self.site_id, self.tableau_conn_id) as tableau_hook:
-            workbook = self._get_workbook_by_name(tableau_hook)
 
-            job_id = self._refresh_workbook(tableau_hook, workbook.id)
-            if self.blocking:
-                from airflow.providers.tableau.sensors.tableau_job_status 
import TableauJobStatusSensor
+            job_id = TableauOperator(
+                resource='workbooks',
+                method='refresh',
+                resource_id=self.workbook_name,
+                resource_id_by='name',
+                site_id=self.site_id,
+                tableau_conn_id=self.tableau_conn_id,
+                task_id='refresh_workbook',
+                dag=None,
+            ).execute(context={})

Review comment:
       Is it backward compatible? The `TableauRefreshWorkbookOperator` had 
`blocking`, the TableauOperator has `blocking_refresh`. This means that it will 
be broken for some users.




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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to