Skip to content

Commit

Permalink
remove dependency on TSC
Browse files Browse the repository at this point in the history
  • Loading branch information
categuyman committed Jan 21, 2025
1 parent 2b0b966 commit b838d02
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 26 deletions.
94 changes: 68 additions & 26 deletions samples/vizql_data_service_smoke_test_cap.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
####
# This script demonstrates how to query for permissions using TSC
# This script is for smoke testing VizQL Data Service.
# To run the script, you must have installed Python 3.7 or later.
#
# Example usage: 'python query_permissions.py -s https://10ax.online.tableau.com --site
# devSite123 -u [email protected] b4065286-80f0-11ea-af1b-cb7191f48e45'
# Example usage: 'python vizql_data_service_smoke_test_cap.py --token-name token
# --token-value DUCg0rbPROuuAMz9rDI4+Q==:OM2SzwPVK7dNITHo4nfUgsTZvVBQj8iQ
# --server stage-dataplane7.online.vnext.tabint.net
# --site hbistagedp7
# --datasouce-luid ebe79f30-bdff-425a-8a9c-3cda79dbbbfd
# --cap 2000'

####

import argparse
import logging
import requests
import json
import xml.etree.ElementTree as ET

def parse_token_from_response(xml_response):
namespace = {'ns': 'http://tableau.com/api'}
root = ET.fromstring(xml_response)

# Find the token attribute in the credentials tag
credentials = root.find('ns:credentials', namespace)
if credentials is not None:
token = credentials.get('token')
return token
else:
raise ValueError("Token not found in the XML response.")


def sign_in(args):
url = f"https://{args.server}/api/3.22/auth/signin"

payload = json.dumps({
"credentials": {
"personalAccessTokenName": args.token_name,
"personalAccessTokenSecret": args.token_value,
"site": {
"contentUrl": args.site
}
}
})
headers = {
'Content-Type': 'application/json'
}

import tableauserverclient as TSC

response = requests.request("POST", url, headers=headers, data=payload)
auth_token = parse_token_from_response(response.text)
return auth_token

def main():
parser = argparse.ArgumentParser(description="Query permissions of a given resource.")
# 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")
Expand All @@ -28,39 +63,46 @@ def main():
default="error",
help="desired logging level (set to error by default)",
)
parser.add_argument("resource_id")
parser.add_argument("--datasource-luid", "-ds", help="The luid of the datasource to query", required=True)
parser.add_argument("--cap", "-c", type=int, help="The cap on the current cloud site", required=True)

args = parser.parse_args()

# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)

# Sign in
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):
endpoint = server.datasources

# Get the resource by its ID
resource = endpoint.get_by_id(args.resource_id)
print(server)
auth_token = sign_in(args)

url = "https://" + args.server + "/api/v1/vizql-data-service/read-metadata"

url = "https://" + args.server + "/api/v1/vizql-data-service/read-metadata"

payload = "{\n \"datasource\": {\n \"datasourceLuid\": \"" + args.resource_id + "\"\n },\n \"options\": {\n \"debug\": true\n }\n}"
headers = {
'X-Tableau-Auth': server.auth_token,
'Content-Type': 'application/json',
payload = json.dumps({
"datasource": {
"datasourceLuid": args.datasource_luid
}
})
headers = {
'X-Tableau-Auth': auth_token,
'Content-Type': 'application/json',
}

response = requests.request("POST", url, headers=headers, data=payload)
# Test cap limit
for i in range(args.cap + 1):
response = requests.post(url, headers=headers, data=payload)
status_code = response.status_code

print(response.text)
if i < args.cap and status_code != 200:
response_message = response.text
exceptionMsg = f"Unexpected status code for call {i + 1}: {status_code} (Expected: 200). Response message: {response_message}";
raise Exception(exceptionMsg)
elif i >= args.cap and status_code != 429:
exceptionMsg = f"Call not rate limited: Unexpected status code for call {i + 1}: {status_code} (Expected: 429)";
raise Exception(exceptionMsg)

logging.info(f"Call {i + 1}/{args.cap}: Status Code {status_code}")

print(f"Completed {args.cap} calls to VizQL Data Service.")

if __name__ == "__main__":
main()


86 changes: 86 additions & 0 deletions samples/vizql_data_service_smoke_test_cap_with_TSC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
####
# This script is for smoke testing VizQL Data Service.
# To run the script, you must have installed Python 3.7 or later.
#
# Example usage: 'python vizql_data_service_smoke_test_cap.py --token-name token
# --token-value DUCg0rbPROuuAMz9rDI4+Q==:OM2SzwPVK7dNITHo4nfUgsTZvVBQj8iW
# --server stage-dataplane7.online.vnext.tabint.net
# --site hbistagedp7
# --datasouce-luid ebe79f30-bdff-425a-8a9c-3cda79dbbbfd
# --cap 2000'

####

import argparse
import logging
import requests
import json

import tableauserverclient as TSC


def main():
parser = argparse.ArgumentParser(description="Query permissions of a given resource.")
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)",
)
parser.add_argument("--datasource-luid", "-ds", help="The luid of the datasource to query", required=True)
parser.add_argument("--cap", "-c", type=int, help="The cap on the current cloud site", required=True)

args = parser.parse_args()

# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)

# Sign in
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):
url = "https://" + args.server + "/api/v1/vizql-data-service/read-metadata"

payload = json.dumps({
"datasource": {
"datasourceLuid": args.datasource_luid
},
"options": {
"debug": True
}
})
headers = {
'X-Tableau-Auth': server.auth_token,
'Content-Type': 'application/json',
}

# Test cap limit
for i in range(args.cap + 1):
response = requests.post(url, headers=headers, data=payload)
status_code = response.status_code

if i < args.cap and status_code != 200:
response_message = response.text
exceptionMsg = f"Unexpected status code for call {i + 1}: {status_code} (Expected: 200). Response message: {response_message}";
raise Exception(exceptionMsg)
elif i >= args.cap and status_code != 429:
exceptionMsg = f"Call not rate limited: Unexpected status code for call {i + 1}: {status_code} (Expected: 429)";
raise Exception(exceptionMsg)

logging.info(f"Call {i + 1}/{args.cap}: Status Code {status_code}")

print(f"Completed {args.cap} calls to VizQL Data Service.")



if __name__ == "__main__":
main()


0 comments on commit b838d02

Please sign in to comment.