import tableauserverclient as TSC
import requests
tableau_auth = TSC.TableauAuth('username', 'password', site_id='siteid') # site_id not needed if there is only one
search_server = 'tablu-nhgc' # server to search... use pipe '|' for additional search criteria
search_server_regex = search_server.replace('.','\.') # replace periods with escape character
replace_server = 'tablu-nhgc' # use if server name/address is changing- otherwise make it the same as search_server
overwrite_credentials = True # set to false to use existing credentials
search_for_certain_users = True # set to True if you only want to update connections for certain usernames
search_username = 'readonly'
replace_username = 'readonly'
replace_pw = 'newpassword'
request_options = TSC.RequestOptions(pagesize=1000) # this needs to be > # of workbooks/data connections on the site
server = TSC.Server('https://tablu-nhgc.com') # tableau server
server.add_http_options({'verify': False})#Update published data source connections
x = 0 # to keep track of how many are changed
y = 0
try:
with server.auth.sign_in(tableau_auth):
all_datasources, pagination_item = server.datasources.get(req_options=request_options)
print("Total Datasources to Search: {}".format(len(all_datasources)))
for datasource in all_datasources: # iterate through all datasources
server.datasources.populate_connections(datasource) # look up connection info for each datasource
for item,conn in enumerate(datasource.connections): # iterate through each connection in datasource
if re.search(search_server_regex,datasource.connections[item].server_address,re.IGNORECASE): # search connection for server string (case insensitive)
connection = datasource.connections[item] # copy existing connection info to variable
if search_for_certain_users and re.search(search_username,connection.username,re.IGNORECASE):
# print(datasource.name, '-', connection.connection_type)
connection.server_address = replace_server
connection.embed_password = False
if overwrite_credentials:
connection.embed_password = True
connection.username = replace_username
connection.password = replace_pw
server.datasources.update_connection(datasource, connection)
x = x + 1
elif not search_for_certain_users:
# print(datasource.name, '-', connection.connection_type)
connection.server_address = replace_server
connection.embed_password = False
if overwrite_credentials:
connection.embed_password = True
connection.username = replace_username
connection.password = replace_pw
server.datasources.update_connection(datasource, connection)
x = x + 1
print("Datasource Connections Changed: {}".format(x))
# Update workbook connections
y = 0
with server.auth.sign_in(tableau_auth):
all_workbooks, pagination_item = server.workbooks.get(req_options=request_options)
print("Total Workbooks to Search: {}".format(len(all_workbooks)))
for wb in all_workbooks:
server.workbooks.populate_connections(wb)
for item,conn in enumerate(wb.connections): #make sure to iterate through all connections in the workbook
if wb.connections[item].connection_type != 'sqlproxy': #sqlproxy indicates published datasource
if re.search(search_server_regex ,wb.connections[item].server_address,re.IGNORECASE):
connection = wb.connections[item]
if search_for_certain_users and re.search(search_username, connection.username, re.IGNORECASE):
# print(wb.name, '-', connection.connection_type)
connection.server_address = replace_server
connection.embed_password = False
if overwrite_credentials:
connection.embed_password = True
connection.username = replace_username
connection.password = replace_pw
server.datasources.update_connection(wb, connection)
y = y + 1
elif not search_for_certain_users:
# print(wb.name, '-', connection.connection_type)
connection.server_address = replace_server
connection.embed_password = False
if overwrite_credentials:
connection.embed_password = True
connection.username = replace_username
connection.password = replace_pw
server.datasources.update_connection(wb, connection)
y = y + 1
print("Workbook Connections Changed: {}".format(y))
except Exception as e:
print("PW Update Failed with error: {}".format(e))
print("Connections Updated: {}".format(x+y))
Comments
Post a Comment