在 Python 中通过 ARM 模板部署 Azure 数据工厂时出现问题

Problems deploying Azure Data Factory via ARM template in Python

我正在使用下面的代码尝试通过 ARM 模板部署 Azure 数据工厂,但出现错误:

msrest.exceptions.ValidationError: Parameter 'Deployment.properties' can not be None.

代码:

from haikunator import Haikunator
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import DeploymentMode

parameters = {
    'factoryName': 'TestADF',
    'dataFactory_location': 'uksouth'
}
parameters = {k: {'value': v} for k, v in parameters.items()}

deployment_properties = {
    'mode': DeploymentMode.incremental,
    'template': template,
    'parameters': parameters
}

deployment_async_operation = self.client.deployments.begin_create_or_update(
    self.resource_group, 'azure-sample',   deployment_properties
)
deployment_async_operation.wait()

我正在使用 azure-mgmt-resource 版本:16.0.0

已编辑: 添加了导出为 ARM 模板的两个 json 文件:

模板文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "factoryName": {
            "type": "string",
            "metadata": "Data Factory name",
            "defaultValue": "TestADF"
        },
        "dataFactory_location": {
            "type": "string",
            "defaultValue": "uksouth"
        }
    },
    "variables": {
        "factoryId": "[concat('Microsoft.DataFactory/factories/', parameters('factoryName'))]"
    },
    "resources": [
        {
            "name": "[parameters('factoryName')]",
            "type": "Microsoft.DataFactory/factories",
            "apiVersion": "2018-06-01",
            "properties": {},
            "dependsOn": [],
            "location": "[parameters('dataFactory_location')]"
        }
    ]
}

模板参数文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "factoryName": {
            "value": "TestADF"
        },
        "dataFactory_location": {
            "value": "uksouth"
        }
    }
}

编辑:基于@Jim Xu 的回答:

class Deployer(object):

    def __init__(self):

        client_id = ''
        client_secret = ''
        tenant_id = ''
        
        self.subscription_id = ''
        self.resource_group = ''       
        self.creds = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
        self.client = ResourceManagementClient( self.creds, '')
    
    def deploy(self):
        parameters = {
            'factoryName': 'TestAdfPipeline',
            'dataFactory_location': 'uksouth'
        }
        parameters = {k: {'value': v} for k, v in parameters.items()}
        with open(r'ArmTemplateDeployer\templates\TestAdftemplate.json', 'r') as template_file_fd:
            template = json.load(template_file_fd)
        deployment_properties = {
            'mode': DeploymentMode.incremental,
            'template': template,
            'parameters': parameters
        }
        deployment_async_operation = self.client.deployments.begin_create_or_update(
            self.resource_group, 'azure-sample', {'properties': deployment_properties, 'tags': []})
        deployment_async_operation.wait()

感谢您的帮助。

当我们使用包azure-mgmt-resource中的方法ResourceManagementClient.deployments.begin_create_or_update()部署azure arm template时。我们需要提供 resource_group_namedeployment_name 和对象 Deployment. For more details, please refer to here

因此,我们需要将代码 deployment_async_operation = self.client.deployments.begin_create_or_update(self.resource_group, 'azure-sample',deployment_properties) 更新为 deployment_async_operation = client.deployments.begin_create_or_update('testdata','azure-sample',{'properties': deployment_properties,'tags': []})

例如

from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import DeploymentMode
from azure.identity import ClientSecretCredential
import json


CLIENT_ID = ''
CLIENT_SECRET = ''
TENANT_ID = ''

creds = ClientSecretCredential(tenant_id=TENANT_ID,
                               client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
client = ResourceManagementClient(
    creds, '')

parameters = {
    'factoryName': 'TestADFderg',
    'dataFactory_location': 'uksouth'
}
parameters = {k: {'value': v} for k, v in parameters.items()}
with open('./template.json', 'r') as template_file_fd:
    template = json.load(template_file_fd)
deployment_properties = {
    'mode': DeploymentMode.incremental,
    'template': template,
    'parameters': parameters
}
deployment_async_operation = client.deployments.begin_create_or_update(
    'testdata', 'azure-sample', {'properties': deployment_properties, 'tags': []})
deployment_async_operation.wait()