8000 Adds a sample for publishing datasources (#644) · rshide/server-client-python@6d48569 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d48569

Browse files
author
Chris Shin
authored
Adds a sample for publishing datasources (tableau#644)
* Adds a sample for publishing datasources * Addresses feedback to use PAT and async flag
1 parent d9edc55 commit 6d48569

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

samples/publish_datasource.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
####
2+
# This script demonstrates how to use the Tableau Server Client
3+
# to publish a datasource to a Tableau server. It will publish
4+
# a specified datasource to the 'default' project of the provided site.
5+
#
6+
# Some optional arguments are provided to demonstrate async publishing,
7+
# as well as providing connection credentials when publishing. If the
8+
# provided datasource file is over 64MB in size, TSC will automatically
9+
# publish the datasource using the chunking method.
10+
#
11+
# For more information, refer to the documentations:
12+
# (https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_datasources.htm#publish_data_source)
13+
#
14+
# For signing into server, this script uses personal access tokens. For
15+
# more information on personal access tokens, refer to the documentations:
16+
# (https://help.tableau.com/current/server/en-us/security_personal_access_tokens.htm)
17+
#
18+
# To run the script, you must have installed Python 3.5 or later.
19+
####
20+
21+
import argparse
22+
import logging
23+
24+
import tableauserverclient as TSC
25+
26+
27+
def main():
28+
parser = argparse.ArgumentParser(description='Publish a datasource to server.')
29+
parser.add_argument('--server', '-s', required=True, help='server address')
30+
parser.add_argument('--site', '-i', help='site name')
31+
parser.add_argument('--token-name', '-p', required=True,
32+
help='name of the personal access token used to sign into the server')
33+
parser.add_argument('--token-value', '-v', required=True,
34+
help='value of the personal access token used to sign into the server')
35+
parser.add_argument('--filepath', '-f', required=True, help='filepath to the datasource to publish')
36+
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
37+
help='desired logging level (set to error by default)')
38+
parser.add_argument('--async', '-a', help='Publishing asynchronously', dest='async_', action='store_true')
39+
parser.add_argument('--conn-username', help='connection username')
40+
parser.add_argument('--conn-password', help='connection password')
41+
parser.add_argument('--conn-embed', help='embed connection password to datasource', action='store_true')
42+
parser.add_argument('--conn-oauth', help='connection is configured to use oAuth', action='store_true')
43+
44+
args = parser.parse_args()
45+
46+
# Ensure that both the connection username and password are provided, or none at all
47+
if (args.conn_username and not args.conn_password) or (not args.conn_username and args.conn_password):
48+
parser.error("Both the connection username and password must be provided")
49+
50+
# Set logging level based on user input, or error by default
51+
logging_level = getattr(logging, args.logging_level.upper())
52+
logging.basicConfig(level=logging_level)
53+
54+
# Sign in to server
55+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
56+
server = TSC.Server(args.server, use_server_version=True)
57+
with server.auth.sign_in(tableau_auth):
58+
# Create a new datasource item to publish - empty project_id field
59+
# will default the publish to the site's default project
60+
new_datasource = TSC.DatasourceItem(project_id="")
61+
62+
# Create a connection_credentials item if connection details are provided
63+
new_conn_creds = None
64+
if args.conn_username:
65+
new_conn_creds = TSC.ConnectionCredentials(args.conn_username, args.conn_password,
66+
embed=args.conn_embed, oauth=args.conn_oauth)
67+
68+
# Define publish mode - Overwrite, Append, or CreateNew
69+
publish_mode = TSC.Server.PublishMode.Overwrite
70+
71+
# Publish datasource
72+
if args.async_:
73+
# Async publishing, returns a job_item
74+
new_job = server.datasources.publish(new_datasource, args.filepath, publish_mode,
75+
connection_credentials=new_conn_creds, as_job=True)
76+
print("Datasource published asynchronously. Job ID: {0}".format(new_job.id))
77+
else:
78+
# Normal publishing, returns a datasource_item
79+
new_datasource = server.datasources.publish(new_datasource, args.filepath, publish_mode,
80+
connection_credentials=new_conn_creds)
81+
print("Datasource published. Datasource ID: {0}".format(new_datasource.id))
82+
83+
84+
if __name__ == '__main__':
85+
main()

0 commit comments

Comments
 (0)
0