Tableau Server如何批量更新数据源服务器名称?

问题描述

我们的Oracle数据库升级到新服务器,因此它具有新的服务器名称。我们在Tableau Server上发布的大多数工作簿都在连接到此Oracle数据库用户名密码保持不变,但是服务器地址已更改。我使用了以下Python代码。它可以识别需要服务器地址更新的正确工作簿,但是会产生错误:'PW Update Failed with error:

404004: Resource Not Found
    Datasource '5f125136-22da-48d0-bdc7-8e5edde8d809' Could not be found.

'''

import tableauserverclient as TSC
import re

tableau_auth = TSC.TableauAuth('site_admin_username','site_admin_password',site_id='default') # site_id not needed if there is only one


search_server_regex = 'oldserver123' # server to search
replace_server = 'newserver123'  # use if server name/address is changing- otherwise make it the same as search_server
overwrite_credentials = False    # 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 = 'username' 
replace_username = 'username'
replace_pw = 'password'

request_options = TSC.RequestOptions(pagesize=1000) # this needs to be > # of workbooks/data connections on the site

server = TSC.Server('http://tableau_server:8000') # tableau server



y = 0   # to keep track of how many are changed

try:

    
    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)
                            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(y))

''' 如何修复代码

解决方法

您必须更新工作簿。

server.workbooks.update_connection(wb,connection)