-
Notifications
You must be signed in to change notification settings - Fork 448
Adds a sample for publishing datasources #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #### | ||
| # This script demonstrates how to use the Tableau Server Client | ||
| # to publish a datasource to a Tableau server. It will publish | ||
| # a specified datasource to the 'default' project of the provided site. | ||
| # | ||
| # Some optional arguments are provided to demonstrate async publishing, | ||
| # as well as providing connection credentials when publishing. If the | ||
| # provided datasource file is over 64MB in size, TSC will automatically | ||
| # publish the datasource using the chunking method. | ||
| # | ||
| # For more information, refer to the documentations: | ||
| # (https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_datasources.htm#publish_data_source) | ||
| # | ||
| # For signing into server, this script uses personal access tokens. For | ||
| # more information on personal access tokens, refer to the documentations: | ||
| # (https://help.tableau.com/current/server/en-us/security_personal_access_tokens.htm) | ||
| # | ||
| # To run the script, you must have installed Python 3.5 or later. | ||
| #### | ||
|
|
||
| import argparse | ||
| import logging | ||
|
|
||
| import tableauserverclient as TSC | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description='Publish a datasource to server.') | ||
| parser.add_argument('--server', '-s', required=True, help='server address') | ||
| parser.add_argument('--site', '-i', help='site name') | ||
| parser.add_argument('--token-name', '-p', required=True, | ||
| help='name of the personal access token used to sign into the server') | ||
| parser.add_argument('--token-value', '-v', required=True, | ||
| help='value of the personal access token used to sign into the server') | ||
| parser.add_argument('--filepath', '-f', required=True, help='filepath to the datasource to publish') | ||
| parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', | ||
| help='desired logging level (set to error by default)') | ||
| parser.add_argument('--async', '-a', help='Publishing asynchronously', dest='async_', action='store_true') | ||
| parser.add_argument('--conn-username', help='connection username') | ||
| parser.add_argument('--conn-password', help='connection password') | ||
| parser.add_argument('--conn-embed', help='embed connection password to datasource', action='store_true') | ||
| parser.add_argument('--conn-oauth', help='connection is configured to use oAuth', action='store_true') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Ensure that both the connection username and password are provided, or none at all | ||
| if (args.conn_username and not args.conn_password) or (not args.conn_username and args.conn_password): | ||
| parser.error("Both the connection username and password must be provided") | ||
|
|
||
| # Set logging level based on user input, or error by default | ||
| logging_level = getattr(logging, args.logging_level.upper()) | ||
| logging.basicConfig(level=logging_level) | ||
|
|
||
| # Sign in to server | ||
| tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) | ||
| server = TSC.Server(args.server, use_server_version=True) | ||
| with server.auth.sign_in(tableau_auth): | ||
| # Create a new datasource item to publish - empty project_id field | ||
| # will default the publish to the site's default project | ||
| new_datasource = TSC.DatasourceItem(project_id="") | ||
|
|
||
| # Create a connection_credentials item if connection details are provided | ||
| new_conn_creds = None | ||
| if args.conn_username: | ||
| new_conn_creds = TSC.ConnectionCredentials(args.conn_username, args.conn_password, | ||
| embed=args.conn_embed, oauth=args.conn_oauth) | ||
|
|
||
| # Define publish mode - Overwrite, Append, or CreateNew | ||
| publish_mode = TSC.Server.PublishMode.Overwrite | ||
|
|
||
| # Publish datasource | ||
| if args.async_: | ||
| # Async publishing, returns a job_item | ||
| new_job = server.datasources.publish(new_datasource, args.filepath, publish_mode, | ||
| connection_credentials=new_conn_creds, as_job=True) | ||
| print("Datasource published asynchronously. Job ID: {0}".format(new_job.id)) | ||
| else: | ||
| # Normal publishing, returns a datasource_item | ||
| new_datasource = server.datasources.publish(new_datasource, args.filepath, publish_mode, | ||
| connection_credentials=new_conn_creds) | ||
| print("Datasource published. Datasource ID: {0}".format(new_datasource.id)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.