-
Notifications
You must be signed in to change notification settings - Fork 446
samples: credential helper, pagination fixes, and new jobs/subscriptions samples #1843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacalata
wants to merge
16
commits into
development
Choose a base branch
from
jac/samples-improvements
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0fe5e96
samples: add shared credential resolver that avoids the command line
jacalata 97ca972
samples: fix mispagination in samples that treated a single page as all
jacalata 6b369de
samples: add list_jobs and manage_subscriptions for coverage gaps
jacalata b6d0835
Improve the stale actions config now that it's been running for a bit…
bcantoni e729e66
feat: parse <statusNotes>/<statusNote> into JobItem.status_notes (#18…
jacalata aa9e3a0
fix: normalize CRLF line endings to LF in six Python files (#1816)
bcantoni 54e51f5
samples: align sign-in short flags with tabcmd
jacalata 5b5c90a
feat: expose refreshExtractTriggered on SubscriptionItem (#1658)
jacalata 4207f7f
Address fresh-eyes review on refreshExtractTriggered subscriptions
jacalata 04e4235
samples: add shared credential resolver that avoids the command line
jacalata aa1d6d9
samples: fix mispagination in samples that treated a single page as all
jacalata 92b1d36
samples: add list_jobs and manage_subscriptions for coverage gaps
jacalata e3b9d85
samples: align sign-in short flags with tabcmd
jacalata e9a11fa
samples: fix argparse blocker + 7 bugs, add JWT + on-extract-refresh
jacalata 98f544d
samples: address remaining fresh-eyes review followups (#1843)
jacalata b3a6c31
Merge origin/jac/samples-improvements to reconcile pre-rebase state
jacalata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| #### | ||
| # Shared helpers for the sample scripts in this directory. | ||
| # | ||
| # The most important thing here is `resolve_credentials`, which lets samples | ||
| # accept a Tableau server URL, site, and credentials from three sources: | ||
| # | ||
| # 1. Command-line arguments (useful for CI, but note that these end up in | ||
| # shell history and process listings, so avoid them for real secrets). | ||
| # 2. Environment variables. If a `.env` file exists next to the sample | ||
| # being run, or in the current working directory, we load it first -- | ||
| # only the standard `KEY=value` lines, no external dependency required. | ||
| # 3. Interactive prompts. Missing values are asked for on stdin; secrets | ||
| # are read with `getpass.getpass` so they are not echoed. | ||
| # | ||
| # CLI args take precedence, then environment, then interactive prompt. | ||
| # This lets a user set defaults in a `.env` file and override individual | ||
| # values on the command line. | ||
| #### | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import getpass | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Iterable | ||
|
|
||
| import tableauserverclient as TSC | ||
|
|
||
| # Recognized environment variable names, in the order we look them up. | ||
| # Older samples used TABLEAU_SERVER etc; keep those working as aliases. | ||
| _ENV_ALIASES: dict[str, tuple[str, ...]] = { | ||
| "server": ("TABLEAU_SERVER", "SERVER"), | ||
| "site": ("TABLEAU_SITE", "SITE"), | ||
| "token_name": ("TABLEAU_TOKEN_NAME", "TOKEN_NAME"), | ||
| "token_value": ("TABLEAU_TOKEN_VALUE", "TOKEN_VALUE"), | ||
| "username": ("TABLEAU_USERNAME", "USERNAME"), | ||
| "password": ("TABLEAU_PASSWORD", "PASSWORD"), | ||
| } | ||
|
|
||
|
|
||
| def add_common_arguments(parser: argparse.ArgumentParser) -> None: | ||
| """Add the sign-in and logging arguments used by every sample. | ||
|
|
||
| Kept in sync with the historical inline definitions so no existing | ||
| command line breaks. All arguments are optional -- missing values | ||
| are pulled from the environment or prompted for interactively. | ||
| """ | ||
| parser.add_argument("--server", "-s", help="server address (env: TABLEAU_SERVER)") | ||
| parser.add_argument("--site", "-t", help="site content URL (env: TABLEAU_SITE)") | ||
| parser.add_argument( | ||
| "--token-name", | ||
| help="name of the personal access token used to sign into the server " "(env: TABLEAU_TOKEN_NAME)", | ||
| ) | ||
| parser.add_argument( | ||
| "--token-value", | ||
| help="value of the personal access token used to sign into the server " | ||
| "(env: TABLEAU_TOKEN_VALUE). Prefer the env var or interactive prompt over the " | ||
| "command line so the secret does not land in shell history.", | ||
| ) | ||
| parser.add_argument( | ||
| "--username", | ||
| "-u", | ||
| help="username to sign into the server (env: TABLEAU_USERNAME). Only used if " | ||
| "no personal access token is supplied.", | ||
| ) | ||
| parser.add_argument( | ||
| "--password", | ||
| "-p", | ||
| help="password (env: TABLEAU_PASSWORD). Prefer the env var or interactive " "prompt over the command line.", | ||
| ) | ||
| parser.add_argument( | ||
| "--env-file", | ||
| help="path to a .env-style file with KEY=value lines to load. If omitted, " | ||
| ".env in the current directory is loaded automatically when present.", | ||
| ) | ||
| parser.add_argument( | ||
| "--logging-level", | ||
| "-l", | ||
| choices=["debug", "info", "error"], | ||
| default="error", | ||
| help="desired logging level (set to error by default)", | ||
| ) | ||
|
|
||
|
|
||
| def _load_env_file(path: Path) -> None: | ||
| """Very small `.env` loader: `KEY=value` per line, `#` for comments. | ||
|
|
||
| We do not want a runtime dependency on python-dotenv for the samples, | ||
| so this parses just the common cases. Existing env vars are not | ||
| overwritten -- a value already in `os.environ` wins. | ||
| """ | ||
| try: | ||
| text = path.read_text(encoding="utf-8") | ||
| except OSError: | ||
| return | ||
| for raw_line in text.splitlines(): | ||
| line = raw_line.strip() | ||
| if not line or line.startswith("#") or "=" not in line: | ||
| continue | ||
| key, _, value = line.partition("=") | ||
| key = key.strip() | ||
| value = value.strip().strip("'\"") | ||
| if key and key not in os.environ: | ||
| os.environ[key] = value | ||
|
|
||
|
|
||
| def _first_env(names: Iterable[str]) -> str | None: | ||
| for name in names: | ||
| val = os.environ.get(name) | ||
| if val: | ||
| return val | ||
| return None | ||
|
|
||
|
|
||
| def resolve_credentials(args: argparse.Namespace, *, allow_prompt: bool = True) -> None: | ||
| """Fill in server/site/credential values on `args` from env or prompt. | ||
|
|
||
| Precedence for each field: existing value on `args` > environment variable | ||
| > interactive prompt (if allow_prompt and stdin is a terminal). | ||
|
|
||
| Pass `allow_prompt=False` in CI environments where blocking on input would | ||
| hang the job; the caller should then verify the fields it needs are set. | ||
| """ | ||
| # Load `.env` file if one is requested or available. | ||
| env_file = getattr(args, "env_file", None) | ||
| if env_file: | ||
| _load_env_file(Path(env_file)) | ||
| else: | ||
| default_env = Path.cwd() / ".env" | ||
| if default_env.is_file(): | ||
| _load_env_file(default_env) | ||
|
|
||
| # For each field, prefer the CLI arg, then env, then prompt. | ||
| for field, env_names in _ENV_ALIASES.items(): | ||
| current = getattr(args, field, None) | ||
| if current: | ||
| continue | ||
| env_val = _first_env(env_names) | ||
| if env_val: | ||
| setattr(args, field, env_val) | ||
|
|
||
| if not allow_prompt: | ||
| return | ||
|
|
||
| # Prompt for what's still missing. We only prompt for the pieces we | ||
| # actually need: server URL, and one of token or username/password. | ||
| if not getattr(args, "server", None): | ||
| args.server = input("Tableau server URL: ").strip() | ||
|
|
||
| # Site is optional (empty string is the default site) so we don't prompt. | ||
|
|
||
| has_token = getattr(args, "token_name", None) and getattr(args, "token_value", None) | ||
| has_user = getattr(args, "username", None) and getattr(args, "password", None) | ||
|
|
||
| if has_token or has_user: | ||
| return | ||
|
|
||
| # Nothing configured yet. Ask which auth method to use. | ||
| if getattr(args, "token_name", None) or getattr(args, "username", None): | ||
| # Partial info supplied -- fill in the matching missing piece. | ||
| if getattr(args, "token_name", None) and not getattr(args, "token_value", None): | ||
| args.token_value = getpass.getpass(f"Personal access token value for '{args.token_name}': ") | ||
| return | ||
| if getattr(args, "username", None) and not getattr(args, "password", None): | ||
| args.password = getpass.getpass(f"Password for '{args.username}': ") | ||
| return | ||
|
|
||
| # Fully unspecified: default to PAT since that's what the docs recommend. | ||
| print("No credentials found in args or environment. Sign in with a personal access token.") | ||
| print("(Set TABLEAU_TOKEN_NAME / TABLEAU_TOKEN_VALUE in your env or a .env file to skip this prompt.)") | ||
| args.token_name = input("Personal access token name: ").strip() | ||
| args.token_value = getpass.getpass("Personal access token value: ") | ||
|
|
||
|
|
||
| def build_auth(args: argparse.Namespace) -> TSC.TableauAuth | TSC.PersonalAccessTokenAuth: | ||
| """Return the appropriate auth object based on what's set on `args`.""" | ||
| site = getattr(args, "site", None) or "" | ||
| if getattr(args, "token_name", None) and getattr(args, "token_value", None): | ||
| return TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=site) | ||
| if getattr(args, "username", None) and getattr(args, "password", None): | ||
| return TSC.TableauAuth(args.username, args.password, site_id=site) | ||
| raise ValueError( | ||
| "No usable credentials found. Provide --token-name/--token-value, " | ||
| "--username/--password, or set the corresponding env vars." | ||
| ) | ||
|
|
||
|
|
||
| def sign_in(args: argparse.Namespace, *, use_server_version: bool = True) -> TSC.Server: | ||
| """Convenience helper: resolve credentials, build the server, and sign in. | ||
|
|
||
| The caller is responsible for calling `server.auth.sign_out()` or using | ||
| the `with server.auth.sign_in(...)` context manager pattern themselves | ||
| when they need finer control. This helper is intended for the small | ||
| samples that just want a signed-in server object to poke at. | ||
| """ | ||
| resolve_credentials(args) | ||
| auth = build_auth(args) | ||
| server = TSC.Server(args.server, use_server_version=use_server_version) | ||
| server.auth.sign_in(auth) | ||
| return server | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.