|
| 1 | +#### |
| 2 | +# This script sets up a server. It uploads datasources and workbooks from the local filesystem. |
| 3 | +# |
| 4 | +# By default, all content is published to the Default project on the Default site. |
| 5 | +#### |
| 6 | + |
| 7 | +import tableauserverclient as TSC |
| 8 | +import argparse |
| 9 | +import getpass |
| 10 | +import logging |
| 11 | +import glob |
| 12 | + |
| 13 | +def main(): |
| 14 | + parser = argparse.ArgumentParser(description='Initialize a server with content.') |
| 15 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 16 | + parser.add_argument('--datasources-folder', '-df', required=True, help='folder containing datasources') |
| 17 | + parser.add_argument('--workbooks-folder', '-wf', required=True, help='folder containing workbooks') |
| 18 | + parser.add_argument('--site', '-si', required=False, default='Default', help='site to use') |
| 19 | + parser.add_argument('--project', '-p', required=False, default='Default', help='project to use') |
| 20 | + parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
| 21 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 22 | + help='desired logging level (set to error by default)') |
| 23 | + args = parser.parse_args() |
| 24 | + |
| 25 | + password = getpass.getpass("Password: ") |
| 26 | + |
| 27 | + # Set logging level based on user input, or error by default |
| 28 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 29 | + logging.basicConfig(level=logging_level) |
| 30 | + |
| 31 | + ################################################################################ |
| 32 | + # Step 1: Sign in to server. |
| 33 | + ################################################################################ |
| 34 | + tableau_auth = TSC.TableauAuth(args.username, password) |
| 35 | + server = TSC.Server(args.server) |
| 36 | + |
| 37 | + with server.auth.sign_in(tableau_auth): |
| 38 | + |
| 39 | + ################################################################################ |
| 40 | + # Step 2: Create the site we need only if it doesn't exist |
| 41 | + ################################################################################ |
| 42 | + print("Checking to see if we need to create the site...") |
| 43 | + |
| 44 | + all_sites, _ = server.sites.get() |
| 45 | + existing_site = next((s for s in all_sites if s.name == args.site), None) |
| 46 | + |
| 47 | + # Create the site if it doesn't exist |
| 48 | + if existing_site is None: |
| 49 | + print("Site not found: {0} Creating it...").format(args.site) |
| 50 | + new_site = TSC.SiteItem(name=args.site, content_url=args.site.replace(" ", ""), admin_mode=TSC.SiteItem.AdminMode.ContentAndUsers) |
| 51 | + server.sites.create(new_site) |
| 52 | + else: |
| 53 | + print("Site {0} exists. Moving on...").format(args.site) |
| 54 | + |
| 55 | + |
| 56 | + ################################################################################ |
| 57 | + # Step 3: Sign-in to our target site |
| 58 | + ################################################################################ |
| 59 | + print("Starting our content upload...") |
| 60 | + server_upload = TSC.Server(args.server) |
| 61 | + tableau_auth.site = args.site |
| 62 | + |
| 63 | + with server_upload.auth.sign_in(tableau_auth): |
| 64 | + |
| 65 | + ################################################################################ |
| 66 | + # Step 4: Create the project we need only if it doesn't exist |
| 67 | + ################################################################################ |
| 68 | + all_projects, _ = server_upload.projects.get() |
| 69 | + project = next((p for p in all_projects if p.name == args.project), None) |
| 70 | + |
| 71 | + # Create our project if it doesn't exist |
| 72 | + if project is None: |
| 73 | + print("Project not found: {0} Creating it...").format(args.project) |
| 74 | + new_project = TSC.ProjectItem(name=args.project) |
| 75 | + project = server_upload.projects.create(new_project) |
| 76 | + |
| 77 | + ################################################################################ |
| 78 | + # Step 5: Set up our content |
| 79 | + # Publish datasources to our site and project |
| 80 | + # Publish workbooks to our site and project |
| 81 | + ################################################################################ |
| 82 | + publish_datasources_to_site(server_upload, project, args.datasources_folder) |
| 83 | + publish_workbooks_to_site(server_upload, project, args.workbooks_folder) |
| 84 | + |
| 85 | +def publish_datasources_to_site(server_object, project, folder): |
| 86 | + path = folder + '/*.tds*' |
| 87 | + |
| 88 | + for fname in glob.glob(path): |
| 89 | + new_ds = TSC.DatasourceItem(project.id) |
| 90 | + new_ds = server_object.datasources.publish(new_ds, fname, server_object.PublishMode.Overwrite) |
| 91 | + print("Datasource published. ID: {0}".format(new_ds.id)) |
| 92 | + |
| 93 | + |
| 94 | +def publish_workbooks_to_site(server_object, project, folder): |
| 95 | + path = folder + '/*.twb*' |
| 96 | + |
| 97 | + for fname in glob.glob(path): |
| 98 | + new_workbook = TSC.WorkbookItem(project.id) |
| 99 | + new_workbook.show_tabs = True |
| 100 | + new_workbook = server_object.workbooks.publish(new_workbook, fname, server_object.PublishMode.Overwrite) |
| 101 | + print("Workbook published. ID: {0}".format(new_workbook.id)) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main() |
0 commit comments