forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish_datasource.py
More file actions
129 lines (112 loc) · 5.55 KB
/
Copy pathpublish_datasource.py
File metadata and controls
129 lines (112 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
####
# 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.7 or later.
####
import argparse
import logging
import tableauserverclient as TSC
import env
import tableauserverclient.datetime_helpers
def main():
parser = argparse.ArgumentParser(description="Publish a datasource to server.")
# Common options; please keep those in sync across all samples
parser.add_argument("--server", "-s", help="server address")
parser.add_argument("--site", "-S", help="site name")
parser.add_argument("--token-name", "-p", help="name of the personal access token used to sign into the server")
parser.add_argument("--token-value", "-v", help="value of the personal access token used to sign into the server")
parser.add_argument(
"--logging-level",
"-l",
choices=["debug", "info", "error"],
default="error",
help="desired logging level (set to error by default)",
)
# Options specific to this sample
parser.add_argument("--file", "-f", help="filepath to the datasource to publish")
parser.add_argument("--project", help="Project within which to publish the datasource")
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()
if not args.server:
args.server = env.server
if not args.site:
args.site = env.site
if not args.token_name:
args.token_name = env.token_name
if not args.token_value:
args.token_value = env.token_value
args.logging = "debug"
args.file = "C:/dev/tab-samples/5M.tdsx"
args.async_ = True
# 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
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
_logger.addHandler(logging.StreamHandler())
# 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):
# Empty project_id field will default the publish to the site's default project
project_id = ""
# Retrieve the project id, if a project name was passed
if args.project is not None:
req_options = TSC.RequestOptions()
req_options.filter.add(
TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, args.project)
)
projects = list(TSC.Pager(server.projects, req_options))
if len(projects) > 1:
raise ValueError("The project name is not unique")
project_id = projects[0].id
# Create a new datasource item to publish
new_datasource = TSC.DatasourceItem(project_id=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_:
print("Publish as a job")
# Async publishing, returns a job_item
new_job = server.datasources.publish(
new_datasource, args.file, 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.file, publish_mode, connection_credentials=new_conn_creds
)
print(
"{0}Datasource published. Datasource ID: {1}".format(
new_datasource.id, tableauserverclient.datetime_helpers.timestamp()
)
)
print("\t\tClosing connection")
if __name__ == "__main__":
main()