elad-k commented on a change in pull request #16937: URL: https://github.com/apache/airflow/pull/16937#discussion_r667847281
########## File path: airflow/providers/tableau/operators/tableau_refresh_datasource.py ########## @@ -0,0 +1,95 @@ +# 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 tableauserverclient import DatasourceItem + +from airflow.exceptions import AirflowException +from airflow.models import BaseOperator +from airflow.providers.tableau.hooks.tableau import TableauHook + + +class TableauRefreshDatasourceOperator(BaseOperator): + """ + Refreshes a Tableau Datasource + + .. seealso:: https://tableau.github.io/server-client-python/docs/api-ref#data-sources + + :param datasource_name: The name of the datasource to refresh. + :type datasource_name: str + :param site_id: The id of the site where the datasource belongs to. + :type site_id: Optional[str] + :param blocking: By default the extract refresh will be blocking means it will wait until it has finished. + :type blocking: bool + :param tableau_conn_id: The Tableau Connection id containing the credentials + to authenticate to the Tableau Server. + :type tableau_conn_id: str + """ + + def __init__( + self, + *, + datasource_name: str, + site_id: Optional[str] = None, + blocking: bool = True, + tableau_conn_id: str = 'tableau_default', + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.datasource_name = datasource_name + self.site_id = site_id + self.blocking = blocking + self.tableau_conn_id = tableau_conn_id + + def execute(self, context: dict) -> str: + """ + Executes the Tableau Extract Refresh and pushes the job id to xcom. + + :param context: The task context during execution. + :type context: dict + :return: the id of the job that executes the extract refresh + :rtype: str + """ + with TableauHook(self.site_id, self.tableau_conn_id) as tableau_hook: + datasource = self._get_datasource_by_name(tableau_hook) + + job_id = self._refresh_datasource(tableau_hook, datasource.id) + if self.blocking: + from airflow.providers.tableau.sensors.tableau_job_status import TableauJobStatusSensor Review comment: This is how it's done now in `TableauRefreshWorkbookOperator` https://github.com/apache/airflow/blob/2fea4cdceaa12b3ac13f24eeb383af624aacb2e7/airflow/providers/tableau/operators/tableau_refresh_workbook.py#L74 ########## File path: airflow/providers/tableau/operators/tableau_refresh_datasource.py ########## @@ -0,0 +1,95 @@ +# 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 tableauserverclient import DatasourceItem + +from airflow.exceptions import AirflowException +from airflow.models import BaseOperator +from airflow.providers.tableau.hooks.tableau import TableauHook + + +class TableauRefreshDatasourceOperator(BaseOperator): + """ + Refreshes a Tableau Datasource + + .. seealso:: https://tableau.github.io/server-client-python/docs/api-ref#data-sources + + :param datasource_name: The name of the datasource to refresh. + :type datasource_name: str + :param site_id: The id of the site where the datasource belongs to. + :type site_id: Optional[str] + :param blocking: By default the extract refresh will be blocking means it will wait until it has finished. + :type blocking: bool + :param tableau_conn_id: The Tableau Connection id containing the credentials + to authenticate to the Tableau Server. + :type tableau_conn_id: str + """ + + def __init__( + self, + *, + datasource_name: str, + site_id: Optional[str] = None, + blocking: bool = True, + tableau_conn_id: str = 'tableau_default', + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.datasource_name = datasource_name + self.site_id = site_id + self.blocking = blocking + self.tableau_conn_id = tableau_conn_id + + def execute(self, context: dict) -> str: + """ + Executes the Tableau Extract Refresh and pushes the job id to xcom. + + :param context: The task context during execution. + :type context: dict + :return: the id of the job that executes the extract refresh + :rtype: str + """ + with TableauHook(self.site_id, self.tableau_conn_id) as tableau_hook: + datasource = self._get_datasource_by_name(tableau_hook) + + job_id = self._refresh_datasource(tableau_hook, datasource.id) + if self.blocking: + from airflow.providers.tableau.sensors.tableau_job_status import TableauJobStatusSensor Review comment: > One small comment here though. It looks a bit strange to use sensor in operator This is how it's done now in `TableauRefreshWorkbookOperator` https://github.com/apache/airflow/blob/2fea4cdceaa12b3ac13f24eeb383af624aacb2e7/airflow/providers/tableau/operators/tableau_refresh_workbook.py#L74 ########## File path: airflow/providers/tableau/operators/tableau_refresh_datasource.py ########## @@ -0,0 +1,95 @@ +# 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 tableauserverclient import DatasourceItem + +from airflow.exceptions import AirflowException +from airflow.models import BaseOperator +from airflow.providers.tableau.hooks.tableau import TableauHook + + +class TableauRefreshDatasourceOperator(BaseOperator): + """ + Refreshes a Tableau Datasource + + .. seealso:: https://tableau.github.io/server-client-python/docs/api-ref#data-sources + + :param datasource_name: The name of the datasource to refresh. + :type datasource_name: str + :param site_id: The id of the site where the datasource belongs to. + :type site_id: Optional[str] + :param blocking: By default the extract refresh will be blocking means it will wait until it has finished. + :type blocking: bool + :param tableau_conn_id: The Tableau Connection id containing the credentials + to authenticate to the Tableau Server. + :type tableau_conn_id: str + """ + + def __init__( + self, + *, + datasource_name: str, + site_id: Optional[str] = None, + blocking: bool = True, + tableau_conn_id: str = 'tableau_default', + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.datasource_name = datasource_name + self.site_id = site_id + self.blocking = blocking + self.tableau_conn_id = tableau_conn_id + + def execute(self, context: dict) -> str: + """ + Executes the Tableau Extract Refresh and pushes the job id to xcom. + + :param context: The task context during execution. + :type context: dict + :return: the id of the job that executes the extract refresh + :rtype: str + """ + with TableauHook(self.site_id, self.tableau_conn_id) as tableau_hook: + datasource = self._get_datasource_by_name(tableau_hook) + + job_id = self._refresh_datasource(tableau_hook, datasource.id) + if self.blocking: + from airflow.providers.tableau.sensors.tableau_job_status import TableauJobStatusSensor Review comment: Also https://github.com/apache/airflow/pull/16916/ will replace the usage of the `TableauJobStatusSensor` -- 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]
