|
| 1 | +#### |
| 2 | +# This script demonstrates how to create a user using the Tableau |
| 3 | +# Server Client. |
| 4 | +# |
| 5 | +# To run the script, you must have installed Python 3.7 or later. |
| 6 | +#### |
| 7 | + |
| 8 | + |
| 9 | +import argparse |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import sys |
| 13 | +from typing import Sequence |
| 14 | + |
| 15 | +import tableauserverclient as TSC |
| 16 | + |
| 17 | + |
| 18 | +def parse_args(args: Sequence[str] | None) -> argparse.Namespace: |
| 19 | + """ |
| 20 | + Parse command line parameters |
| 21 | + """ |
| 22 | + if args is None: |
| 23 | + args = sys.argv[1:] |
| 24 | + parser = argparse.ArgumentParser(description="Creates a sample user group.") |
| 25 | + # Common options; please keep those in sync across all samples |
| 26 | + parser.add_argument("--server", "-s", help="server address") |
| 27 | + parser.add_argument("--site", "-S", help="site name") |
| 28 | + parser.add_argument("--token-name", "-p", help="name of the personal access token used to sign into the server") |
| 29 | + parser.add_argument("--token-value", "-v", help="value of the personal access token used to sign into the server") |
| 30 | + parser.add_argument( |
| 31 | + "--logging-level", |
| 32 | + "-l", |
| 33 | + choices=["debug", "info", "error"], |
| 34 | + default="error", |
| 35 | + help="desired logging level (set to error by default)", |
| 36 | + ) |
| 37 | + # Options specific to this sample |
| 38 | + # This sample has no additional options, yet. If you add some, please add them here |
| 39 | + parser.add_argument("--role", "-r", help="Site Role for the new user", default="Unlicensed") |
| 40 | + parser.add_argument( |
| 41 | + "--user", |
| 42 | + "-u", |
| 43 | + help="Username for the new user. If using active directory, it should be in the format of SAMAccountName@FullyQualifiedDomainName", |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--email", "-e", help="Email address of the new user. If using active directory, this field is optional." |
| 47 | + ) |
| 48 | + |
| 49 | + return parser.parse_args(args) |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + args = parse_args(None) |
| 54 | + |
| 55 | + # Set logging level based on user input, or error by default |
| 56 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 57 | + logging.basicConfig(level=logging_level) |
| 58 | + |
| 59 | + tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) |
| 60 | + server = TSC.Server(args.server, use_server_version=True, http_options={"verify": False}) |
| 61 | + with server.auth.sign_in(tableau_auth): |
| 62 | + # this code shows 2 different error codes for common mistakes |
| 63 | + # 400013: Invalid site role |
| 64 | + # 409000: user already exists on site |
| 65 | + |
| 66 | + user = TSC.UserItem(args.user, args.role) |
| 67 | + if args.email: |
| 68 | + user.email = args.email |
| 69 | + user = server.users.add(user) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments