|
| 1 | +#### |
| 2 | +# This script demonstrates how to use the Tableau Server Client |
| 3 | +# to filter and sort on the name of the projects present on site. |
| 4 | +# |
| 5 | +# |
| 6 | +# To run the script, you must have installed Python 2.7.X or 3.3 and later. |
| 7 | +#### |
| 8 | + |
| 9 | +import argparse |
| 10 | +import getpass |
| 11 | +import logging |
| 12 | + |
| 13 | +import tableauserverclient as TSC |
| 14 | + |
| 15 | + |
| 16 | +def create_example_project(name='Example Project', content_permissions='LockedToProject', |
| 17 | + description='Project created for testing', server=None): |
| 18 | + |
| 19 | + new_project = TSC.ProjectItem(name=name, content_permissions=content_permissions, |
| 20 | + description=description) |
| 21 | + try: |
| 22 | + server.projects.create(new_project) |
| 23 | + print('Created a new project called: %s' % name) |
| 24 | + except TSC.ServerResponseError: |
| 25 | + print('We have already created this resource: %s' % name) |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + parser = argparse.ArgumentParser(description='Get all of the refresh tasks available on a server') |
| 30 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 31 | + parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
| 32 | + parser.add_argument('--site', '-S', default=None) |
| 33 | + parser.add_argument('-p', default=None) |
| 34 | + |
| 35 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 36 | + help='desired logging level (set to error by default)') |
| 37 | + |
| 38 | + args = parser.parse_args() |
| 39 | + |
| 40 | + if args.p is None: |
| 41 | + password = getpass.getpass("Password: ") |
| 42 | + else: |
| 43 | + password = args.p |
| 44 | + |
| 45 | + # Set logging level based on user input, or error by default |
| 46 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 47 | + logging.basicConfig(level=logging_level) |
| 48 | + |
| 49 | + tableau_auth = TSC.TableauAuth(args.username, password) |
| 50 | + server = TSC.Server(args.server) |
| 51 | + |
| 52 | + with server.auth.sign_in(tableau_auth): |
| 53 | + # Use highest Server REST API version available |
| 54 | + server.use_server_version() |
| 55 | + |
| 56 | + filter_project_name = 'default' |
| 57 | + options = TSC.RequestOptions() |
| 58 | + |
| 59 | + options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, |
| 60 | + TSC.RequestOptions.Operator.Equals, |
| 61 | + filter_project_name)) |
| 62 | + |
| 63 | + filtered_projects, _ = server.projects.get(req_options=options) |
| 64 | + # Result can either be a matching project or an empty list |
| 65 | + if filtered_projects: |
| 66 | + project_name = filtered_projects.pop().name |
| 67 | + print(project_name) |
| 68 | + else: |
| 69 | + error = "No project named '{}' found".format(filter_project_name) |
| 70 | + print(error) |
| 71 | + |
| 72 | + create_example_project(name='Example 1', server=server) |
| 73 | + create_example_project(name='Example 2', server=server) |
| 74 | + create_example_project(name='Example 3', server=server) |
| 75 | + create_example_project(name='Proiect ca Exemplu', server=server) |
| 76 | + |
| 77 | + options = TSC.RequestOptions() |
| 78 | + |
| 79 | + # don't forget to URL encode the query names |
| 80 | + options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, |
| 81 | + TSC.RequestOptions.Operator.In, |
| 82 | + ['Example+1', 'Example+2', 'Example+3'])) |
| 83 | + |
| 84 | + options.sort.add(TSC.Sort(TSC.RequestOptions.Field.Name, |
| 85 | + TSC.RequestOptions.Direction.Desc)) |
| 86 | + |
| 87 | + matching_projects, pagination_item = server.projects.get(req_options=options) |
| 88 | + print('Filtered projects are:') |
| 89 | + for project in matching_projects: |
| 90 | + print(project.name, project.id) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments