如何将Azure数据工厂链接服务连接字符串作为纯文本传递?

时间:2018-09-18 13:59:06

标签: azure-data-factory

使用Azure数据工厂,我试图创建一个链接服务,通过自托管的集成运行时连接到本地sql服务器。我正在使用Python Azure SDK。

服务创建成功,但是我无法在脚本中正确获得连接字符串。应该是纯文本形式的服务器名称和数据库名称,但是sdk仅接受变量作为SecureString。

以下是一些示例代码:

from azure.mgmt.datafactory.models import (SecureString, SqlServerLinkedService)
from pprint import pprint
from azure_setup_automation.utils import print_item, pdir
from utils.config import get_config_sql, get_config_azure
from azure_setup_automation.utils import (get_azure_credentials)
from azure.mgmt.datafactory import DataFactoryManagementClient

conf = get_config_azure()
local_conf = get_config_sql()
credentials = get_azure_credentials(conf)
adf_client = DataFactoryManagementClient(credentials, conf.subscription_id)

connection_string_source = (
    f'Data Source={local_conf.server};'
    f'Initial Catalogue={local_conf.database};'
    'Integrated Security=False;'
)

ls_name_local_sql = '02_test_linked_service_local_sql'

ls_local_storage = SqlServerLinkedService(
    connection_string=SecureString(connection_string_source),
    connect_via={"referenceName": "IRTest1", "type": "IntegrationRuntimeReference"},
    user_name=local_conf.user_id,
    password=SecureString(local_conf.password)
)

ls_obj_local_sql = adf_client.linked_services.create_or_update(
    conf.resource_group_name,
    conf.azure_data_factory_name,
    ls_name_local_sql,
    properties = ls_local_storage
)

#print_item(ls_obj_local_sql)
#pdir(ls_obj_local_sql)
#print('-------------------------')
#print (ls_obj_local_sql.type)
#print (ls_obj_local_sql.serialize())

pprint (ls_obj_local_sql.as_dict())

这将打印以下内容。

{'etag': 'XXXX-0000-0000-0000-5ba0fcb10000',
 'id': '/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/XXX/providers/Microsoft.DataFactory/factories/XXXX/linkedservices/02_test_linked_service_local_sql',
 'name': '02_test_linked_service_local_sql',
 'properties': {'connect_via': {'reference_name': 'IRTest1',
                                'type': 'IntegrationRuntimeReference'},
                'connection_string': {'type': 'SecureString',
                                      'value': '**********'},
                'encrypted_credential': 'XXXXXXXXXXXXXXXXXXXXXXX',
                'type': 'SqlServer',
                'user_name': 'XXXXX\\XXXXX'},
 'type': 'Microsoft.DataFactory/factories/linkedservices'}

这是ADF门户的图片。说明参数如何显示星号以及连接失败的方式:Link to image

有没有办法以纯文本形式传递这些参数而无需手动更改它们?

1 个答案:

答案 0 :(得分:1)

这全都归功于打字错误。显然,美国人对目录的拼写方式有所不同:

f'Initial Catalog={local_conf.database};'

最后,即使门户网站界面以这种方式显示,连接字符串也无需采用纯文本格式。我误解了正在发生的事情,以为该错误是因为SDK无法解析SecureString。